Lecture10 - Assembly Programming II Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Stack Region of

Lecture10 - Assembly Programming II Topics IA32 stack...

This preview shows page 1 - 13 out of 65 pages.

Assembly Programming II Topics square6 IA32 stack discipline square6 Register saving conventions square6 Creating pointers to local variables
Image of page 1
IA32 Stack square6 Region of memory managed with stack discipline square6 Grows toward lower addresses square6 Register %esp indicates lowest stack address circle6 address of top element Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom”
Image of page 2
IA32 Stack Pushing Pushing square6 pushl Src square6 Fetch operand at Src square6 Decrement %esp by 4 square6 Write operand at address given by %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” Stack Pointer %esp -4
Image of page 3
IA32 Stack Popping Popping square6 popl Dest square6 Read operand at address given by %esp square6 Increment %esp by 4 square6 Write to Dest Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” +4
Image of page 4
%esp %eax %edx %esp %eax %edx %esp %eax %edx 0x104 555 0x108 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 0x104 213 popl %edx 0x108 213
Image of page 5
Procedure Control Flow square6 Use stack to support procedure call and return Procedure call: call label Push return address on stack; Jump to label Return address value square6 Address of instruction beyond call square6 Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax circle6 Return address = 0x8048553 Procedure return: square6 ret Pop address from stack; Jump to address
Image of page 6
%esp %eip %esp %eip 0x804854e 0x108 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 7
%esp %eip 0x104 %esp %eip 0x8048591 0x8048591 0x104 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 8
Stack-Based Languages Languages that Support Recursion square6 e.g., C, Pascal, Java square6 Code must be “ Reentrant circle6 Multiple simultaneous instantiations of single procedure square6 Need some place to store state of each instantiation circle6 Arguments circle6 Local variables circle6 Return pointer Stack Discipline square6 State for given procedure needed for limited time circle6 From when called to when return square6 Callee returns before caller does Stack Allocated in Frames square6 state for single procedure instantiation
Image of page 9
Call Chain Example Code Structure yoo(…) { who(); } who(…) { • • • amI(); • • • amI(); • • • } amI(…) { amI(); } yoo who amI amI amI Call Chain square6 Procedure amI recursive amI
Image of page 10
Stack Pointer %esp yoo who proc Frame Pointer %ebp Stack “Top” Stack Frames Contents square6 Local variables square6 Return information square6 Temporary space Management square6 Space allocated when enter procedure circle6 “Set-up” code square6 Deallocated when return circle6 “Finish” code Pointers square6 Stack pointer %esp indicates stack top square6 Frame pointer %ebp indicates start of current frame amI
Image of page 11
Stack Pointer %esp yoo Frame Pointer %ebp Stack Operation yoo Call Chain yoo(…) { who(); }
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 65 pages?

  • Fall '08
  • Staff
  • Array, Call stack, EBP, Yoo

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture