the stack
play

The Stack pushl * does %esp <- %esp - 4 movl *, (%esp) popl * - PowerPoint PPT Presentation

Function Implementation (x86-specific) double words are pushed and popped addr 0 dedicated register %esp contains address of item currently at top of stack (TOS) %esp The Stack pushl * does %esp <- %esp - 4 movl *, (%esp) popl *


  1. Function Implementation (x86-specific) double words are pushed and popped addr 0 dedicated register %esp contains address of item currently at top of stack (TOS) %esp The Stack

  2. pushl * does %esp <- %esp - 4 movl *, (%esp) popl * movl (%esp), * does %esp <- %esp + 4 The Stack

  3. push $6 push $-1 push $3 addr 0 %esp The Stack

  4. addr 0 code Layout of segments in data memory heap stack

  5. What we need to know how to do. . . (what the compiler must be able to implement) 1. call 2. return 3. AR and local variables 4. return value 5. parameters

  6. 1. call ● remember the return address ● go to fcn this is such a common operation that the addr 0 x86 architecture supports it with a single instruction call fcn does the equivalent of PC push %eip jmp fcn %esp

  7. 2. return use the return address pushed onto the stack ret addr 0 does the equivalent of popl %eip ra %esp

  8. sample code to show only call and return void c() { c: ret } void b() { b: addr 0 c(); call c } L3: ret void a() { a: b(); call b } L2: ret main() { main: a(); call a } L1: %esp

  9. 3. incorporate AR For example, assume we need AR space for 3 ints. gcc on x86 allocates AR space in multiples of 16 bytes. addr 0 %esp Before fcn After fcn starts, but prologue after the call instruction prev %ebp %ebp ra %esp %ebp

  10. prologue code pushl %ebp movl %esp, %ebp subl $16, %esp addr 0 %esp Before fcn After fcn starts, but prologue after the call instruction prev %ebp %ebp ra %esp %ebp

  11. epilogue code leave does movl %ebp, %esp popl %ebp ret does popl %eip addr 0 %esp After epilogue Before prev %ebp %ebp epilogue ra %esp %ebp

  12. Put local variables into AR: b: pushl %ebp prologue void b() { int x, y, z; movl %esp, %ebp x = 1; subl $16, %esp y = 2; movl $1, -12(%ebp) z = 3; movl $2, -8(%ebp) c(); movl $3, -4(%ebp) %esp } call c x leave epilogue y before ret Z epilogue prev %ebp %ebp ra

  13. 4. return value On x86, return value goes in %eax (by convention) int b() { b: c(); call c return 4; movl $4, %eax } leave ret

  14. 5. parameters No room in registers on the x86, so parameters go onto the stack. Caller allocates space and places copies (for call by value). Child retrieves and uses copies. main () { main: pushl %ebp movl %esp, %ebp a( 1, 2, 3 ); subl $12, %esp movl $1, (%esp) movl $2, 4(%esp) movl $3, 8(%esp) call a } leave ret

  15. addr 0 p1 main's p2 outgoing p3 parameters %ebp

  16. int b(int a1, int a2) b: pushl %ebp movl %esp, %ebp { movl 12(%ebp), %eax return (a1 + a2); movl 8(%ebp), %edx } add %edx, %eax popl %ebp ret a: pushl %ebp void a(int p1, int p2, movl %esp, %ebp int p3) { subl $8, %esp b(4,5); movl $4, (%esp) movl $5, 4(%esp) call b } leave ret

  17. addr 0 prev %ebp %ebp b's AR ra a1 a2 by convention, a's AR prev %ebp and to make the compiler's ra job easy, the first parameter p1 will always be at main's p2 8(%ebp) outgoing p3 parameters

  18. addr 0 %esp P1 out P2 out (parameter P3 out build . space) . current AR local variables callee-saved regs prev %ebp %ebp ra P1 in P1 incoming P2 in is always at . 8(%ebp) .

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