Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Branch Example

Similar presentations


Presentation on theme: "Conditional Branch Example"— Presentation transcript:

1 Conditional Branch Example
absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 int absdiff(int x, int y) { int result; if (x > y) { result = x-y; } else { result = y-x; } return result; Setup Body1 Note labels Finish Body2 x86

2 Conditional Branch Example (Cont.)
int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } int absdiff(int x, int y) { int result; if (x > y) { result = x-y; } else { result = y-x; } return result; Lets look at the code a little differently to make it easier to look at the assembly version. C allows “goto” as means of transferring control Closer to machine-level programming style Generally considered bad coding style x86

3 Conditional Branch Example (Cont.)
int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 Loading x and y into registers. int x %edx int y %eax x86

4 Conditional Branch Example (Cont.)
int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 Performing the comparison. Goto else. int x %edx int y %eax x86

5 Conditional Branch Example (Cont.)
int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 Edx = edx-eax. int x %edx int y %eax x86

6 Conditional Branch Example (Cont.)
int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 int x %edx int y %eax x86

7 Conditional Branch Example (Cont.)
int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 int x %edx int y %eax x86

8 General Conditional Expression Translation
if (Test) val = Then-Expr; else val = Else-Expr; C Code val = Test ? Then-Expr : Else-Expr; result = x>y ? x-y : y-x; Test is expression returning integer = 0 interpreted as false 0 interpreted as true Create separate code regions for then & else expressions Execute appropriate one How might you make this more efficient? Goto Version More efficient if we had conditional move. It would avoid using a branch instruction. nt = !Test; if (nt) goto Else; val = Then-Expr; Done: . . . Else: val = Else-Expr; goto Done; x86

9 Conditionals: x86-64 Conditional move instruction cmovC src, dest
int absdiff( int x, int y) { int result; if (x > y) { result = x-y; } else { result = y-x; } return result; absdiff: # x in %edi, y in %esi movl %edi, %eax # eax = x movl %esi, %edx # edx = y subl %esi, %eax # eax = x-y subl %edi, %edx # edx = y-x cmpl %esi, %edi # x:y cmovle %edx, %eax # eax=edx if <= ret cmov instructions available in IA32 since 1995, but not used by gcc unless –march=686 flag used. They are used regularly in x86-64. Conditional move instruction cmovC src, dest Move value from src to dest if condition C holds More efficient than conditional branching (simple control flow) But overhead: both branches are evaluated x86

10 PC Relative Addressing
0x cmp r2, r x1000 0x je 0x x1002 0x … x1004 … … … 0x add r3, r x1072 PC relative branches are relocatable Absolute branches are not x86


Download ppt "Conditional Branch Example"

Similar presentations


Ads by Google