IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ......
Transcript of IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ......
![Page 1: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/1.jpg)
1
IA32Procedures
![Page 2: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/2.jpg)
2
IA32Stack
¢ Regionofmemorymanagedwithstackdiscipline
¢ Growstowardloweraddresses
¢ Register%espcontainsloweststackaddress§ addressof“top”element
StackPointer:%esp
StackGrows Down
Increasing Addresses
Stack“Top”
Stack“BoDom”
![Page 3: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/3.jpg)
3
IA32Stack:Push¢ pushl Src § FetchoperandatSrc§ Decrement%espby4§ Writeoperandataddressgivenby%esp
-4
StackGrows Down
Increasing Addresses
Stack“BoDom”
StackPointer:%esp
Stack“Top”
![Page 4: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/4.jpg)
4
StackPointer:%esp
StackGrows Down
Increasing Addresses
Stack“Top”
Stack“BoDom”IA32Stack:Pop
+4
![Page 5: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/5.jpg)
5
ProcedureControlFlow¢ Usestacktosupportprocedurecallandreturn¢ Procedurecall:call label § Pushreturnaddressonstack§ Jumptolabel
¢ Returnaddress:§ AddressofthenextinstrucBonrightaCercall§ Examplefromdisassembly804854e: e8 3d 06 00 00 call 8048b90 <main>
8048553: 50 pushl %eax § Returnaddress=0x8048553
¢ Procedurereturn:ret § Popaddressfromstack§ Jumptoaddress
![Page 6: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/6.jpg)
6
0x8048553
0x104 %esp
%eip
%esp
%eip 0x8048b90
0x108
0x10c
0x110
0x104
0x804854e
123
ProcedureCallExample
0x108
0x10c
0x110
123
0x108
call 8048b90
804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax
%eip: programcounter
![Page 7: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/7.jpg)
7
%esp
%eip
0x104
%esp
%eip 0x8048591
0x104
0x108
0x10c
0x110
0x8048553
123
ProcedureReturnExample
0x108
0x10c
0x110
123
ret
8048591: c3 ret
0x108
0x8048553
0x8048553
%eip: programcounter
![Page 8: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/8.jpg)
8
Stack-BasedLanguages¢ Languagesthatsupportrecursion§ e.g.,C,Pascal,Java§ Codemustbe“Reentrant”
§ MulBplesimultaneousinstanBaBonsofsingleprocedure§ NeedsomeplacetostorestateofeachinstanBaBon
§ Arguments§ Localvariables§ Returnpointer
¢ Stackdiscipline§ StateforgivenprocedureneededforlimitedBme
§ Fromwhencalledtowhenreturn§ Calleereturnsbeforecallerdoes
¢ StackallocatedinFrames§ stateforsingleprocedureinstanBaBon
![Page 9: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/9.jpg)
9
CallChainExampleyoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
yoo
who
amI
amI
amI
Example CallChain
amI
ProcedureamI()isrecursive
![Page 10: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/10.jpg)
10
FramePointer:%ebp
StackFrames
¢ Contents§ Localvariables§ ReturninformaBon§ Temporaryspace
¢ Management§ Spaceallocatedwhenenter
procedure§ “Set-up”code
§ Deallocatedwhenreturn§ “Finish”code
StackPointer:%esp
Stack“Top”
PreviousFrame
Frameforproc
![Page 11: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/11.jpg)
11
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
yoo(…) { • • who(); • • }
![Page 12: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/12.jpg)
12
yoo(…) { • • who(); • • }
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
who(…) { • • • amI(); • • • amI(); • • • }
![Page 13: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/13.jpg)
13
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
amI(…) { • • amI(); • • }
![Page 14: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/14.jpg)
14
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
amI
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
amI(…) { • • amI(); • • }
![Page 15: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/15.jpg)
15
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
amI
amI
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
amI(…) { • • amI(); • • }
amI(…) { • • amI(); • • }
![Page 16: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/16.jpg)
16
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
amI
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
amI(…) { • • amI(); • • }
![Page 17: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/17.jpg)
17
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
![Page 18: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/18.jpg)
18
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
![Page 19: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/19.jpg)
19
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
![Page 20: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/20.jpg)
20
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
![Page 21: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/21.jpg)
21
Exampleyoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo yoo(…) { • • who(); • • }
![Page 22: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/22.jpg)
22
IA32/LinuxStackFrame
¢ CurrentStackFrame(“Top”toBoDom)§ “Argumentbuild:”
ParametersforfuncBonabouttocall§ Localvariables
Ifcan’tkeepinregisters§ Savedregistercontext§ Oldframepointer
¢ CallerStackFrame§ Returnaddress
§ PushedbycallinstrucBon§ Argumentsforthiscall
ReturnAddr
SavedRegisters+LocalVariables
ArgumentBuild
Old%ebp
Arguments
Caller Frame
Framepointer%ebp
Stackpointer %esp
![Page 23: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/23.jpg)
23
RevisiWngswap
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; }
int course1 = 15213; int course2 = 18243; void call_swap() { swap(&course1, &course2); }
call_swap: • • • subl $8, %esp movl $course2, 4(%esp) movl $course1, (%esp) call swap • • •
&course2 &course1 Rtnadr %esp
ResulWng Stack•
• •
Callingswapfromcall_swap
%esp
%esp subl
call
![Page 24: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/24.jpg)
24
RevisiWngswap
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; }
swap: pushl %ebp movl %esp, %ebp pushl %ebx
movl 8(%ebp), %edx movl 12(%ebp), %ecx movl (%edx), %ebx movl (%ecx), %eax movl %eax, (%edx) movl %ebx, (%ecx)
popl %ebx popl %ebp ret
Body
Set Up
Finish
![Page 25: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/25.jpg)
25
swapSetup#1
swap: pushl %ebp movl %esp,%ebp pushl %ebx
ResulWngStack
&course2
&course1
Rtnadr %esp
EnteringStack
• • •
%ebp
yp
xp
Rtnadr
Old%ebp
%ebp • • •
%esp
![Page 26: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/26.jpg)
26
swapSetup#2
swap: pushl %ebp movl %esp,%ebp pushl %ebx
ResulWngStack
&course2
&course1
Rtnadr %esp
EnteringStack
• • •
%ebp
yp
xp
Rtnadr
Old%ebp %ebp
• • •
%esp
![Page 27: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/27.jpg)
27
swapSetup#3
swap: pushl %ebp movl %esp,%ebp pushl %ebx
ResulWngStack
&course2
&course1
Rtnadr %esp
EnteringStack
• • •
%ebp
yp
xp
Rtnadr
Old%ebp %ebp
• • •
%esp Old%ebx
![Page 28: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/28.jpg)
28
swapBody
movl 8(%ebp),%edx # get xp movl 12(%ebp),%ecx # get yp . . .
ResulWngStack
&course2
&course1
Rtnadr %esp
EnteringStack
• • •
%ebp
yp
xp
Rtnadr
Old%ebp %ebp
• • •
%esp Old%ebx
OffsetrelaWveto%ebp
12
8
4
![Page 29: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/29.jpg)
29
swapFinishStackBeforeFinish
popl %ebx popl %ebp
yp
xp
Rtnadr
Old%ebp %ebp
• • •
%esp Old%ebx
ResulWngStack
yp
xp
Rtnadr
• • •
%ebp
%esp
¢ Observation § Savedandrestoredregister%ebx§ Notsofor%eax,%ecx,%edx
![Page 30: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/30.jpg)
30
Disassembledswap 08048384 <swap>: 8048384: 55 push %ebp 8048385: 89 e5 mov %esp,%ebp 8048387: 53 push %ebx 8048388: 8b 55 08 mov 0x8(%ebp),%edx 804838b: 8b 4d 0c mov 0xc(%ebp),%ecx 804838e: 8b 1a mov (%edx),%ebx 8048390: 8b 01 mov (%ecx),%eax 8048392: 89 02 mov %eax,(%edx) 8048394: 89 19 mov %ebx,(%ecx) 8048396: 5b pop %ebx 8048397: 5d pop %ebp 8048398: c3 ret
80483b4: movl $0x8049658,0x4(%esp) # Copy &course2 80483bc: movl $0x8049654,(%esp) # Copy &course1 80483c3: call 8048384 <swap> # Call swap 80483c8: leave # Prepare to return 80483c9: ret # Return
CallingCode
![Page 31: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/31.jpg)
31
Today¢ Switchstatements¢ IA32Procedures
§ StackStructure§ CallingConvenBons§ IllustraBonsofRecursion&Pointers
![Page 32: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/32.jpg)
32
RegisterSavingConvenWons¢ Whenprocedureyoocallswho:§ yooisthecaller§ whoisthecallee
¢ Canregisterbeusedfortemporarystorage?
§ Contentsofregister%edxoverwriUenbywho § Thiscouldbetrouble➙somethingshouldbedone!
§ NeedsomecoordinaBon
yoo: • • •
movl $15213, %edx call who addl %edx, %eax
• • • ret
who: • • •
movl 8(%ebp), %edx addl $18243, %edx
• • • ret
![Page 33: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/33.jpg)
33
RegisterSavingConvenWons¢ Whenprocedureyoocallswho:§ yooisthecaller§ whoisthecallee
¢ Canregisterbeusedfortemporarystorage?¢ ConvenWons§ “CallerSave”
§ Callersavestemporaryvaluesinitsframebeforethecall§ “CalleeSave”
§ Calleesavestemporaryvaluesinitsframebeforeusing
![Page 34: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/34.jpg)
34
IA32/Linux+WindowsRegisterUsage¢ %eax,%edx,%ecx
§ Callersavespriortocallifvaluesareusedlater
¢ %eax § alsousedtoreturnintegervalue
¢ %ebx,%esi,%edi § Calleesavesifwantstousethem
¢ %esp,%ebp § specialformofcalleesave§ Restoredtooriginalvaluesupon
exitfromprocedure
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
Caller-Save Temporaries
Callee-Save Temporaries
Special
![Page 35: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/35.jpg)
35
Today¢ Switchstatements¢ IA32Procedures
§ StackStructure§ CallingConvenBons§ IllustraBonsofRecursion&Pointers
![Page 36: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/36.jpg)
36
/* Recursive popcount */ int pcount_r(unsigned x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
RecursiveFuncWon
¢ Registers§ %eax,%edx usedwithout
firstsaving§ %ebxused,butsavedat
beginning&restoredatend
pcount_r: pushl %ebp movl %esp, %ebp pushl %ebx subl $4, %esp movl 8(%ebp), %ebx movl $0, %eax testl %ebx, %ebx je .L3 movl %ebx, %eax shrl %eax movl %eax, (%esp) call pcount_r movl %ebx, %edx andl $1, %edx leal (%edx,%eax), %eax
.L3: addl $4, %esp popl %ebx popl %ebp ret
![Page 37: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/37.jpg)
37
/* Recursive popcount */ int pcount_r(unsigned x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
RecursiveCall#1
¢ AcWons§ Saveoldvalueof%ebxon
stack§ Allocatespaceforargument
torecursivecall§ Storexin%ebx
pcount_r: pushl %ebp movl %esp, %ebp pushl %ebx subl $4, %esp movl 8(%ebp), %ebx • • •
x
Rtnadr
Old%ebp %ebp
• • •
%esp Old%ebx
x %ebx
![Page 38: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/38.jpg)
38
/* Recursive popcount */ int pcount_r(unsigned x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
RecursiveCall#2
¢ AcWons§ Ifx==0,return
§ with%eaxsetto0
• • • movl $0, %eax testl %ebx, %ebx je .L3 • • •
.L3: • • • ret
x %ebx
![Page 39: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/39.jpg)
39
/* Recursive popcount */ int pcount_r(unsigned x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
RecursiveCall#3
¢ AcWons§ Store x >> 1 on stack § Make recursive call
¢ Effect§ %eax set to function result § %ebx still has value of x
• • • movl %ebx, %eax shrl %eax movl %eax, (%esp) call pcount_r • • •
Rtnadr
Old%ebp %ebp
• • •
%esp Old%ebx
x >> 1 x %ebx
![Page 40: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/40.jpg)
40
/* Recursive popcount */ int pcount_r(unsigned x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
RecursiveCall#4
¢ Assume§ %eaxholds value from recursive call § %ebxholds x
¢ AcWons§ Compute (x & 1) + computed value
¢ Effect§ %eax set to function result
• • • movl %ebx, %edx andl $1, %edx leal (%edx,%eax), %eax • • •
x %ebx
![Page 41: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/41.jpg)
41
/* Recursive popcount */ int pcount_r(unsigned x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
RecursiveCall#5
¢ AcWons§ Restore values of %ebx and %ebp
§ Restore %esp
• • • L3:
addl $4, %esp popl %ebx popl %ebp ret
Rtnadr
Old%ebp %ebp
• • •
%esp Old%ebx
Old %ebx
%ebx
%ebp • • •
%esp
![Page 42: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/42.jpg)
42
ObservaWonsAboutRecursion¢ HandledWithoutSpecialConsideraWon
§ StackframesmeanthateachfuncBoncallhasprivatestorage§ Savedregisters&localvariables§ Savedreturnpointer
§ RegistersavingconvenBonspreventonefuncBoncallfromcorrupBnganother’sdata
§ Stackdisciplinefollowscall/returnpaUern§ IfPcallsQ,thenQreturnsbeforeP§ Last-In,First-Out
¢ Alsoworksformutualrecursion§ PcallsQ;QcallsP
![Page 43: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/43.jpg)
43
PointerCode
/* Compute x + 3 */ int add3(int x) { int localx = x; incrk(&localx, 3); return localx; }
GeneraWngPointer
¢ add3createspointerandpassesittoincrk
/* Increment value by k */ void incrk(int *ip, int k) { *ip += k; }
ReferencingPointer
![Page 44: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/44.jpg)
44
%esp
CreaWngandIniWalizingLocalVariable
int add3(int x) { int localx = x; incrk(&localx, 3); return localx; }
¢ Variablelocalxmustbestoredonstack§ Because:Needtocreatepointertoit
¢ Computepointeras-4(%ebp)
Firstpartofadd3
x
Rtnadr
Old%ebp %ebp 0
4
8
-4 localx = x
Unused-12
-8
-16
add3: pushl %ebp movl %esp, %ebp subl $24, %esp # Alloc. 24 bytes movl 8(%ebp), %eax movl %eax, -4(%ebp) # Set localx to x -20
-24
![Page 45: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/45.jpg)
45
%esp
CreaWngPointerasArgument
int add3(int x) { int localx = x; incrk(&localx, 3); return localx; }
¢ UselealinstrucWontocomputeaddressoflocalx
Middlepartofadd3
x
Rtnadr
Old%ebp %ebp 0
4
8
-4 localx
Unused-12
-8
-16
movl $3, 4(%esp) # 2nd arg = 3 leal -4(%ebp), %eax # &localx movl %eax, (%esp) # 1st arg = &localx call incrk
-20
-24
3 %esp+4
![Page 46: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/46.jpg)
46
%esp
Retrievinglocalvariable
int add3(int x) { int localx = x; incrk(&localx, 3); return localx; }
¢ Retrievelocalxfromstackasreturnvalue
Finalpartofadd3
x
Rtnadr
Old%ebp %ebp 0
4
8
-4 localx
Unused-12
-8
-16
movl -4(%ebp), %eax # Return val= localx leave ret
-20
-24
![Page 47: IA32 Procedurescsong/cs153/refs/procedures.pdf · 2018. 4. 2. · Rtn adr %esp ResulWng ... 8048397: 5d pop %ebp 8048398: c3 ret 80483b4: movl $0x8049658,0x4(%esp) # Copy &course2](https://reader035.fdocuments.us/reader035/viewer/2022070300/6149f91312c9616cbc691c9a/html5/thumbnails/47.jpg)
47
IA32ProcedureSummary
¢ ImportantPoints§ Stackistherightdatastructureforprocedure
call/return§ IfPcallsQ,thenQreturnsbeforeP
¢ Recursion(&mutualrecursion)handledbynormalcallingconvenWons§ Cansafelystorevaluesinlocalstackframeand
incallee-savedregisters§ PutfuncBonargumentsattopofstack§ Resultreturnin%eax
¢ Pointers are addresses of values § On stack or global
ReturnAddr
Saved Registers + Local Variables
Argument Build
Old%ebp
Arguments
Caller Frame
%ebp
%esp