Title: Machine-Level Programming III: Procedures Jan 30, 2003
1Machine-Level Programming IIIProceduresJan 30,
2003
15-213The course that gives CMU its Zip!
- Topics
- IA32 stack discipline
- Register saving conventions
- Creating pointers to local variables
2IA32 Stack
Stack Bottom
- Region of memory managed with stack discipline
- Grows toward lower addresses
- Register esp indicates lowest stack address
- address of top element
Stack Grows Down
Stack Top
3IA32 Stack Pushing
- Pushing
- pushl Src
- Fetch operand at Src
- Decrement esp by 4
- Write operand at address given by esp
Stack Bottom
Stack Grows Down
-4
Contents of Src
Stack Top
4IA32 Stack Popping
- Popping
- popl Dest
- Read operand at address given by esp
- Increment esp by 4
- Write to Dest
Stack Bottom
Stack Grows Down
4
Goes to Dest
Stack Top
5Stack Operation Examples
pushl eax
popl edx
0x110
0x110
0x110
0x10c
0x10c
0x10c
0x108
123
0x108
123
0x108
123
0x104
0x104
213
213
eax
eax
eax
213
213
213
213
edx
edx
edx
555
555
555
esp
esp
esp
0x108
0x108
0x104
0x104
0x108
6Procedure Control Flow
- Use stack to support procedure call and return
- Procedure call
- call label Push return address on stack Jump to
label - Return address value
- Address of instruction beyond call
- Example from disassembly
- 804854e e8 3d 06 00 00 call 8048b90 ltmaingt
- 8048553 50 pushl eax
- Return address 0x8048553
- Procedure return
- ret Pop address from stack Jump to address
7Procedure Call Example
804854e e8 3d 06 00 00 call 8048b90
ltmaingt 8048553 50 pushl eax
0x110
0x10c
0x108
123
esp
0x108
eip
0x804854e
eip is the program counter
8Procedure Call Example
804854e e8 3d 06 00 00 call 8048b90
ltmaingt 8048553 50 pushl eax
0x110
0x10c
(bump up eip to next instruction)
0x108
123
esp
0x108
eip
0x804854e
eip is the program counter
9Procedure Call Example
804854e e8 3d 06 00 00 call 8048b90
ltmaingt 8048553 50 pushl eax
0x110
0x10c
(bump up eip to next instruction)
0x108
123
(save eip on stack)
esp
0x108
eip
0x8048553
eip is the program counter
10Procedure Call Example
804854e e8 3d 06 00 00 call 8048b90
ltmaingt 8048553 50 pushl eax
0x110
0x10c
(bump up eip to next instruction)
0x108
123
0x104
(save eip on stack)
esp
0x104
eip
0x8048553
0x8048553
eip is the program counter
11Procedure Call Example
804854e e8 3d 06 00 00 call 8048b90
ltmaingt 8048553 50 pushl eax
8048b90
0x110
0x10c
(bump up eip to next instruction)
0x108
123
0x8048553
0x104
(save eip on stack)
esp
0x104
0x8048553
eip
0x
(set eip to dest adr)
eip is the program counter
12Procedure Return Example
8048591 c3 ret
0x110
0x10c
0x108
123
0x8048553
0x104
esp
0x104
0x
0x8048591
eip
eip is the program counter
13Procedure Return Example
8048591 c3 ret
(incr eip)
0x110
0x10c
(pop top of stack to eip)
0x108
123
0x8048553
0x8048553
0x104
esp
0x104
0x8048591
eip
0x8048592
eip is the program counter
14Procedure Return Example
8048591 c3 ret
(incr eip)
0x110
0x10c
(pop top of stack to eip)
0x108
123
0x8048553
0x8048553
0x104
esp
0x108
0x8048591
eip
0x8048553
eip is the program counter
15Stack-Based Languages
- Languages that Support Recursion
- e.g., C, Pascal, Java
- Code must be Reentrant
- Multiple simultaneous instantiations of single
procedure - Need some place to store state of each
instantiation - Arguments
- Local variables
- Return pointer
- Stack Discipline
- Callee returns before caller does
- State for given procedure needed for limited time
- When?
- Stack Allocated in Frames
- state for single procedure instantiation
16Stack-Based Languages
- Languages that Support Recursion
- e.g., C, Pascal, Java
- Code must be Reentrant
- Multiple simultaneous instantiations of single
procedure - Need some place to store state of each
instantiation - Arguments
- Local variables
- Return pointer
- Stack Discipline
- Callee returns before caller does
- State for given procedure needed for limited time
- From when called to when return
- Stack Allocated in Frames
- state for single procedure instantiation
17Call Tree Example
Call Tree
yoo() who()
yoo
who() amI() amI()
who
amI
amI
amI() amI()
amI
amI
18Stack Frames
- Contents
- Local variables
- Return information
- Temporary space
- Management
- Space allocated when enter procedure
- Set-up code (prolog)
- Deallocated when return
- Finish code (epilog)
- Pointers
- Stack pointer esp indicates stack top
- Frame pointer ebp indicates base of current frame
yoo
who
amI
proc
Stack Top
19Stack Operation
yoo
Call Tree
yoo
20Stack Operation
yoo
Call Tree
yoo
who
who
21Stack Operation
yoo
Call Tree
yoo
who
who
amI
amI
22Stack Operation
yoo
Call Tree
who() amI() amI()
yoo
who
who
amI
amI
amI
amI
23Stack Operation
yoo
Call Tree
who() amI() amI()
yoo
amI() amI()
who
who
amI
amI
amI
amI
amI
amI
24Stack Operation
yoo
Call Tree
yoo() who()
who() amI() amI()
amI() amI()
amI() amI()
yoo
who
who
amI
amI
amI
amI
amI
25Stack Operation
yoo
Call Tree
yoo() who()
who() amI() amI()
amI() amI()
yoo
who
who
amI
amI
amI
amI
26Stack Operation
yoo
Call Tree
who() amI() amI()
yoo
who
who
amI
amI
amI
27Stack Operation
yoo
Call Tree
who() amI() amI()
amI() amI()
yoo
who
who
amI
amI
amI
amI
amI
28Stack Operation
yoo
Call Tree
who() amI() amI()
yoo
who
who
amI
amI
amI
amI
29Stack Operation
yoo
Call Tree
yoo
who
amI
amI
amI
amI
30IA32/Linux Stack Frame
- Current Stack Frame (Top to Bottom)
- Parameters for function about to call
- Argument build
- Local variables
- If cant keep in registers
- Saved register context
- Old frame pointer
- Caller Stack Frame
- Return address
- Pushed by call instruction
- Arguments for this call
Caller Frame
Arguments
Return Addr
Frame Pointer (ebp)
Old ebp
Saved Registers Local Variables
Argument Build
Stack Pointer (esp)
31Revisiting swap
Calling swap from call_swap
int zip1 15213 int zip2 91125 void
call_swap() swap(zip1, zip2)
call_swap pushl zip2 Global
Var pushl zip1 Global Var call swap
Resulting Stack
void swap(int xp, int yp) int t0 xp
int t1 yp xp t1 yp t0
zip2
zip1
Rtn adr
esp
32Revisiting swap
swap pushl ebp movl esp,ebp pushl
ebx movl 12(ebp),ecx movl
8(ebp),edx movl (ecx),eax movl
(edx),ebx movl eax,(edx) movl
ebx,(ecx) movl -4(ebp),ebx movl
ebp,esp popl ebp ret
Prolog
void swap(int xp, int yp) int t0 xp
int t1 yp xp t1 yp t0
Body
Epilog
33swap Prolog 1
Resulting Stack
Entering Stack
ebp
zip2
zip1
Rtn adr
esp
swap pushl ebp movl esp,ebp pushl ebx
34swap Prolog 2
Resulting Stack
Entering Stack
ebp
yp
zip2
xp
zip1
Rtn adr
Rtn adr
esp
ebp
Old ebp
swap pushl ebp movl esp,ebp pushl ebx
esp
35swap Prolog 3
Resulting Stack
Entering Stack
ebp
Why save ebx?
yp
zip2
xp
zip1
Rtn adr
Rtn adr
esp
ebp
Old ebp
swap pushl ebp movl esp,ebp pushl ebx
Old ebx
esp
36Effect of swap Prolog
Entering Stack
Resulting Stack
ebp
Offset (relative to ebp)
yp
12
zip2
xp
8
zip1
Rtn adr
4
Rtn adr
esp
ebp
Old ebp
0
Old ebx
esp
movl 12(ebp),ecx get yp movl 8(ebp), edx
get xp . . .
Body
37swap Finish 1
swaps Stack
Offset
Offset
yp
12
yp
12
xp
8
xp
8
Rtn adr
4
Rtn adr
4
ebp
Old ebp
0
ebp
Old ebp
0
Old ebx
esp
-4
Old ebx
esp
-4
movl -4(ebp),ebx movl ebp,esp popl
ebp ret
- Observation
- Saved restored register ebx
38swap Finish 2
swaps Stack
swaps Stack
Offset
Offset
yp
12
yp
12
xp
8
xp
8
Rtn adr
4
Rtn adr
4
ebp
Old ebp
0
ebp
Old ebp
0
Old ebx
esp
-4
esp
movl -4(ebp),ebx movl ebp,esp popl
ebp ret
39swap Finish 3
ebp
swaps Stack
swaps Stack
Offset
yp
yp
12
xp
xp
8
Rtn adr
Rtn adr
4
esp
Old ebp
0
ebp
esp
movl -4(ebp),ebx movl ebp,esp popl
ebp ret
40swap Finish 4
ebp
swaps Stack
ebp
Exiting Stack
Offset
yp
12
zip2
xp
8
zip1
esp
Rtn adr
4
esp
movl -4(ebp),ebx movl ebp,esp popl
ebp ret
- Observation
- Saved restored register ebx
- Didnt for eax, ecx, or edx
41Register Saving Conventions
- When procedure yoo calls who
- yoo is the caller, who is the callee
- Can Register be Used for Temporary Storage?
- Contents of register edx overwritten by who
yoo movl 15213, edx call who addl edx,
eax ret
who movl 8(ebp), edx addl 91125,
edx ret
42Register Saving Conventions
- When procedure yoo calls who
- yoo is the caller, who is the callee
- Can Register be Used for Temporary Storage?
- Conventions
- Caller Save
- Caller saves temporary in its frame before
calling - Callee Save
- Callee saves temporary in its frame before using
43IA32/Linux Register Usage
- Integer Registers
- Two have special uses
- ebp, esp
- Three managed as callee-save
- ebx, esi, edi
- Old values saved on stack prior to using
- Three managed as caller-save
- eax, edx, ecx
- Do what you please, but expect any callee to do
so, as well - Register eax also stores returned value
eax
Caller-Save Temporaries
edx
ecx
ebx
Callee-Save Temporaries
esi
edi
esp
Special
ebp
44Swap 1 mo time
callswap pushl ebp movl esp,ebp subl 24,e
sp movl 1,-8(ebp) addl -8,esp movl 2,-4(e
bp) leal -4(ebp),eax pushl eax leal -8(ebp)
,eax pushl eax call _swap movl -4(ebp),eax
addl -4,esp pushl eax movl -8(ebp),eax pu
shl eax pushl LC0 call _printf movl ebp,esp
popl ebp ret
Void callswap(void) int x 1 int y
2 swap(x, y) printf(d d, x, y)
void swap(int xp, int yp) int t0 xp
int t1 yp xp t1 yp t0
45Callswaps frame
callswap pushl ebp movl esp,ebp subl 24,e
sp movl 1,-8(ebp) addl -8,esp movl 2,-4(e
bp) leal -4(ebp),eax pushl eax leal -8(ebp)
,eax pushl eax call _swap movl -4(ebp),eax
addl -4,esp pushl eax movl -8(ebp),eax pu
shl eax pushl LC0 call _printf movl ebp,esp
popl ebp ret
Void callswap(void) int x 1 int y
2 swap(x, y) printf(d d, x, y)
46After swaps prolog
Callers ebp
y
x
Void callswap(void) int x 1 int y
2 swap(x, y) printf(d d, x, y)
y
void swap(int xp, int yp) int t0 xp
int t1 yp xp t1 yp t0
x
Swaps Frame
RetAdr
callers ebx
47Recursive Factorial
.globl rfact .type rfact,_at_function rfact pushl
ebp movl esp,ebp pushl ebx movl
8(ebp),ebx cmpl 1,ebx jle .L78 leal
-1(ebx),eax pushl eax call rfact imull
ebx,eax jmp .L79 .align 4 .L78 movl
1,eax .L79 movl -4(ebp),ebx movl
ebp,esp popl ebp ret
int rfact(int x) int rval if (x lt 1)
return 1 rval rfact(x-1) return rval
x
Where is X? Where is rval?
48Recursive Factorial
.globl rfact .type rfact,_at_function rfact pushl
ebp movl esp,ebp pushl ebx movl
8(ebp),ebx cmpl 1,ebx jle .L78 leal
-1(ebx),eax pushl eax call rfact imull
ebx,eax jmp .L79 .align 4 .L78 movl
1,eax .L79 movl -4(ebp),ebx movl
ebp,esp popl ebp ret
int rfact(int x) int rval if (x lt 1)
return 1 rval rfact(x-1) return rval
x
Prolog
Where is X? 8(ebp), ebx Where is rval? eax
Epilog
49Rfact Stack Prolog
pre ebp
ebp
Entering Stack
pre ebx
rfact pushl ebp movl esp,ebp pushl ebx
rfact pushl ebp movl esp,ebp pushl ebx
rfact pushl ebp movl esp,ebp pushl ebx
pre ebp
Caller
pre ebx
x
Can now see that argument, x, is at 8(ebp)
Rtn adr
Callee
50Rfact Body
movl 8(ebp),ebx ebx x cmpl 1,ebx
Compare x 1 jle .L78 If lt goto
Term leal -1(ebx),eax eax
x-1 pushl eax Push x-1 call rfact
rfact(x-1) imull ebx,eax rval
x jmp .L79 Goto done .L78
Term movl 1,eax return val 1 .L79
Done
int rfact(int x) int rval if (x lt 1)
return 1 rval rfact(x-1) return rval
x
- Registers
- ebx Stored value of x
- eax
- Temporary value of x-1
- Returned value from rfact(x-1)
- Returned value from this call
51Rfact Body
movl 8(ebp),ebx ebx x cmpl 1,ebx
Compare x 1 jle .L78 If lt goto
Term leal -1(ebx),eax eax
x-1 pushl eax Push x-1 call rfact
rfact(x-1) imull ebx,eax rval
x jmp .L79 Goto done .L78
Term movl 1,eax return val 1 .L79
Done
int rfact(int x) int rval if (x lt 1)
return 1 rval rfact(x-1) return rval
x
- Registers
- ebx Stored value of x
- eax
- Temporary value of x-1
- Returned value from rfact(x-1)
- Returned value from this call
52Rfact Body
movl 8(ebp),ebx ebx x cmpl 1,ebx
Compare x 1 jle .L78 If lt goto
Term leal -1(ebx),eax eax
x-1 pushl eax Push x-1 call rfact
rfact(x-1) imull ebx,eax rval
x jmp .L79 Goto done .L78
Term movl 1,eax return val 1 .L79
Done
int rfact(int x) int rval if (x lt 1)
return 1 rval rfact(x-1) return rval
x
- Registers
- ebx Stored value of x
- eax
- Temporary value of x-1
- Returned value from rfact(x-1)
- Returned value from this call
53Rfact Body
movl 8(ebp),ebx ebx x cmpl 1,ebx
Compare x 1 jle .L78 If lt goto
Term leal -1(ebx),eax eax
x-1 pushl eax Push x-1 call rfact
rfact(x-1) imull ebx,eax rval
x jmp .L79 Goto done .L78
Term movl 1,eax return val 1 .L79
Done
Recursion
int rfact(int x) int rval if (x lt 1)
return 1 rval rfact(x-1) return rval
x
- Registers
- ebx Stored value of x
- eax
- Temporary value of x-1
- Returned value from rfact(x-1)
- Returned value from this call
54Rfact Body
movl 8(ebp),ebx ebx x cmpl 1,ebx
Compare x 1 jle .L78 If lt goto
Term leal -1(ebx),eax eax
x-1 pushl eax Push x-1 call rfact
rfact(x-1) imull ebx,eax rval
x jmp .L79 Goto done .L78
Term movl 1,eax return val 1 .L79
Done
Recursion
int rfact(int x) int rval if (x lt 1)
return 1 rval rfact(x-1) return rval
x
- Registers
- ebx Stored value of x
- eax
- Temporary value of x-1
- Returned value from rfact(x-1)
- Returned value from this call
55Recursive Factorial
.globl rfact .type rfact,_at_function rfact pushl
ebp movl esp,ebp pushl ebx movl
8(ebp),ebx cmpl 1,ebx jle .L78 leal
-1(ebx),eax pushl eax call rfact imull
ebx,eax jmp .L79 .align 4 .L78 movl
1,eax .L79 movl -4(ebp),ebx movl
ebp,esp popl ebp ret
int rfact(int x) int rval if (x lt 1)
return 1 rval rfact(x-1) return rval
x
- Registers
- eax used without first saving
- ebx used, but save at beginning restore at end
56Rfact Recursion
x
Rtn adr
ebp
Old ebp
Old ebx
esp
eax
x
ebx
leal -1(ebx),eax eax x-1 pushl eax
Push x-1 call rfact rfact(x-1)
57After Rfact Recursion?
x
Rtn adr
ebp
Old ebp
Old ebx
esp
eax
x
ebx
- ebx has value of x
- eax has value of (x-1)!
leal -1(ebx),eax eax x-1 pushl eax
Push x-1 call rfact rfact(x-1) imull ebx,eax
rval x
58Rfact Result
Return from Call
x
x
Rtn adr
Rtn adr
ebp
ebp
Old ebp
Old ebp
Old ebx
Old ebx
x-1
x-1
esp
esp
(x-1)!
eax
(x-1)!
eax
(x-1)!
x
ebx
x
ebx
Assume that rfact(x-1) returns (x-1)! in register
eax
59Rfact Epilog
movl -4(ebp),ebx movl ebp,esp popl
ebp ret
movl -4(ebp),ebx movl ebp,esp popl
ebp ret
movl -4(ebp),ebx movl ebp,esp popl
ebp ret
pre ebp
pre ebx
x
8
Rtn adr
4
ebp
Old ebp
0
Old ebx
Old ebx
-4
esp
x-1
-8
x!
eax
x
ebx
60Pointer Code
Recursive Procedure
Top-Level Call
void s_helper (int x, int accum) if (x lt
1) return else int z accum x
accum z s_helper (x-1,accum)
int sfact(int x) int val 1 s_helper(x,
val) return val
- Pass pointer to update location
61Creating Initializing Pointer
Initial part of sfact
_sfact pushl ebp Save ebp movl esp,ebp
Set ebp subl 16,esp Add 16 bytes movl
8(ebp),edx edx x movl 1,-4(ebp) val 1
_sfact pushl ebp Save ebp movl esp,ebp
Set ebp subl 16,esp Add 16 bytes movl
8(ebp),edx edx x movl 1,-4(ebp) val 1
_sfact pushl ebp Save ebp movl esp,ebp
Set ebp subl 16,esp Add 16 bytes movl
8(ebp),edx edx x movl 1,-4(ebp) val 1
_sfact pushl ebp Save ebp movl esp,ebp
Set ebp subl 16,esp Add 16 bytes movl
8(ebp),edx edx x movl 1,-4(ebp) val 1
x
8
Rtn adr
4
Old ebp
0
-4
-8
- Using Stack for Local Variable
- Variable val must be stored on stack
- Need to create pointer to it
- Compute pointer as -4(ebp)
- Push on stack as second argument
-12
-16
int sfact(int x) int val 1 s_helper(x,
val) return val
62Passing Pointer
Calling s_helper from sfact
Stack at time of call
x
8
leal -4(ebp),eax Compute val pushl eax
Push on stack pushl edx Push x call
s_helper call movl -4(ebp),eax Return
val Finish
leal -4(ebp),eax Compute val pushl eax
Push on stack pushl edx Push x call
s_helper call movl -4(ebp),eax Return
val Finish
leal -4(ebp),eax Compute val pushl eax
Push on stack pushl edx Push x call
s_helper call movl -4(ebp),eax Return
val Finish
Rtn adr
4
ebp
Old ebp
0
val 1
-4
val x!
Unused
-8
-12
int sfact(int x) int val 1 s_helper(x,
val) return val
-16
63Using Pointer
void s_helper (int x, int accum)
int z accum x accum z
accumx
accumx
x
ecx
movl ecx,eax z x imull
(edx),eax z accum movl eax,(edx)
accum z
- Register ecx holds x
- Register edx holds pointer to accum
- Use access (edx) to reference memory
64Summary
- The Stack Makes Recursion Work
- Private storage for each instance of procedure
call - Instantiations dont clobber each other
- Addressing of locals arguments can be relative
to stack positions - Can be managed by stack discipline
- Procedures return in inverse order of calls
- IA32 Procedures Combination of Instructions
Conventions - Call / Ret instructions
- Register usage conventions
- Caller / Callee save
- ebp and esp
- Stack frame organization conventions