roadmap
play

Roadmap Memory & data Section 5: Procedures & Stacks - PowerPoint PPT Presentation

University of Washington University of Washington Roadmap Memory & data Section 5: Procedures & Stacks Integers & floats Machine code & C C: Java: x86 assembly Stacks in memory and stack operations Car c = new Car();


  1. University of Washington University of Washington Roadmap Memory & data Section 5: Procedures & Stacks Integers & floats Machine code & C C: Java: x86 assembly � Stacks in memory and stack operations Car c = new Car(); car *c = malloc(sizeof(car)); Procedures & stacks c->miles = 100; c.setMiles(100); Arrays & structs � The stack used to keep track of procedure calls c.setGals(17); c->gals = 17; Memory & caches float mpg = get_mpg(c); float mpg = � Return addresses and return values Processes free(c); c.getMPG(); Virtual memory � Stack-based languages Memory allocation Assembly get_mpg: Java vs. C pushq %rbp � The Linux stack frame language: movq %rsp, %rbp ... � Passing arguments on the stack popq %rbp ret � Allocating local variables on the stack OS: Machine � Register-saving conventions 0111010000011000 100011010000010000000010 code: � Procedures and stacks on x64 architecture 1000100111000010 110000011111101000011111 Computer system: Procedures and Stacks Procedure Calls University of Washington University of Washington Memory Layout Memory Layout 2 N -1 local variables; Managed “automatically” Stack Stack procedure context writable; not executable (by compiler) Dynamic Data Dynamic Data variables allocated with writable; not executable new or malloc Managed by programmer (Heap) (Heap) static variables Static Data Static Data writable; not executable Initialized when process starts (including global variables (C)) Literals Literals literals (e.g., “example”) Read-only; not executable Initialized when process starts Instructions Instructions Initialized when process starts Read-only; executable 0 Procedures and Stacks Procedures and Stacks

  2. University of Washington University of Washington IA32 Call Stack IA32 Call Stack: Push Stack “Bottom” Stack “Bottom” � Region of memory managed � pushl Src with a stack “discipline” Increasing � Grows toward lower addresses Addresses Increasing � Customarily shown “upside-down” Addresses � Register %esp contains lowest stack address Stack Grows = address of “top” element Down Stack Grows Stack Pointer: %esp Down Stack Pointer: %esp Stack “Top” Stack “Top” Procedures and Stacks Procedures and Stacks University of Washington University of Washington IA32 Call Stack: Push IA32 Call Stack: Pop Stack “Bottom” Stack “Bottom” � pushl Src � popl Dest � Fetch value from Src Increasing Increasing � Decrement %esp by 4 (why 4?) Addresses Addresses � Store value at address given by %esp Stack Grows Stack Grows Down Down Stack Pointer: %esp -4 Stack Pointer: %esp Stack “Top” Stack “Top” Procedures and Stacks Procedures and Stacks

  3. University of Washington University of Washington IA32 Call Stack: Pop Section 5: Procedures & Stacks Stack “Bottom” � Stacks in memory and stack operations � popl Dest � The stack used to keep track of procedure calls � Load value from address %esp Increasing � Write value to Dest � Return addresses and return values Addresses � Increment %esp by 4 � Stack-based languages � Item still remains, we are just not � The Linux stack frame referencing it any more � Passing arguments on the stack � Not really moving out of memory � Allocating local variables on the stack Stack Grows Stack Pointer: %esp � Register-saving conventions Down +4 � Procedures and stacks on x64 architecture Stack “Top” Procedures and Stacks Procedure Calls University of Washington University of Washington Procedure Call Overview Procedure Call Overview Caller Caller … … Callee <set up args> <save regs> Callee call <set up args> <create local vars> <clean up args> call <save regs> … <find return val> <clean up args> <create local vars> <set up return val> … <restore regs> … <destroy local vars> <find return val> <set up return val> return … <destroy local vars> <restore regs> return � Callee must know where to find args � Callee must know where to find “return address” � The convention of where to leave/find things is called the � Caller must know where to find return val procedure call linkage � Details vary between systems � Caller and Callee run on same CPU → use the same registers � We will see the convention for IA32/Linux in detail � Caller might need to save registers that Callee might use � What could happen if our program didn’t follow these conventions? � Callee might need to save registers that Caller has used Procedure Calls Procedure Calls

  4. University of Washington University of Washington Procedure Control Flow Procedure Control Flow � Use stack to support procedure call and return � Use stack to support procedure call and return � Procedure call: call label � Procedure call: call label � Push return address on stack � Push return address on stack � Jump to label � Jump to label � Return address: � Address of instruction after call � Example from disassembly: 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax � Return address = 0x8048553 � Procedure return: ret � Pop return address from stack � Jump to address Procedure Calls Procedure Calls University of Washington University of Washington Procedure Call Example Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 8048553: 50 pushl %eax call 8048b90 call 8048b90 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x108 123 0x108 123 0x108 123 0x104 %esp 0x108 %esp 0x108 %esp 0x108 %eip 0x804854e %eip 0x804854e %eip 0x804854e %eip: program counter %eip: program counter Procedure Calls Procedure Calls

  5. University of Washington University of Washington Procedure Call Example Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 8048553: 50 pushl %eax call 8048b90 call 8048b90 0x110 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x10c 0x108 123 0x108 123 0x108 123 0x108 123 0x104 0x104 0x8048553 %esp 0x108 %esp 0x108 %esp 0x108 %esp 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x8048553 %eip 0x804854e %eip 0x804854e 0x8048553 %eip: program counter %eip: program counter Procedure Calls Procedure Calls University of Washington University of Washington Procedure Call Example Procedure Return Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048591: c3 ret 8048553: 50 pushl %eax ret call 8048b90 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x108 123 0x108 123 0x108 123 0x104 0x8048553 0x104 0x8048553 %esp 0x104 %esp 0x108 %esp 0x108 0x104 %eip 0x8048591 %eip 0x804854e %eip 0x8048553 + 0x000063d 0x8048b90 %eip: program counter %eip: program counter Procedure Calls Procedure Calls

  6. University of Washington University of Washington Procedure Return Example Procedure Return Example 8048591: c3 ret 8048591: c3 ret ret ret 0x110 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x10c 0x108 123 0x108 123 0x108 123 0x108 123 0x104 0x8048553 0x8048553 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x104 %esp 0x104 %esp 0x104 %eip 0x8048591 %eip 0x8048591 %eip 0x8048591 %eip 0x8048591 0x8048553 %eip: program counter %eip: program counter Procedure Calls Procedure Calls University of Washington University of Washington Procedure Return Example Return Values 8048591: c3 ret � By convention, values returned by procedures are placed in the %eax register ret � Choice of %eax is arbitrary, could have easily been a different register � Caller must make sure to save that register before calling a 0x110 0x110 callee that returns a value � Part of register-saving convention we’ll see later 0x10c 0x10c 0x108 123 0x108 123 � Callee placed return value (any type that can fit in 4 bytes – 0x104 0x8048553 0x8048553 integer, float, pointer, etc.) into the %eax register � For return values greater than 4 bytes, best to return a pointer to them %esp 0x104 %esp 0x108 0x104 � Upon return, caller finds the return value in the %eax register %eip 0x8048591 %eip 0x8048591 0x8048553 %eip: program counter Procedure Calls Procedure Calls

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