Fetch operand at Src n Decrement esp by 4 n Write operand at address given by

Fetch operand at src n decrement esp by 4 n write

This preview shows page 90 - 102 out of 155 pages.

Fetch operand at Src n Decrement %esp by 4 n Write operand at address given by %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” Stack Pointer %esp -4
Image of page 90
Rutgers University Santosh Nagarakatte 91 IA32 Stack Popping Popping n popl Dest n Read operand at address given by %esp n Increment %esp by 4 n Write to Dest Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” +4
Image of page 91
%esp %eax %edx %esp %eax %edx %esp %eax %edx 0x104 0x108 0x10c 0x110 0x104 555 213 213 123 Stack Operation Examples 0x108 0x10c 0x110 555 213 123 0x108 0x104 pushl %eax 0x108 0x10c 0x110 213 123 213 popl %edx 0x108 213
Image of page 92
Rutgers University Santosh Nagarakatte 93 Procedure Control Flow n Use stack to support procedure call and return Procedure call: call label Push return address on stack; Jump to label Return address value n Address of instruction beyond call n Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax l Return address = 0x8048553 Procedure return: n ret Pop address from stack; Jump to address
Image of page 93
%esp %eip %esp %eip 0x804854e 0x108 0x10c 0x110 0x104 0x804854e 0x8048553 123 Procedure Call Example 0x108 0x10c 0x110 123 0x108 call 8048b90 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 0x8048b90 0x104 %eip is program counter
Image of page 94
%esp %eip 0x104 %esp %eip 0x8048591 0x8048591 0x104 0x108 0x10c 0x110 0x8048553 123 Procedure Return Example 0x108 0x10c 0x110 123 ret 8048591: c3 ret 0x108 %eip is program counter 0x8048553 0x8048553
Image of page 95
Iclicker Quiz int **p, *q; int r; q = *p; Which assembly statement corresponds to the above C statement? Assume p is %eax and q is in %ebx A: movl %eax, %ebx B: movl (%ebx) , %eax C: movl %eax, (%ebx) D: movl (%eax), %ebx
Image of page 96
Iclicker Quiz int **p, *q; int r; q = *p; r = *q; Which assembly statement corresponds to the above C statements? Assume p is %eax and q is in %ebx, r is in %ecx A: movl (%eax), %ebx movl %ecx, %ebx B: movl (%eax) , %ebx movl (%ecx), %ebx C: movl (%eax), %ebx movl (%ebx), %ecx D: movl (%eax), %ebx movl %ecx, (%ebx)
Image of page 97
Iclicker Quiz .globl test .type test, @function test: pushl %ebp movl %esp, %ebp pushl %ebx movl 8(%ebp), %edx movl 12(%ebp), %ecx movl $1, %eax cmpl %ecx, %edx jge .L3 .L6: leal (%edx,%ecx), %ebx imull %ebx, %eax addl $1, %edx cmpl %edx, %ecx jg .L6 .L3: popl %ebx popl %ebp ret A: Function has only if then else statements B: Function has a loop C: Function takes 3 arguments D: Function is wrong
Image of page 98
Can you write the C code for this assembly? .globl test .type test, @function test: pushl %ebp movl %esp, %ebp pushl %ebx movl 8(%ebp), %edx movl 12(%ebp), %ecx movl $1, %eax cmpl %ecx, %edx jge .L3 .L6: leal (%edx,%ecx), %ebx imull %ebx, %eax addl $1, %edx cmpl %edx, %ecx jg .L6 .L3: popl %ebx popl %ebp ret What does this function do? What is the C code?
Image of page 99
Rutgers University Santosh Nagarakatte 100 Stack-Based Languages Languages that Support Recursion n e.g., C, Pascal, Java n Code must be “ Reentrant l Multiple simultaneous instantiations of single procedure n Need some place to store state of each instantiation l Arguments, local variables, return pointer Stack Discipline n State for given procedure needed for limited time l From when called to when return n Callee returns before caller does Stack Allocated in Frames (Activation records) n state for single procedure instantiation
Image of page 100
Rutgers University Santosh Nagarakatte 101 Call Chain Example Code Structure yoo(…) { who(); } who(…) { • • • amI(); • • •
Image of page 101
Image of page 102

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture