1 Section 5: Procedures & StacksStacks in memory and stack operations The stack used to keep track of procedure calls Return addresses and return values Stack-based languages The Linux stack frame Passing arguments on the stack Allocating local variables on the stack Register-saving conventions Procedures and stacks on x64 architecture Procedure Calls
2 Procedure Call OverviewCaller …
3 Procedure Call OverviewCaller …
4 Procedure Control FlowUse stack to support procedure call and return Procedure call: call label Push return address on stack Jump to label Procedure Calls
5 Procedure Control FlowUse stack to support procedure call and return Procedure call: call label Push return address on stack Jump to label Return address: Address of instruction after call Example from disassembly: 804854e: e8 3d call b90 : pushl %eax Return address = 0x Procedure return: ret Pop return address from stack Jump to address Procedure Calls
6 Procedure Call Example804854e: e8 3d call b90 : pushl %eax call b90 0x110 0x10c 0x108 123 %esp 0x108 %eip 0x804854e %eip: program counter Procedure Calls
7 Procedure Call Example804854e: e8 3d call b90 : pushl %eax call b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 %esp 0x108 %esp 0x108 %eip 0x804854e %eip 0x804854e %eip: program counter Procedure Calls
8 Procedure Call Example804854e: e8 3d call b90 : pushl %eax call b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 %esp 0x108 %esp 0x108 %eip 0x804854e %eip 0x804854e 0x %eip: program counter Procedure Calls
9 Procedure Call Example804854e: e8 3d call b90 : pushl %eax call b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x %esp 0x108 %esp 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x %eip: program counter Procedure Calls
10 Procedure Call Example804854e: e8 3d call b90 : pushl %eax call b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x %esp 0x108 %esp 0x108 0x104 %eip 0x804854e %eip 0x + 0x000063d 0x8048b90 %eip: program counter Procedure Calls
11 Procedure Return Example: c ret ret 0x110 0x10c 0x108 123 0x104 0x %esp 0x104 %eip 0x %eip: program counter Procedure Calls
12 Procedure Return Example: c ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x 0x %esp 0x104 %esp 0x104 %eip 0x %eip 0x %eip: program counter Procedure Calls
13 Procedure Return Example: c ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x 0x %esp 0x104 %esp 0x104 %eip 0x %eip 0x 0x %eip: program counter Procedure Calls
14 Procedure Return Example: c ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x 0x %esp 0x104 %esp 0x104 0x108 %eip 0x %eip 0x 0x %eip: program counter Procedure Calls
15 Return Values By convention, values returned by procedures are placed in the %eax register Choice of %eax is arbitrary, could have easily been a different register Caller must make sure to save that register before calling a callee that returns a value Part of register-saving convention we’ll see later Callee placed return value (any type that can fit in 4 bytes – integer, float, pointer, etc.) into the %eax register For return values greater than 4 bytes, best to return a pointer to them Upon return, caller finds the return value in the %eax register Procedure Calls