cs356 unit 5
play

CS356 Unit 5 x86 Control Flow 5.2 JUMP/BRANCHING OVERVIEW 5.3 - PowerPoint PPT Presentation

5.1 CS356 Unit 5 x86 Control Flow 5.2 JUMP/BRANCHING OVERVIEW 5.3 Concept of Jumps/Branches Assembly is executed in sequential movq addq order by default ---- Jump instruction (aka "branches") ---- ---- cause execution


  1. 5.1 CS356 Unit 5 x86 Control Flow

  2. 5.2 JUMP/BRANCHING OVERVIEW

  3. 5.3 Concept of Jumps/Branches • Assembly is executed in sequential movq addq order by default ---- • Jump instruction (aka "branches") ---- ---- cause execution to skip ahead or ---- back to some other location movq • Jumps are used to implement control addq structures like if statements & loops ---- jmp ---- ---- if( x < 0 ){ while ( x > 0 ){ } else { } }

  4. 5.4 Jump/Branch Instructions • Jump (aka "branch") instructions allow us to jump backward or forward in our code • How? By manipulating the Program Counter (PC) • Operation: PC = PC + displacement – Compiler/programmer specifies a "label" for the instruction to branch to; then the assembler will determine the displacement Instrucs. In Memory Instrucs. In Memory Proc. Proc. 0x400 label ---- 0x400 ---- 0x404 ---- PC/ 0x424 0x404 PC/ 0x400 jmp label 0x408 IP IP 0x408 ---- ---- . . ---- ---- . . ---- jmp label 0x424 0x424 label ---- ---- Jump Forward => Conditional Jump Back => Loop

  5. 5.5 Conditional vs. Unconditional Jumps • Two kinds of jumps/branches if( x < 0 ){ < ---- • Conditional ---- >= } – Jump only if a condition is true, else { otherwise continue sequentially } – x86 instructions: je, jne, jge, … (see next slides) while ( x < 0 ){ < • Need a way to compare and check >= F conditions } • Needed for if, while, for L1: jge L2 • Unconditional ---- – Always jump to a new location ---- jmp L1 – x86 instruction: jmp label L2: ---- x86 View

  6. 5.6 Condition Codes MAKING A DECISION

  7. 5.7 Condition Codes (Flags) CS:APP 3.6.1 Processor • The processor hardware performs several %rax = 1 tests on the result of most instructions %rcx = • 0x80000000 Each test generates a True/False (1 or 0) outcome which are recorded in various bits of %rdx = 2 the FLAGS register in the process EFLAGS Reg • The tests and associated bits are: – SF = Sign Flag 11 7 6 31 0 • Tests if the result is negative (just a copy of the 0 0 0 0 MSB of the result of the instruction) – ZF = Zero Flag OF SF ZF CF • Tests if the result is equal to 0 subl %edx, %eax – OF = 2’s complement Overflow Flag 0 1 0 1 • Set if signed overflow has occurred OF SF ZF CF – CF = Carry Flag Unsigned Overflow • Not just the carry-out, 1 if unsigned overflow • Unsigned Overflow: carry out in addition, or borrow out in subtraction

  8. 5.8 cmp and test Instructions • cmp [bwql] src1, src2 – Compares src2 to src1 (e.g. src2 < src1 , src2 == src1 ) – Performs ( src2 – src1 ) and sets the condition codes based on the result – src1 and src2 are not changed (subtraction result is only used for condition codes and then discarded) • test [bwql] src1, src2 – Performs ( src1 & src2 ) and sets condition codes – src1 and src2 are not changed, OF and CF always set to 0 – Often used with the src1 = src2 (i.e., test %eax, %eax ) to check if a value is 0 or negative (ZF and SF)

  9. 5.9 Condition Code Exercises Processor Registers EFLAGS Reg 0000 0000 0000 0001 rax 0000 0000 0000 0000 rbx 11 7 6 31 0 0000 0000 0000 8801 rcx ? ? ? ? 0000 0000 0000 0002 rdx OF SF ZF CF – addl $0x7fffffff,%edx 1 1 0 0 0000 0000 8000 0001 rdx – andb %al, %bl OF SF ZF CF 0 0 1 0 0000 0000 0000 0000 rbx OF SF ZF CF – addb $0xff, %al 0 0 1 1 0000 0000 0000 0000 rax OF SF ZF CF – cmpw $0x7000, %cx 1 0 0 0 0000 0000 0000 1801 result 0000 0000 0000 8801 rcx OF SF ZF CF

  10. 5.10 Conditional Branches Processor • Comparison in x86 is usually a 2-step %rax = 1 (2-instruction) process %rdx = 2 • Step 1: cmpl %edx, %eax – Execute an instruction that will compare or examine the data (e.g. cmp, test, etc.) EFLAGS Reg – Results of comparison will be saved in the EFLAGS register via the condition 0 11 7 6 31 codes 0 1 0 1 • Step 2: OF SF ZF CF – Use a conditional jump (je, jne, jl, etc.) that will check for a certain comparison jne L1 # jump if ZF=0 result of the previous instruction 0 1 0 0 OF SF ZF CF

  11. 5.11 Conditional Jump Instructions CS:APP 3.6.3 • Figure 3.15 from CS:APP, 3e Instruction Synonym Jump Condition Description jmp label jmp *(Operand) je label jz ZF Equal / zero jne label jnz ~ZF Not equal / not zero js label SF Negative jns label ~SF Non-negative jg label jnle ~(SF ^ OF) & ~ZF Greater (signed >) jge label jnl ~(SF ^ OF) Greater or Equal (signed >=) jl label jnge (SF ^ OF) Less (signed <) Less of equal (signed <=) jle label jng (SF ^ OF) | ZF ja label jnbe ~CF & ~ZF Above (unsigned >) jae label jnb ~CF Above or equal (unsigned >=) jb label jnae CF Below (unsigned <) jbe label jna CF | ZF Below or equal (unsigned <=) Reminder : For all jump instructions other than jmp (which is unconditional), some previous instruction (cmp, test, etc.) is needed to set the condition codes to be examined by the jmp

  12. 5.12 Condition Code Exercises Processor Registers 0000 0000 0000 0001 rax 0000 0000 0000 0002 rbx 0000 0000 ffff fffe rcx 0000 0000 0000 0000 rdx OF SF ZF CF Order: f1: __1__ testl %edx, %edx 0 0 1 0 __2__ je L2 __5___ L1: cmpw %bx, %ax 0 1 0 1 __6___ jge L3 __3,7_ L2: addl $1,%ecx 0 1 0 0 0 0 1 1 __4,8_ js L1 ____9_ L3: ret

  13. 5.13 Control Structure Examples 1 CS:APP 3.6.5 func1: // x = %edi, y = %esi, res = %rdx cmpl %esi, %edi void func1(int x, int y, int *res) jge .L2 { movl %edi, (%rdx) if (x < y) ret *res = x; .L2: else movl %esi, (%rdx) gcc -S -Og func1.c *res = y; ret } // x = %edi, y = %esi, res = %rdx func2: cmpl $-1, %edi void func2(int x, int y, int *res) je .L6 { cmpl $-1, %esi if(x == -1 || y == -1) je .L6 *res = y-1; testl %edi, %edi else if(x > 0 && y < x) jle .L5 cmpl %esi, %edi *res = x+1; jle .L5 else addl $1, %edi *res = 0; movl %edi, ( %rdx ) ret } .L5: movl $0, ( %rdx ) gcc -S –O3 func2.c ret .L6: subl $1, %esi movl %esi, ( %rdx ) ret

  14. 5.14 Control Structure Examples 2 CS:APP 3.6.7 func3: // str = %rdi movl $0, %eax int func3(char str[]) jmp .L2 { .L3: int i = 0; addl $1, %eax while(str[i] != 0){ .L2: i++; movslq %eax, %rdx gcc -S -Og func3.c } cmpb $0, (%rdi,%rdx) return i; jne .L3 ret } func4: // dat = %rdi, len = %esi movl (%rdi), %eax int func4(int dat[], int len) movl $1, %edx { jmp .L2 .L4: int min = dat[0]; movslq %edx, %rcx for (int i=1; i < len; i++) { movl (%rdi,%rcx,4), %ecx if (dat[i] < min) { cmpl %ecx, %eax min = dat[i]; jle .L3 } movl %ecx, %eax gcc -S -Og func4.c } .L3: addl $1, %edx return min; .L2: } cmpl %esi, %edx jl .L4 ret

  15. 5.15 Branch Displacements CS:APP 3.6.4 // dat = %rdi, len = %esi • Recall : Jumps perform PC = PC + displacement int func4(int dat[], int len) { • Assembler converts jumps and labels to int i, min = dat[0]; for(i=1; i < len; i++){ appropriate displacements if(dat[i] < min){ min = dat[i]; • Examine the disassembled output (below) } } especially the machine code in the left column return min; } – Displacements are in the 2 nd byte of the instruction C Code – Recall: PC increments to point at next instruction while jump is fetched and BEFORE the jump is executed func4: movl (%rdi), %eax 0000000000000000 <func4>: movl $1, %edx 0: 8b 07 mov (%rdi),%eax jmp .L2 2: ba 01 00 00 00 mov $0x1,%edx .L4: 7: eb 0f jmp 18 <func4+0x18> movslq %edx, %rcx 9: 48 63 ca movslq %edx,%rcx movl (%rdi,%rcx,4), %ecx c: 8b 0c 8f mov (%rdi,%rcx,4),%ecx cmpl %ecx, %eax f: 39 c8 cmp %ecx,%eax jle .L3 11: 7e 02 jle 15 <func4+0x15> movl %ecx, %eax 13: 89 c8 mov %ecx,%eax .L3: 15: 83 c2 01 add $0x1,%edx addl $1, %edx 18: 39 f2 cmp %esi,%edx .L2: 1a: 7c ed jl 9 <func4+0x9> cmpl %esi, %edx 1c: f3 c3 retq jl .L4 ret x86 Disassembled Output x86 Assembler

  16. 5.16 CONDITIONAL MOVES

  17. 5.17 Cost of Jumps CS:APP 3.6.6 func1: • Fact: Modern processors execute multiple instructions cmpl $-1, %edi je .L6 at one time cmpl $-1, %esi je .L6 – While earlier instructions are executing the processor testl %edi, %edi jle .L5 can be fetching and decoding later instructions cmpl %esi, %edi jl .L5 – This overlapped execution is known as pipelining and is addl $1, %edi movl %edi, ( %rdx ) key to obtaining good performance ret .L5: • Problem: Conditional jumps limit pipelining because movl $0, ( %rdx ) ret when we reach a jump, the comparison results it relies .L6: subl $1, %esi on may not be computed yet movl %esi, ( %rdx ) ret – It is unclear which instruction to fetch next – To be safe we have to stop and wait for the jump condition to be known time cmpl fetch decode execute fetch decode execute jne ??? fetch

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend