Presentation is loading. Please wait.

Presentation is loading. Please wait.

Machine-Level Representation of Programs II

Similar presentations


Presentation on theme: "Machine-Level Representation of Programs II"— Presentation transcript:

1 Machine-Level Representation of Programs II

2 Outline Conditional codes Jump instructions Loop Control Switch
Suggested reading Chap 3.6

3 Assembly Programmer’s View
FF BF 7F 3F C0 80 40 00 Stack DLLs Text Data Heap 08 %eax %edx %ecx %ebx %esi %edi %esp %ebp %al %ah %dl %dh %cl %ch %bl %bh %eip %eflag Addresses Instructions

4 Condition codes Condition codes A set of single-bit
Maintained in a condition code register Describe attributes of the most recently arithmetic or logical operation

5 Condition codes EFLAGS CF: Carry Flag
The most recent operation generated a carry out of the most significant bit Used to detect overflow for unsigned operations OF: Overflow Flag The most recent operation caused a two’s complement overflow — either negative or positive

6 Condition codes EFLAGS ZF: Zero Flag
The most recent operation yielded zero SF: Sign Flag The most recent operation yielded a negative value

7 Setting Conditional Codes
Implicit Setting By Arithmetic Operations addl Src,Dest C analog: t = a+b CF set if carry out from most significant bit Used to detect unsigned overflow ZF set if t == 0 SF set if t < 0 OF set if two’s complement overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)

8 Conditional Code lea instruction Xorl instruction Shift instruction
has no effect on condition codes Xorl instruction The carry and overflow flags are set to 0 Shift instruction carry flag is set to the last bit shifted out Overflow flag is set to 0

9 Setting Conditional Codes
Explicit Setting by Compare Instruction cmpl Src2,Src1 cmpl b,a like computing a-b without setting destination CF set if carry out from most significant bit Used for unsigned comparisons ZF set if a == b SF set if (a-b) < 0 OF set if two’s complement overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

10 Setting Conditional Codes
Explicit Setting by Test instruction testl Src2,Src1 Sets condition codes based on value of Src1 & Src2 Useful to have one of the operands be a mask testl b,a like computing a&b without setting destination ZF set when a&b == 0 SF set when a&b < 0

11 Accessing Conditional Codes
The condition codes cannot be read directly One of the most common methods of accessing them is setting an integer register based on some combination of condition codes Set commands

12 Accessing Conditional Codes
After each set command is executed A single byte to 0 or to 1 is obtained The descriptions of the different set commands apply to the case where a comparison instruction has been executed

13 Accessing Conditional Codes
Instruction Synonym Effect Set Condition Sete Setz ZF Equal/zero Setne Setnz ~ZF Not equal/not zero Sets SF Negative Setns ~SF Nonnegative Setl Setnge SF^OF Less Setle Setng (SF^OF)|ZF Less or Equal Setg Setnle ~(SF^OF)&~ZF Greater Setge Setnl ~(SF^OF) Greater or Equal Seta Setnbe ~CF&~ZF Above Setae Setnb ~CF Above or equal Setb Setnae CF Below Setbe Setna CF|ZF Below or equal

14 Accessing Conditional Codes
The destination operand is either one of the eight single-byte register elements or a memory location where the single byte is to be stored To generate a 32-bit result we must also clear the high-order 24 bits

15 Accessing Conditional Codes
Initially a is in %edx, b is in %eax 1 cmpl %eax, %edx #compare a:b 2 setl %al #set low order by to 0 or 1 3 movzbl %al, %eax #set remaining bytes of %eax to 0

16 Two of the most important parts of program execution
Control Two of the most important parts of program execution Data flow (Accessing and operating data) Control flow (control the sequence of operations)

17 Sequential execution is default
Control Sequential execution is default the instructions are executed in the order they appear in the program Chang the control flow Jump instructions

18 Jump Instructions 1 xorl %eax, %eax Set %eax to 0 2 jmp .L1 Goto .L1 3 movl (%eax), %edx Null pointer dereference 4 .L1: 5 popl %edx

19 Jumps unconditionally Direct jump: jmp label
Unconditional jump Jumps unconditionally Direct jump: jmp label jmp .L Indirect jump: jmp *Operand jmp *%eax jmp *(%eax)

20 Conditional jump Either jump or continue executing at the next instruction in the code sequence Depending on some combination of the condition codes All direct jump

21 Jump Instructions jle .L2 .L5: movl %edx, %eax sarl $1, %eax
subl %eax, %edx Leal (%edx, %edx, 2), %edx testl %edx, %edx jg L5 9 .L2: 10 movl %edx, %eax

22 Example for Jump 8: 7e 0d jle 17<silly+0x17>
a: 89 d0 mov %edx, %eax dest1: 3 c: c1 f8 sar %eax e: 29 c2 sub %eax, %edx 10: 8d lea (%edx, %edx, 2), %edx 6 13: 85 d2 test %edx, %edx 15: 7f f3 jg a<silly+0x10> 17: 89 d0 movl %edx, %eax dest2: d+a = 17 17+f3(-d) =a

23 Example for Jump 804839c: 7e 0d jle 17<silly+0x17>
804839e: 89 d0 mov %edx, %eax dest1: a0: c1 f8 sar %eax 80483a2: 29 c2 sub %eax, %edx 80483a4: 8d lea (%edx, %edx, 2), %edx a7: 85 d2 test %edx, %edx 80483a9: 7f f3 jg a<silly+0x10> 80483ab: 89 d0 movl %edx, %eax dest2: d e = 80483ab 80483ab+f3(-d) = e

24 Jump Instructions PC-relative Absolute address
Jump target is an offset relative to the address of the instruction just followed jump (pointed by PC) Absolute address Jump target is an absolute address

25 Loop

26 Control Constructs in C
Gotos goto L break continue Branch if () { } else { } switch () { }

27 Control Constructs in C
Loop while () { } do { } while () for (init; test; incr) { }

28 Translating Conditional Branches
t = test-expr ; if ( t ) goto true ; else-statement goto done true: then-statement done: if ( test-expr ) then-statement else else-statement

29 Translating Conditional Branches
int absdiff(int x, int y) { if (x < y) return y – x; else return x – y; } int absdiff(int x, int y) { int rval ; if (x < y) goto less rval = x – y ; goto done; less: rval = y – x; done: return rval; }

30 Jump Instructions movl 8(%ebp), %edx get x movl 12(%ebp), %eax get y
cmpl %eax, %edx cal x - y jl .L if x < y goto less subl %eax, %edx compute x - y movl %edx, %eax set return val jmp .L5 goto done .L3: less: subl %edx, %eax compute y – x .L5: done: Begin Completion code

31 Do-while Translation do body-statement while (test-expr) loop:
t = test-expr; if ( t ) goto loop ;

32 Do-while Translation .L6: lea (%ebx, %edx), %eax movl %edx, %ebx
movl %eax, %edx incl %ecx cmpl %esi, %ecx jl L6 movl %ebx, %eax int fib_dw(int n) { int i = 0; int val = 0 ; int nval = 1 ; do { int t = val + nval ; val = nval ; nval = t ; i++; } while ( i<n) ; return val ; } register value initially %ecx i %esi n %ebx val %edx nval 1 %eax t -

33 While Loop Translation
while (test-expr) body-statement loop: if ( !test-expr) t = test-expr goto done; if ( !t ) do goto done; body-statement body-statement while(test-expr) goto loop; done: done:

