machine-level programming iii: procedures jan 30, 2003 topics ia32 stack discipline register saving...
Post on 21-Dec-2015
216 views
TRANSCRIPT
![Page 1: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/1.jpg)
Machine-Level Programming III:Procedures
Jan 30, 2003
Machine-Level Programming III:Procedures
Jan 30, 2003
TopicsTopics IA32 stack discipline Register saving
conventions Creating pointers to local
variables
15-213“The course that gives CMU its Zip!”
![Page 2: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/2.jpg)
– 2 – 15-213, S’03
IA32 StackIA32 Stack Region of memory managed
with stack discipline Grows toward lower
addresses Register %esp indicates
lowest stack addressaddress of top element
StackPointer%esp
Stack GrowsDown
IncreasingAddresses
Stack “Top”
Stack “Bottom”
![Page 3: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/3.jpg)
– 3 – 15-213, S’03
IA32 Stack PushingIA32 Stack PushingPushingPushing
pushl Src Fetch operand at Src Decrement %esp by 4 Write operand at address
given by %esp
Stack GrowsDown
IncreasingAddresses
Stack “Top”
Stack “Bottom”
Contents of Src
StackPointer%esp
-4
![Page 4: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/4.jpg)
– 4 – 15-213, S’03
IA32 Stack PoppingIA32 Stack PoppingPoppingPopping
popl Dest Read operand at address
given by %esp Increment %esp by 4 Write to Dest
StackPointer%esp
Stack GrowsDown
IncreasingAddresses
Stack “Top”
Stack “Bottom”
+4Goes to Dest
![Page 5: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/5.jpg)
– 5 – 15-213, S’03
%esp
%eax
%edx
%esp
%eax
%edx
%esp
%eax
%edx
0x104
555
0x108
0x108
0x10c
0x110
0x104
555
213
123
Stack Operation ExamplesStack Operation Examples
0x108
0x10c
0x110
555
213
123
0x108 0x104
pushl %eax
0x108
0x10c
0x110
213
123
0x104
popl %edx
0x108
213
213
213
![Page 6: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/6.jpg)
– 6 – 15-213, S’03
Procedure Control FlowProcedure Control Flow Use stack to support procedure call and return
Procedure call:Procedure call:call label Push return address on stack; Jump to label
Return address valueReturn address value Address of instruction beyond call Example from disassembly
804854e: e8 3d 06 00 00 call 8048b90 <main>
8048553: 50 pushl %eaxReturn address = 0x8048553
Procedure return:Procedure return: ret Pop address from stack; Jump to address
![Page 7: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/7.jpg)
– 7 – 15-213, S’03
%esp
%eip 0x804854e
Procedure Call ExampleProcedure Call Example
0x108
0x10c
0x110
123
0x108
804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax
%eip is the program counter
![Page 8: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/8.jpg)
– 8 – 15-213, S’03
%esp
%eip 0x804854e
Procedure Call ExampleProcedure Call Example
0x108
0x10c
0x110
123
0x108
804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax
%eip is the program counter
(bump up %eip to next
instruction)
![Page 9: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/9.jpg)
– 9 – 15-213, S’03
%esp
%eip 0x8048553
Procedure Call ExampleProcedure Call Example
0x108
0x10c
0x110
123
0x108
804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax
%eip is the program counter
(bump up %eip to next
instruction)
(save %eip on stack)
![Page 10: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/10.jpg)
– 10 – 15-213, S’03
%esp
%eip 0x8048553
Procedure Call ExampleProcedure Call Example
0x108
0x10c
0x110
123
0x104
804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax
%eip is the program counter
(bump up %eip to next
instruction)
(save %eip on stack)
0x104
0x8048553
![Page 11: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/11.jpg)
– 11 – 15-213, S’03
0x8048553
%esp
%eip 0x
Procedure Call ExampleProcedure Call Example
0x108
0x10c
0x110
123
0x104
804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax
%eip is the program counter
(bump up %eip to next
instruction)
(save %eip on stack)
0x104
(set %eip to dest adr)
8048b90
0x8048553
![Page 12: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/12.jpg)
– 12 – 15-213, S’03
0x
Procedure Return ExampleProcedure Return Example8048591: c3 ret
0x8048591
%esp
%eip
0x108
0x10c
0x110
123
0x104
%eip is the program counter
0x104 0x8048553
![Page 13: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/13.jpg)
– 13 – 15-213, S’03
0x80485910x8048592
Procedure Return ExampleProcedure Return Example8048591: c3 ret
%esp
%eip
0x108
0x10c
0x110
123
0x104
%eip is the program counter
0x104
(incr %eip)
(pop top of stack to %eip)
0x80485530x8048553
![Page 14: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/14.jpg)
– 14 – 15-213, S’03
0x8048591 0x8048553
Procedure Return ExampleProcedure Return Example8048591: c3 ret
%esp
%eip
0x108
0x10c
0x110
123
0x108
%eip is the program counter
0x104
(incr %eip)
(pop top of stack to %eip)0x80485530x8048553
![Page 15: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/15.jpg)
– 15 – 15-213, S’03
Stack-Based LanguagesStack-Based LanguagesLanguages that Support RecursionLanguages 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 instantiationArgumentsLocal variablesReturn pointer
Stack DisciplineStack Discipline Callee returns before caller does State for given procedure needed for limited time
When?
Stack Allocated in Stack Allocated in FramesFrames state for single procedure instantiation
![Page 16: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/16.jpg)
– 16 – 15-213, S’03
Stack-Based LanguagesStack-Based LanguagesLanguages that Support RecursionLanguages 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 instantiationArgumentsLocal variablesReturn pointer
Stack DisciplineStack Discipline Callee returns before caller does State for given procedure needed for limited time
From when called to when return
Stack Allocated in Stack Allocated in FramesFrames state for single procedure instantiation
![Page 17: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/17.jpg)
– 17 – 15-213, S’03
Call Tree ExampleCall Tree ExampleCode StructureCode Structure
yoo(…){
••who();••
}
who(…){
• • •amI();• • •amI();• • •
}
amI(…){
••amI();••
}
yoo
who
amI
amI
amI
Call Tree
Procedure amI recursive
amI
![Page 18: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/18.jpg)
– 18 – 15-213, S’03
StackPointer%esp
yoo
who
proc
FramePointer%ebp
Stack“Top”
Stack FramesStack FramesContentsContents
Local variables Return information Temporary space
ManagementManagement Space allocated when enter
procedure“Set-up” code (prolog)
Deallocated when return“Finish” code (epilog)
PointersPointers Stack pointer %esp indicates stack
top Frame pointer %ebp indicates base
of current frame
amI
![Page 19: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/19.jpg)
– 19 – 15-213, S’03
StackPointer%esp
yoo
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
Call Treeyoo(…){
••who();••
}
![Page 20: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/20.jpg)
– 20 – 15-213, S’03
StackPointer%esp
yoo
who
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
who
Call Treeyoo(…){
••who();••
}
who(…){
• • •amI();• • •amI();• • •
}
![Page 21: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/21.jpg)
– 21 – 15-213, S’03
StackPointer%esp
yoo
who
amI
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
who
amI
Call Treeyoo(…){
••who();••
}
who(…){
• • •amI();• • •amI();• • •
}
amI(…){
••amI();••
}
![Page 22: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/22.jpg)
– 22 – 15-213, S’03
StackPointer%esp
yoo
who
amI
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
who
amI
Call Tree
amIamI
yoo(…){
••who();••
}
who(…){
• • •amI();• • •amI();• • •
}
amI(…){
••amI();••
}
amI(…){
••amI();••
}
![Page 23: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/23.jpg)
– 23 – 15-213, S’03
StackPointer%esp
yoo
who
amI
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
who
amI
Call Tree
amIamI
amI
amI
yoo(…){
••who();••
}
who(…){
• • •amI();• • •amI();• • •
}
amI(…){
••amI();••
}
amI(…){
••amI();••
}
amI(…){
••amI();••
}
![Page 24: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/24.jpg)
– 24 – 15-213, S’03
StackPointer%esp
yoo
who
amI
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
who
amI
Call Tree
amIamI
amI
yoo(…){
••who();••
}
who(…){
• • •amI();• • •amI();• • •
}
amI(…){
••amI();••
}
amI(…){
••amI();••
}
![Page 25: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/25.jpg)
– 25 – 15-213, S’03
StackPointer%esp
yoo
who
amI
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
who
amI
Call Tree
amI
amI
yoo(…){
••who();••
}
who(…){
• • •amI();• • •amI();• • •
}
amI(…){
••amI();••
}
![Page 26: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/26.jpg)
– 26 – 15-213, S’03
StackPointer%esp
yoo
who
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
who
Call Tree
amI
amI
amI
yoo(…){
••who();••
}
who(…){
• • •amI();• • •amI();• • •
}
![Page 27: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/27.jpg)
– 27 – 15-213, S’03
StackPointer%esp
yoo
who
amI
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
who
Call Tree
amI
amI
amI
amI
yoo(…){
••who();••
}
who(…){
• • •amI();• • •amI();• • •
}
amI(…){
••amI();••
}
![Page 28: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/28.jpg)
– 28 – 15-213, S’03
StackPointer%esp
yoo
who
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
who
Call Tree
amI
amI
amI
amI
yoo(…){
••who();••
}
who(…){
• • •amI();• • •amI();• • •
}
![Page 29: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/29.jpg)
– 29 – 15-213, S’03
StackPointer%esp
yoo
•••
FramePointer%ebp
Stack OperationStack Operation
yoo
who
Call Tree
amI
amI
amI
amI
yoo(…){
••who();••
}
![Page 30: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/30.jpg)
– 30 – 15-213, S’03
IA32/Linux Stack FrameIA32/Linux Stack FrameCurrent Stack Frame (“Top” to Bottom)Current Stack Frame (“Top” to Bottom)
Parameters for function about to call“Argument build”
Local variables If can’t keep in registers
Saved register context Old frame pointer
Caller Stack FrameCaller Stack Frame Return address
Pushed by call instruction
Arguments for this call
Stack Pointer(%esp)
Frame Pointer(%ebp)
Return Addr
SavedRegisters
+Local
Variables
ArgumentBuild
Old %ebp
Arguments
CallerFrame
![Page 31: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/31.jpg)
– 31 – 15-213, S’03
Revisiting swapRevisiting swap
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
int zip1 = 15213;int zip2 = 91125;
void call_swap(){ swap(&zip1, &zip2);}
call_swap:• • •pushl $zip2 # Global Varpushl $zip1 # Global Varcall swap• • •
&zip2
&zip1
Rtn adr %esp
ResultingStack
•••
Calling swap from call_swap
![Page 32: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/32.jpg)
– 32 – 15-213, S’03
Revisiting swapRevisiting swap
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
swap:pushl %ebpmovl %esp,%ebppushl %ebx
movl 12(%ebp),%ecxmovl 8(%ebp),%edxmovl (%ecx),%eaxmovl (%edx),%ebxmovl %eax,(%edx)movl %ebx,(%ecx)
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
Body
Prolog
Epilog
![Page 33: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/33.jpg)
– 33 – 15-213, S’03
swap Prolog #1swap Prolog #1
swap:pushl %ebpmovl %esp,%ebppushl %ebx
ResultingStack
&zip2
&zip1
Rtn adr %esp
EnteringStack
•••
%ebp
yp
xp
Rtn adr
Old %ebp
%ebp•••
%esp
![Page 34: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/34.jpg)
– 34 – 15-213, S’03
swap:pushl %ebpmovl %esp,%ebppushl %ebx
swap Prolog #2swap Prolog #2
yp
xp
Rtn adr
Old %ebp %ebp
ResultingStack
•••
&zip2
&zip1
Rtn adr %esp
EnteringStack
•••
%ebp
%esp
![Page 35: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/35.jpg)
– 35 – 15-213, S’03
swap:pushl %ebpmovl %esp,%ebppushl %ebx
swap Prolog #3swap Prolog #3
yp
xp
Rtn adr
Old %ebp %ebp
ResultingStack
•••
&zip2
&zip1
Rtn adr %esp
EnteringStack
•••
%ebp
Old %ebx %esp
Why save %ebx?
![Page 36: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/36.jpg)
– 36 – 15-213, S’03
Effect of swap PrologEffect of swap Prolog
yp
xp
Rtn adr
Old %ebp %ebp 0
4
8
12
Offset(relative to %ebp)
ResultingStack
•••
&zip2
&zip1
Rtn adr %esp
EnteringStack
•••
%ebp
Old %ebx %esp
movl 12(%ebp),%ecx # get ypmovl 8(%ebp), %edx # get xp. . .
Body
![Page 37: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/37.jpg)
– 37 – 15-213, S’03
swap Finish #1swap Finish #1
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
yp
xp
Rtn adr
Old %ebp %ebp 0
4
8
12
Offset
swap’sStack
•••
Old %ebx %esp-4
ObservationObservation Saved & restored register %ebx
yp
xp
Rtn adr
Old %ebp %ebp 0
4
8
12
Offset
•••
Old %ebx %esp-4
![Page 38: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/38.jpg)
– 38 – 15-213, S’03
swap Finish #2swap Finish #2
yp
xp
Rtn adr
Old %ebp %ebp 0
4
8
12
Offset
swap’sStack
•••
Old %ebx %esp-4
yp
xp
Rtn adr
Old %ebp %ebp 0
4
8
12
Offset
swap’sStack
•••
%esp
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
![Page 39: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/39.jpg)
– 39 – 15-213, S’03
swap Finish #3swap Finish #3
yp
xp
Rtn adr
%ebpswap’sStack
•••
yp
xp
Rtn adr
Old %ebp %ebp 0
4
8
12
Offset
swap’sStack
•••
%esp
%esp
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
![Page 40: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/40.jpg)
– 40 – 15-213, S’03
swap Finish #4swap Finish #4
&zip2
&zip1 %esp
ExitingStack
•••
%ebp
ObservationObservation Saved & restored register %ebx Didn’t for %eax, %ecx, or %edx
yp
xp
Rtn adr
%ebp
4
8
12
Offset
swap’sStack
•••
%esp
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
![Page 41: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/41.jpg)
– 41 – 15-213, S’03
Register Saving ConventionsRegister Saving Conventions
When procedure When procedure yooyoo calls calls whowho:: yoo is the caller, who is the callee
Can Register be Used for Temporary Storage?Can Register be Used for Temporary Storage?
Contents of register %edx overwritten by who
yoo:• • •movl $15213, %edxcall whoaddl %edx, %eax• • •ret
who:• • •movl 8(%ebp), %edxaddl $91125, %edx• • •ret
![Page 42: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/42.jpg)
– 42 – 15-213, S’03
Register Saving ConventionsRegister Saving Conventions
When procedure When procedure yooyoo calls calls whowho:: yoo is the caller, who is the callee
Can Register be Used for Temporary Storage?Can Register be Used for Temporary Storage?
ConventionsConventions “Caller Save”
Caller saves temporary in its frame before calling
“Callee Save”Callee saves temporary in its frame before using
![Page 43: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/43.jpg)
– 43 – 15-213, S’03
IA32/Linux Register UsageIA32/Linux Register Usage
Integer RegistersInteger Registers Two have special uses
%ebp, %esp Three managed as
callee-save%ebx, %esi, %ediOld values saved on
stack prior to using
Three managed as caller-save%eax, %edx, %ecxDo what you please,
but expect any callee to do so, as well
Register %eax also stores returned value
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
Caller-SaveTemporaries
Callee-SaveTemporaries
Special
![Page 44: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/44.jpg)
– 44 – 15-213, S’03
Swap: 1 mo’ timeSwap: 1 mo’ time
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
Void callswap(void){
int x = 1;int y = 2;
swap(&x, &y);printf(“%d %d”, x, y);
}
callswap:pushl %ebpmovl %esp,%ebpsubl $24,%espmovl $1,-8(%ebp)addl $-8,%espmovl $2,-4(%ebp)leal -4(%ebp),%eaxpushl %eaxleal -8(%ebp),%eaxpushl %eaxcall _swapmovl -4(%ebp),%eaxaddl $-4,%esppushl %eaxmovl -8(%ebp),%eaxpushl %eaxpushl $LC0call _printfmovl %ebp,%esppopl %ebpret
![Page 45: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/45.jpg)
– 45 – 15-213, S’03
Callswap’s frameCallswap’s frame
Void callswap(void){
int x = 1;int y = 2;
swap(&x, &y);printf(“%d %d”, x, y);
}
callswap:pushl %ebpmovl %esp,%ebpsubl $24,%espmovl $1,-8(%ebp)addl $-8,%espmovl $2,-4(%ebp)leal -4(%ebp),%eaxpushl %eaxleal -8(%ebp),%eaxpushl %eaxcall _swapmovl -4(%ebp),%eaxaddl $-4,%esppushl %eaxmovl -8(%ebp),%eaxpushl %eaxpushl $LC0call _printfmovl %ebp,%esppopl %ebpret
Caller’s %ebp
y
x
![Page 46: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/46.jpg)
– 46 – 15-213, S’03
Swap’s Frame
After swap’s prologAfter swap’s prolog
Void callswap(void){
int x = 1;int y = 2;
swap(&x, &y);printf(“%d %d”, x, y);
}
Caller’s %ebp
y
x
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
&y
&x
caller’s $ebx
RetAdr
![Page 47: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/47.jpg)
– 47 – 15-213, S’03
Epilog
Prologint rfact(int x){ int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x;}
.globl rfact.type
rfact,@functionrfact:
pushl %ebpmovl %esp,%ebppushl %ebxmovl 8(%ebp),%ebxcmpl $1,%ebxjle .L78leal -1(%ebx),%eaxpushl %eaxcall rfactimull %ebx,%eaxjmp .L79.align 4
.L78:movl $1,%eax
.L79:movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
Recursive FactorialRecursive Factorial
Where is X?Where is rval?
![Page 48: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/48.jpg)
– 48 – 15-213, S’03
int rfact(int x){ int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x;}
.globl rfact.type
rfact,@functionrfact:
pushl %ebpmovl %esp,%ebppushl %ebxmovl 8(%ebp),%ebxcmpl $1,%ebxjle .L78leal -1(%ebx),%eaxpushl %eaxcall rfactimull %ebx,%eaxjmp .L79.align 4
.L78:movl $1,%eax
.L79:movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
Recursive FactorialRecursive Factorial
Epilog
Prolog
Where is X? 8(%ebp), %ebxWhere is rval? %eax
![Page 49: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/49.jpg)
– 49 – 15-213, S’03
rfact:pushl %ebpmovl %esp,%ebppushl %ebx
rfact:pushl %ebpmovl %esp,%ebppushl %ebx
Rfact Stack PrologRfact Stack Prolog
Entering Stack
x
Rtn adr
Caller
%espOld %ebx
4
8
%ebp 0
-4 Callee
x
Rtn adr
Caller
%esp
%ebppre %ebp
pre %ebx
pre %ebp
pre %ebx
Old %ebp
rfact:pushl %ebpmovl %esp,%ebppushl %ebx
Can now see that argument, x, is at
8(%ebp)
![Page 50: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/50.jpg)
– 50 – 15-213, S’03
Rfact BodyRfact Body
RegistersRegisters%ebx Stored value of x
%eaxTemporary value of x-1Returned value from rfact(x-1)Returned value from this call
movl 8(%ebp),%ebx # ebx = xcmpl $1,%ebx # Compare x : 1jle .L78 # If <= goto Termleal -1(%ebx),%eax # eax = x-1pushl %eax # Push x-1call rfact # rfact(x-1)imull %ebx,%eax # rval * xjmp .L79 # Goto done
.L78: # Term:movl $1,%eax # return val = 1
.L79: # Done:
int rfact(int x){ int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x;}
![Page 51: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/51.jpg)
– 51 – 15-213, S’03
Rfact BodyRfact Body
RegistersRegisters%ebx Stored value of x
%eaxTemporary value of x-1Returned value from rfact(x-1)Returned value from this call
movl 8(%ebp),%ebx # ebx = xcmpl $1,%ebx # Compare x : 1jle .L78 # If <= goto Termleal -1(%ebx),%eax # eax = x-1pushl %eax # Push x-1call rfact # rfact(x-1)imull %ebx,%eax # rval * xjmp .L79 # Goto done
.L78: # Term:movl $1,%eax # return val = 1
.L79: # Done:
int rfact(int x){ int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x;}
![Page 52: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/52.jpg)
– 52 – 15-213, S’03
Rfact BodyRfact Body
RegistersRegisters%ebx Stored value of x
%eaxTemporary value of x-1Returned value from rfact(x-1)Returned value from this call
movl 8(%ebp),%ebx # ebx = xcmpl $1,%ebx # Compare x : 1jle .L78 # If <= goto Termleal -1(%ebx),%eax # eax = x-1pushl %eax # Push x-1call rfact # rfact(x-1)imull %ebx,%eax # rval * xjmp .L79 # Goto done
.L78: # Term:movl $1,%eax # return val = 1
.L79: # Done:
int rfact(int x){ int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x;}
![Page 53: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/53.jpg)
– 53 – 15-213, S’03
Rfact BodyRfact Body
RegistersRegisters%ebx Stored value of x
%eaxTemporary value of x-1Returned value from rfact(x-1)Returned value from this call
movl 8(%ebp),%ebx # ebx = xcmpl $1,%ebx # Compare x : 1jle .L78 # If <= goto Termleal -1(%ebx),%eax # eax = x-1pushl %eax # Push x-1call rfact # rfact(x-1)imull %ebx,%eax # rval * xjmp .L79 # Goto done
.L78: # Term:movl $1,%eax # return val = 1
.L79: # Done:
int rfact(int x){ int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x;}
Recursion
![Page 54: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/54.jpg)
– 54 – 15-213, S’03
Rfact BodyRfact Body
RegistersRegisters%ebx Stored value of x
%eaxTemporary value of x-1Returned value from rfact(x-1)Returned value from this call
movl 8(%ebp),%ebx # ebx = xcmpl $1,%ebx # Compare x : 1jle .L78 # If <= goto Termleal -1(%ebx),%eax # eax = x-1pushl %eax # Push x-1call rfact # rfact(x-1)imull %ebx,%eax # rval * xjmp .L79 # Goto done
.L78: # Term:movl $1,%eax # return val = 1
.L79: # Done:
int rfact(int x){ int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x;}
Recursion
![Page 55: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/55.jpg)
– 55 – 15-213, S’03
int rfact(int x){ int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x;}
.globl rfact.type
rfact,@functionrfact:
pushl %ebpmovl %esp,%ebppushl %ebxmovl 8(%ebp),%ebxcmpl $1,%ebxjle .L78leal -1(%ebx),%eaxpushl %eaxcall rfactimull %ebx,%eaxjmp .L79.align 4
.L78:movl $1,%eax
.L79:movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
Recursive FactorialRecursive Factorial
RegistersRegisters %eax used without first saving %ebx used, but save at
beginning & restore at end
![Page 56: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/56.jpg)
– 56 – 15-213, S’03
Rfact RecursionRfact Recursion
%ebp
pushl %eax
%espx-1
x
Rtn adr
Old %ebp
Old %ebx
x-1%eax
x%ebx
x
Rtn adr
Old %ebp %ebp
Old %ebx %esp
%eax
x%ebx
x-1
leal -1(%ebx),%eax
x
Rtn adr
Old %ebp
Old %ebx
x-1
x-1%eax
x%ebx
%ebp
%espRtn adr
call rfact
…leal -1(%ebx),%eax # eax = x-1pushl %eax # Push x-1call rfact # rfact(x-1)…
![Page 57: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/57.jpg)
– 57 – 15-213, S’03
After Rfact Recursion?After Rfact Recursion?
%ebp
pushl %eax
%espx-1
x
Rtn adr
Old %ebp
Old %ebx
x-1%eax
x%ebx
x
Rtn adr
Old %ebp %ebp
Old %ebx %esp
%eax
x%ebx
x-1
leal -1(%ebx),%eax
x
Rtn adr
Old %ebp
Old %ebx
x-1
x-1%eax
x%ebx
%ebp
%espRtn adr
call rfact
…leal -1(%ebx),%eax # eax = x-1pushl %eax # Push x-1call rfact # rfact(x-1)imull %ebx,%eax # rval * x…
•%ebx has value of x•%eax has value of (x-1)!
![Page 58: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/58.jpg)
– 58 – 15-213, S’03
(x-1)!
Rfact ResultRfact Result
x
Rtn adr
Old %ebp %ebp
Old %ebx
%espx-1
imull %ebx,%eax
x!%eax
x%ebx
x
Rtn adr
Old %ebp %ebp
Old %ebx
%espx-1
(x-1)!%eax
x%ebx
Return from Call
(x-1)!
Assume that rfact(x-1) returns (x-1)! in register %eax
![Page 59: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/59.jpg)
– 59 – 15-213, S’03
Rfact EpilogRfact Epilog movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
x
Rtn adr
Old %ebp %ebp 0
4
8
Old %ebx
%esp
-4
x!%eax
x%ebx
x-1-8
pre %ebp
pre %ebx
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
x
Rtn adr
Old %ebp %ebp 0
4
8
%esp
x!%eax
Old %ebx%ebx
pre %ebp
pre %ebx
Old %ebx
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
x
Rtn adr
%ebp
%esp
x!%eax
Old %ebx%ebx
pre %ebp
pre %ebx
![Page 60: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/60.jpg)
– 60 – 15-213, S’03
Pointer CodePointer Code
void s_helper (int x, int *accum){ if (x <= 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;}
Top-Level CallRecursive Procedure
Pass pointer to update location
![Page 61: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/61.jpg)
– 61 – 15-213, S’03
Temp.Space
%esp
Creating & Initializing PointerCreating & Initializing Pointer
int sfact(int x){ int val = 1; s_helper(x, &val); return val;}
_sfact:pushl %ebp # Save %ebpmovl %esp,%ebp # Set %ebpsubl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = xmovl $1,-4(%ebp) # val = 1
Using Stack for Local VariableUsing Stack for Local Variable Variable val must be stored
on stackNeed to create pointer to it
Compute pointer as -4(%ebp) Push on stack as second
argument
Initial part of sfact
x
Rtn adr
Old %ebp %ebp 0
4
8
-4 val = 1
Unused-12
-8
-16
_sfact:pushl %ebp # Save %ebpmovl %esp,%ebp # Set %ebpsubl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = xmovl $1,-4(%ebp) # val = 1
_sfact:pushl %ebp # Save %ebpmovl %esp,%ebp # Set %ebpsubl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = xmovl $1,-4(%ebp) # val = 1
_sfact:pushl %ebp # Save %ebpmovl %esp,%ebp # Set %ebpsubl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = xmovl $1,-4(%ebp) # val = 1
![Page 62: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/62.jpg)
– 62 – 15-213, S’03
Passing PointerPassing Pointer
int sfact(int x){ int val = 1; s_helper(x, &val); return val;}
leal -4(%ebp),%eax # Compute &valpushl %eax # Push on stackpushl %edx # Push xcall s_helper # callmovl -4(%ebp),%eax # Return val• • • # Finish
Calling s_helper from sfact
x
Rtn adr
Old %ebp %ebp 0
4
8
val = 1 -4
Unused-12
-8
-16
%espx
&val
Stack at time of call
leal -4(%ebp),%eax # Compute &valpushl %eax # Push on stackpushl %edx # Push xcall s_helper # callmovl -4(%ebp),%eax # Return val• • • # Finish
leal -4(%ebp),%eax # Compute &valpushl %eax # Push on stackpushl %edx # Push xcall s_helper # callmovl -4(%ebp),%eax # Return val• • • # Finish
val =x!
![Page 63: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/63.jpg)
– 63 – 15-213, S’03
Using PointerUsing Pointer
• • •movl %ecx,%eax # z = ximull (%edx),%eax # z *= *accummovl %eax,(%edx) # *accum = z• • •
void s_helper (int x, int *accum){ • • • int z = *accum * x; *accum = z; • • •}
Register %ecx holds x Register %edx holds pointer to accum
Use access (%edx) to reference memory
%edxaccum
x
x%eax
%ecx
accum*x
accum*x
![Page 64: Machine-Level Programming III: Procedures Jan 30, 2003 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 15-213](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d585503460f94a37d6b/html5/thumbnails/64.jpg)
– 64 – 15-213, S’03
SummarySummary
The Stack Makes Recursion WorkThe Stack Makes Recursion Work Private storage for each instance of procedure call
Instantiations don’t clobber each otherAddressing of locals + arguments can be relative to stack
positions
Can be managed by stack disciplineProcedures return in inverse order of calls
IA32 Procedures Combination of Instructions + IA32 Procedures Combination of Instructions + ConventionsConventions Call / Ret instructions Register usage conventions
Caller / Callee save %ebp and %esp
Stack frame organization conventions