34 While Loop Translation
int fib_w_goto(int n) { int val=1; int nval=1; int nmi, t ; if ( val >= n ) goto done ; nmi = n-1; loop: t=val+nval ; val = nval ; nval = t ; nmi--; if ( nmi ) goto loop done: return val } int fib_w(int n) { int i=1; int val=1; int nval=1; while ( i<n ) { int t=val+nval ; val = nval ; nval = t ; i++; } return val ;

35 While Loop Translation
Register usage Register Variable Initially %edx nmi n-1 %ebx val 1 %ecx nval movl 8(%ebp), %eax movl $1, %ebx movl $1, %ecx cmpl %eax, ebx jge .L9 lea –1(%eax), %edx .L10: lea (%ecx, %ebx), %eax movl %ecx, %ebx movl %eax, %ecx decl %edx jnz .L10 .L9:

36 While Loop Translation
/* strcpy: copy t to s; pointer version 2 */ void strcpy(char *s, char *t){ while ((*s = *t) != '\0') { s++ ; t++ ; }

37 While Loop Translation
movl 12(%ebp), %eax movzbl (%eax), %edx movl 8(%ebp), %eax movb %dl, (%eax) movzbl (%eax), %eax testb %al, %al jne L3 popl %ebp ret _strcpy: pushl %ebp movl %esp, %ebp jmp L2 L3: addl $1, 8(%ebp) addl $1, 12(%ebp)

38 For Loop Translation for ( init-expr; test-expr; update-expr)
body-statement init-expr while ( test-expr) { body-statement update-expr }

39 For Loop Translation /* strcmp: return <0 if s<t, 0 if s==t, >0 if s>t */ int strcmp(char *s, char *t) { for (; *s == *t ; s++, t++) if (*s == '\0') return 0; return *s - *t; }

40 For Loop Translation _strcmp: pushl %ebp movl %esp, %ebp subl $4, %esp
jmp L2 L5: movl 8(%ebp), %eax movzbl (%eax), %eax testb %al, %al jne L3 movl $0, -4(%ebp) jmp L4 L3: addl $1, 8(%ebp) addl $1, 12(%ebp) L2: movl 8(%ebp), %eax movzbl (%eax), %edx movl 12(%ebp), %eax movzbl (%eax), %eax cmpb %al, %dl je L5

41 For Loop Translation movl 8(%ebp), %eax movzbl (%eax), %eax
movsbl %al,%edx movl 12(%ebp), %eax movsbl %al,%eax movl %edx, %ecx subl %eax, %ecx movl %ecx, -4(%ebp) L4: movl -4(%ebp), %eax leave ret

42 (b) Implementation using conditional assignment
Conditional Move Original C code 1 int absdiff(int x, int y) { 2 return x < y ? y-x : x-y; 3 } (b) Implementation using conditional assignment 1 int cmovdiff(int x, int y) { 2 int tval = y-x; 3 int rval = x-y; 4 int test = x < y; 5 /* Line below requires single instruction: */ 7 if (test) rval = tval; 8 return rval; 9 } 42 42

43 (c) Generated assembly code
Conditional Move (c) Generated assembly code (x at %ebp+8, y at %ebp+12) movl 8(%ebp), %ecx Get x movl 12(%ebp), %edx Get y movl %edx, %ebx Copy y subl %ecx, %ebx Compute y-x movl %ecx, %eax Copy x subl %edx, %eax Compute x-y and set as return value cmpl %edx, %ecx Compare x:y cmovl %ebx, %eax If < , replace return value with y-x %ecx x %edx y %ebx y-x %eax x-y 43 43

44 Conditional Move instructions suppose that “there is no side effect”
Invalid Situation Conditional Move instructions suppose that “there is no side effect” int cread(int *xp) { return (xp ? *xp : 0); } 44 44

45 Switch

46 Switch Statements int switch_eg(int x, int n) { int result = x ;
switch ( n ) { case 100: result *= 13 ; break ; case 102: result += 10 ; /* fall through */ case 103 result += 11; break ; case 104: case 106: result *= result ; default: result = 0 ; } return result ;

47 Properties of Switch Construct
Integer testing Multiple outcomes (may be a large number) Improve the readability of the source code

48 Switch Statements int switch_eg(int x, int n) { int result = x ;
switch ( n ) { case 100: result *= 13 ; break ; case 102: result += 10 ; /* fall through */ case 103 result += 11; break ; case 104: case 106: result *= result ; default: result = 0 ; } return result ; Multiple cases Integer testing

49 Switch Form switch(op) { case val_0: Block 0 case val_1: Block 1 • • • case val_n-1: Block n–1 }

50 Efficient implementation Avoid long sequence of if-else statement
Jump Table Efficient implementation Avoid long sequence of if-else statement Criteria the number of cases and the sparcity of the case value

51 Jump Table Implementation
Jump Targets Targ0 Targ1 Targ2 Targn-1 jtab: Code Block 0 Targ0: Code Block 1 Targ1: Code Block 2 Targ2: Code Block n–1 Targn-1: Approx. Translation target = JTab[op]; goto *target;

52 Switch Statements int switch_eg(int x, int n) { int result = x ;
switch ( n ) { case 100: result *= 13 ; break ; case 102: result += 10 ; /* fall through */ case 103 result += 11; break ; case 104: case 106: result *= result ; default: result = 0 ; } return result ;

53 Jump Table Implementation
code jt[7] = {loc_a, loc_def, loc_b, loc_c, loc_d, loc_def, loc_d}; int switch_eg_goto ( int x, int n) { unsigned ni = n - 100; int result = x ; if ( ni >6 ) goto loc_def ; //default goto jt[xi]; loc_a: //100 result *= 13 ; goto done ; loc_b: //102 result += 10 ; /* fall through*/ loc_c: //103 result +=11; goto done ; loc_d: //104, 106 result *= result ; loc_def: //default result = 0 ; done: return result ; }

54 Jump Table .section .rodata .align 4 .L7: .long .L3 case 100: loc_a
.long .L2 case 101: loc_def .long .L4 case 102: loc_b .long .L5 case 103: loc_c .long .L6 case 104: loc_d .long .L2 case 105: loc_def .long .L6 case 106: loc_d

55 Jump Table Implementation
movl 8(%ebp), %edx get x movl 12(%ebp), %eax get n subl $100, %eax compute index = n – 100 cmpl $6, %eax compare index:6 ja .L2 If > , goto default jmp *.L7(, %eax, 4) .L2: default: mov $0, %eax result = 0 jmp .L8 goto done

56 Jump Table Implementation
.L5: loc_c: // 103 movl %edx, %eax result = x jmp .L9 goto rest .L3: loc_a: // 100 leal (%edx, %edx, 2), %eax result = x * 3 leal (%edx, %eax, 4), %eax result = x + 4 * result jmp .L8 goto done .L4: loc_b: // 102 leal 10(%edx), %eax result = x + 10

57 Jump Table Implementation
.L9: rest: // fall through addl $11, %eax result += 11 jmp .L8 goto done .L6: loc_d: // 104, 106 movl %edx, %eax result = x imull %edx, %eax result *= x .L8: done:

58 Procedure Call

59 Procedure/Function Implementation
Invoke callee: call (new instructions) Return to caller: ret (new instructions) Passing data: stack, register Registers: calling convention Local variable: stack

60 Why not store local variables in registers ?
No enough registers Array and structures (e.g., a[2]) Need address (e.g., &a)

61 Local Variable Allocation De-allocation Usage Callee Frame
Below saved regs or old %ebp move/sub %esp, (e.g., subl $4, %esp) De-allocation move/add %esp, (e.g., addl $4, %esp) Usage Relative to %esp/%ebp, (e.g., movl %eax, 8(%esp)) retaddr %ebp %esp Callee Frame Old %ebp Saved regs Local variable

62 %ebp %esp Caller Frame Put it Together

63 caller-save registers
%ebp %esp Caller Frame Put it Together caller-save registers 1. Save caller-save registers (%eax, %edx, %ecx)

64 caller-save registers
%ebp %esp Caller Frame Put it Together caller-save registers 1. Save caller-save registers (%eax, %edx, %ecx) 2. Push actual arguments from right to left arguments (n~1)

65 caller-save registers
%ebp %esp Caller Frame Put it Together caller-save registers 1. Save caller-save registers (%eax, %edx, %ecx) 2. Push actual arguments from right to left 3. Call instruction Save return address Transfer control to callee arguments (n~1) retaddr

66 caller-save registers
%ebp %esp Caller Frame Put it Together caller-save registers 4. Save caller %ebp arguments (n~1) retaddr old %ebp

67 caller-save registers
Caller Frame Put it Together caller-save registers 4. Save caller %ebp 5. Set callee %ebp arguments (n~1) retaddr %esp / %ebp old %ebp

68 6. Save callee-save registers (%ebx, %edi, %esi)
Caller Frame Put it Together caller-save registers 4. Save caller %ebp 5. Set callee %ebp 6. Save callee-save registers (%ebx, %edi, %esi) arguments (n~1) retaddr %ebp %esp old %ebp callee-save registers

69 6. Save callee-save registers (%ebx, %edi, %esi)
Caller Frame Put it Together caller-save registers 4. Save caller %ebp 5. Set callee %ebp 6. Save callee-save registers (%ebx, %edi, %esi) 7. Allocate space for local variable arguments (n~1) retaddr %ebp %esp old %ebp callee-save registers local variables

70 n-4. save return value in %eax
Caller Frame Put it Together . . . n-4. save return value in %eax caller-save registers arguments (n~1) retaddr %ebp %esp old %ebp callee-save registers local variables

71 n-4. save return value in %eax n-3. de-allocate local variable
Caller Frame Put it Together . . . n-4. save return value in %eax n-3. de-allocate local variable caller-save registers arguments (n~1) retaddr %ebp %esp old %ebp callee-save registers local variables

72 n-4. save return value in %eax n-3. de-allocate local variable
Caller Frame Put it Together . . . n-4. save return value in %eax n-3. de-allocate local variable n-2. Restore callee-save registers caller-save registers arguments (n~1) retaddr %ebp %esp / old %ebp callee-save registers local variables

73 n-4. save return value in %eax n-3. de-allocate local variable
%ebp %esp Caller Frame Put it Together . . . n-4. save return value in %eax n-3. de-allocate local variable n-2. Restore callee-save registers n-1. Restore caller %ebp caller-save registers arguments (n~1) retaddr old %ebp callee-save registers local variables

74 n-4. save return value in %eax n-3. de-allocate local variable
%ebp %esp Caller Frame Put it Together . . . n-4. save return value in %eax n-3. de-allocate local variable n-2. Restore callee-save registers n-1. Restore caller %ebp n. Ret instruction pop return address Transfer control to caller caller-save registers arguments (n~1) retaddr old %ebp callee-save registers local variables

75 Example 1 int swap_add(int *xp, int *yp) 2 { 3 int x = *xp;
4 int y = *yp; 5 6 *xp = y; 7 *yp = x; 8 return x + y; 9 } 10

76 Example 11 int caller() 12 { 13 int arg1 = 534; 14 int arg2 = 1057;
int sum = swap_add(&arg1, &arg2); int diff = arg1 - arg2; 17 return sum * diff; 19 }

77 Example Before int sum = swap_add(&arg1, &arg2);
Stack frame for caller Saved %ebp -4 arg2(1057) -8 arg1(534) -12 %ebp %esp

78 Parameter Passing 1 leal -4(%ebp),%eax Compute &arg2 2 pushl %eax
Push &arg2 Stack frame for caller Saved %ebp -4 arg2(1057) -8 arg1(534) -12 &arg2 %ebp %esp

79 Parameter Passing 1 leal -4(%ebp),%eax Compute &arg2 2 pushl %eax
Push &arg2 3 leal -8(%ebp),%eax Compute &arg1 4 pushl %eax Push &arg1 Stack frame for caller Saved %ebp -4 arg2(1057) -8 arg1(534) -12 &arg2 -16 &arg1 %ebp %esp

80 Call Instruction 1 leal -4(%ebp),%eax Compute &arg2 2 pushl %eax
Push &arg2 3 leal -8(%ebp),%eax Compute &arg1 4 pushl %eax Push &arg1 5 call swap_add Call the swap_add function Stack frame for caller Saved %ebp -4 arg2(1057) -8 arg1(534) -12 &arg2 -16 &arg1 -20 Return Addr %ebp %esp

81 Setup code in swap_add swap_add: 1 pushl %ebp Save old %ebp
Stack frame for caller Saved %ebp -4 arg2(1057) -8 arg1(534) -12 yp(&arg2) -16 xp(&arg1) -20 Return Addr -24 %ebp %esp

82 Stack frame for swap_add
Setup code in swap_add swap_add: 1 pushl %ebp Save old %ebp 2 movl %esp,%ebp Set %ebp as frame pointer Stack frame for caller 24 Saved %ebp 20 arg2(1057) 16 arg1(534) 12 yp(&arg2) 8 xp(&arg1) 4 Return Addr %ebp %esp Stack frame for swap_add

83 Stack frame for swap_add
Setup code in swap_add swap_add: 1 pushl %ebp Save old %ebp 2 movl %esp,%ebp Set %ebp as frame pointer 3 pushl %ebx Save %ebx Stack frame for caller 24 Saved %ebp 20 arg2(1057) 16 arg1(534) 12 yp(&arg2) 8 xp(&arg1) 4 Return Addr -4 Saved %ebx %ebp %esp Stack frame for swap_add

84 Stack frame for swap_add
Body code in swap_add Stack frame for caller 5 movl 8(%ebp),%edx Get xp 24 Saved %ebp 20 arg2(1057) 16 arg1(534) 12 yp(&arg2) 8 xp(&arg1) 4 Return Addr -4 Saved %ebx %edx xp(=&arg1=%ebp+16) %ebp %esp Stack frame for swap_add

85 Stack frame for swap_add
Body code in swap_add Stack frame for caller 5 movl 8(%ebp),%edx Get xp 6 movl 12(%ebp),%ecx Get yp 24 Saved %ebp 20 arg2(1057) 16 arg1(534) 12 yp(&arg2) 8 xp(&arg1) 4 Return Addr -4 Saved %ebx %edx xp(=&arg1=%ebp+16) %ebp %esp %ecx yp(=&arg2=%ebp+20) Stack frame for swap_add

86 Stack frame for swap_add
Body code in swap_add Stack frame for caller 5 movl 8(%ebp),%edx Get xp 6 movl 12(%ebp),%ecx Get yp 7 movl (%edx),%ebx Get x 8 movl (%ecx),%eax Get y 24 Saved %ebp 20 arg2(1057) 16 arg1(534) 12 yp(&arg2) 8 xp(&arg1) 4 Return Addr -4 Saved %ebx %edx xp(=&arg1=%ebp+16) %ebp %esp %ecx yp(=&arg2=%ebp+20) Stack frame for swap_add %ebx 534 %eax 1057

87 Stack frame for swap_add
Body code in swap_add 9 movl %eax, (%edx) Store y at *xp Stack frame for caller 24 Saved %ebp 20 arg2(1057) 16 arg1(1057) 12 yp(&arg2) 8 xp(&arg1) 4 Return Addr -4 Saved %ebx %edx xp(=&arg1=%ebp+16) %ebp %esp %ecx yp(=&arg2=%ebp+20) Stack frame for swap_add %ebx 534 %eax 1057

88 Stack frame for swap_add
Body code in swap_add 9 movl %eax, (%edx) Store y at *xp 10 movl %ebx, (%ecx) Store x at *yp Stack frame for caller 24 Saved %ebp 20 arg2(534) 16 arg1(1057) 12 yp(&arg2) 8 xp(&arg1) 4 Return Addr -4 Saved %ebx %edx xp(=&arg1=%ebp+16) %ebp %esp %ecx yp(=&arg2=%ebp+20) Stack frame for swap_add %ebx 534 %eax 1057

89 Stack frame for swap_add
Body code in swap_add 9 movl %eax, (%edx) Store y at *xp 10 movl %ebx, (%ecx) Store x at *yp 11 addl %ebx,%eax Set return value = x+y Stack frame for caller 24 Saved %ebp 20 arg2(534) 16 arg1(1057) 12 yp(&arg2) 8 xp(&arg1) 4 Return Addr -4 Saved %ebx %edx xp(=&arg1=%ebp+16) %ebp %esp %ecx yp(=&arg2=%ebp+20) Stack frame for swap_add %ebx 534 %eax 1591

90 Finishing code in swap_add
12 popl %ebx Restore %ebx Stack frame for caller 24 Saved %ebp 20 arg2(534) 16 arg1(1057) 12 yp(&arg2) 8 xp(&arg1) 4 Return Addr -4 Saved %ebx %edx xp(=&arg1=%ebp+16) %ebp %esp %ecx yp(=&arg2=%ebp+20) Stack frame for swap_add %ebx original value %eax 1591

91 Finishing code in swap_add
12 popl %ebx Restore %ebx 13 movl %ebp, %esp Restore %esp Stack frame for caller 24 Saved %ebp 20 arg2(534) 16 arg1(1057) 12 yp(&arg2) 8 xp(&arg1) 4 Return Addr -4 Saved %ebx %edx xp(=&arg1=%ebp+16) %ebp %esp %ecx yp(=&arg2=%ebp+20) Stack frame for swap_add %ebx original value %eax 1591

92 Finishing code in swap_add
12 popl %ebx Restore %ebx 13 movl %ebp, %esp Restore %esp 14 popl %ebp Restore %ebp Stack frame for caller Saved %ebp -4 arg2(534) -8 arg1(1057) -12 yp(&arg2) -16 xp(&arg1) -20 Return Addr Saved %ebx %ebp %esp %edx xp(=&arg1=%ebp+16) %ecx yp(=&arg2=%ebp+20) %ebx original value %eax 1591

93 Finishing code in swap_add
12 popl %ebx Restore %ebx 13 movl %ebp, %esp Restore %esp 14 popl %ebp Restore %ebp 15 ret Return to caller Call by value Stack frame for caller Saved %ebp -4 arg2(534) -8 arg1(1057) -12 yp(&arg2) -16 xp(&arg1) -20 Return Addr Saved %ebx %ebp %esp %edx xp(=&arg1=%ebp+16) %ecx yp(=&arg2=%ebp+20) %ebx original value %eax 1591

94 Recursion Example 1 int fib_rec(int n) 2 { 3 int prev_val, val; 4
2 { 3 int prev_val, val; 4 5 if (n <= 2) 6 return 1; 7 prev_val = fib_rec(n-2); 8 val = fib_rec(n-1); 9 return prev_val + val; 10 }

95 Setup code 1 fib_rec: 2 pushl %ebp Save old %ebp
3 movl %esp,%ebp Set %ebp as frame pointer 4 subl $16,%esp Allocate 16 bytes on stack 5 pushl %esi Save %esi (offset -20) 6 pushl %ebx Save %ebx (offset -24)

96 Recursion . Unused Stack frame +8 n +4 Return Addr Saved %ebp -20
Saved %ebp Unused -20 Saved %esi -24 Saved %ebx %ebp %esp

97 Body code 7 movl 8(%ebp),%ebx Get n 8 cmpl $2,%ebx Compare n:2
9 jle .L if <=, goto terminate 10 addl $-12,%esp Allocate 12 bytes on stack 11 leal -2(%ebx),%eax Compute n-2 12 pushl %eax Push as argument 13 call fib_rec Call fib_rec(n-2)

98 Recursion . Unused Stack frame +8 n +4 Return Addr Saved %ebp -20
Saved %ebp Unused -20 Saved %esi -24 Saved %ebx -40 n-2 %ebp %esp

99 Body code 14 movl %eax,%esi Store result in %esi
15 addl $-12,%esp Allocate 12 bytes to stack 16 leal -1(%ebx),%eax Compute n-1 17 pushl %eax Push as argument 18 call fib_rec Call fib_rec(n-1) 19 addl %esi,%eax Compute val+nval 20 jmp .L Go to done

100 . Unused +8 n +4 Return Addr Saved %ebp -20 Saved %esi -24 Saved %ebx
Saved %ebp Unused -20 Saved %esi -24 Saved %ebx -40 n-2 -56 n-1 Stack frame %ebp %esp

101 Terminal condition 21 .L24: terminate: 22 movl $1,%eax Return value 1

102 Finishing code 23 .L25: done:
24 leal -24(%ebp),%esp Set stack to offset -24 25 popl %ebx Restore %ebx 26 popl %esi Restore %esi 27 movl %ebp,%esp Restore stack pointer 28 popl %ebp Restore %ebp 29 ret Return

103 Permutation #include <stdio.h>
void permute(int a[], int n, int k) { int i; if (k == 0) { for ( i=0; i<n; i++) printf("%d", a[i]); printf("\n"); }

104 Permutation for ( i=0; i<=k; i++) { int tempi = a[i], tempk = a[k];
a[i] = tempk ; a[k] = tempi; permute(a,n,k-1); a[i] = tempi; a[k] = tempk; }

105 Permutation .file "permute.c" .section .rdata,"dr" LC0: .ascii "%d\0"
.text .globl _permute .def _permute; .scl 2; .type 32; .endef

106 Permutation _permute: pushl %ebp movl %esp, %ebp subl $40, %esp
cmpl $0, 16(%ebp) # k == 0 ? jne L2 movl $0, -12(%ebp) # i = 0 jmp L3

107 Permutation L4: movl -12(%ebp), %eax # i sall $2, %eax # 4*i
addl 8(%ebp), %eax # a+4*i movl (%eax), %eax # a[i] movl %eax, 4(%esp) # pushl a[i] movl $LC0, (%esp) # pushl “%d\n” call _printf addl $1, -12(%ebp) # i = i + 1

108 Permutation L3: movl -12(%ebp), %eax cmpl 12(%ebp), %eax # i ? n jl L4
movl $10, (%esp) # \n call _putchar L2: movl $0, -12(%ebp) # i = 0 jmp L5

109 Permutation L6: movl -12(%ebp), %eax sall $2, %eax addl 8(%ebp), %eax
movl (%eax), %eax movl %eax, -8(%ebp) # tempi = a[i] movl 16(%ebp), %eax movl %eax, -4(%ebp) # tempk = a[k]

110 Permutation movl -12(%ebp), %eax sall $2, %eax movl %eax, %edx
addl 8(%ebp), %edx movl -4(%ebp), %eax movl %eax, (%edx) # a[i] = tempk movl 16(%ebp), %eax movl -8(%ebp), %eax movl %eax, (%edx) # a[k] = tempi

111 Permutation movl 16(%ebp), %eax subl $1, %eax
movl %eax, 8(%esp) # pushl k-1 movl 12(%ebp), %eax movl %eax, 4(%esp) # pushl n movl 8(%ebp), %eax movl %eax, (%esp) # pushl a call _permute

112 Permutation movl -12(%ebp), %eax sall $2, %eax movl %eax, %edx
addl 8(%ebp), %edx movl -8(%ebp), %eax movl %eax, (%edx) # a[i] = tempi movl 16(%ebp), %eax movl -4(%ebp), %eax movl %eax, (%edx) # a[k] = tempk

113 Permutation addl $1, -12(%ebp) # i = i + 1 L5: movl -12(%ebp), %eax
cmpl 16(%ebp), %eax jle L6 leave ret

114 Permutation int a[]={1,2,3,4}; void main() {
permute(a, sizeof(a)/sizeof(a[0]), sizeof(a)/sizeof(a[0])-1); }

115 Permutation .globl _a .data .align 4 _a: .long 1 .long 2 .long 3

116 Permutation .text _main: ……… movl $3, 8(%esp) movl $4, 4(%esp)
movl $_a, (%esp) call _permute ret


Download ppt "Machine-Level Representation of Programs II"

Similar presentations


Ads by Google