Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure...
Transcript of Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure...
![Page 1: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/1.jpg)
FunctionsandtheStack10/4/16
![Page 2: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/2.jpg)
Overview
• Stackdatastructure,appliedtomemory
• Behavioroffunctioncalls
• Storageoffunctiondata,atIA32level
![Page 3: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/3.jpg)
“A”Stack• Astackisabasicdatastructure• Lastin,firstoutbehavior(LIFO)• Twooperations
• Push(additemtotopofstack)• Pop(removeitemfromtopofstack)
Oldestdata
Newestdata
Push(adddataitem)
Pop(removeandreturnitem)
![Page 4: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/4.jpg)
• Applystackdatastructuretomemory• Storelocal(automatic)variables• Maintainstateforfunctions(e.g.,wheretoreturn)
• Organizedintounitscalledframes• Oneframerepresentsalloftheinformationforonefunction.• Sometimescalledactivationrecords
“The”Stack
![Page 5: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/5.jpg)
MemoryModel
• Stackstartsatthehighestmemoryaddresses,growsintoloweraddresses.
0x0
0xFFFFFFFF
Operatingsystem
Stack
TextData
Heap
![Page 6: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/6.jpg)
StackFrames
• Asfunctionsgetcalled,newframesaddedtostack.
• Example:Lab4• maincallsget_values()• get_values callsread_float()• read_float callsI/Olibrary
main
0xFFFFFFFF
get_values
read_float
(I/Olibrary)
![Page 7: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/7.jpg)
StackFrames
• Asfunctionsreturn,framesremovedfromstack.
• Example:Lab4• I/Olibraryreturnstoread_float• read_float returnstoget_values• get_values returnstomain
main
0xFFFFFFFF
get_values
read_float
(I/Olibrary)
Allofthisstackgrowing/shrinkinghappensautomatically(fromtheprogrammer’sperspective).
![Page 8: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/8.jpg)
Whatisresponsibleforcreatingandremovingstackframes?A. Theuser
B. Thecompiler
C. Clibrarycode
D. Theoperatingsystem
E. Something/someoneelse
Insight:EVERYfunctionneedsastackframe.Creating/destroyingastackframeisa(mostly)genericprocedure.
![Page 9: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/9.jpg)
StackFrameContents• Whatneedstobestoredinastackframe?• Alternatively:Whatmust afunctionknow/access?
• Hint:Atleast5things
main
0xFFFFFFFF
get_values
read_float
![Page 10: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/10.jpg)
StackFrameContents
• Whatneedstobestoredinastackframe?• Alternatively:Whatmust afunctionknow?
• Localvariables• Previousstackframebaseaddress• Functionarguments• Returnvalue• Returnaddress
• Savedregisters• Spilledtemporaries
main
0xFFFFFFFF
function1
function2
![Page 11: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/11.jpg)
LocalVariables
Iftheprogrammersays:int x = 0;
Whereshouldxbestored?(Recallbasicstackdatastructure)
Whichmemoryaddressisthat?
main
0xFFFFFFFF
function1
function2
Xgoeshere
0x????????
![Page 12: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/12.jpg)
Howshouldwedeterminetheaddresstouseforstoringanewlocalvariable?
A. Theprogrammerspecifiesthevariablelocation.
B. TheCPUstoresthelocationofthecurrentstackframe.
C. Theoperatingsystemkeepstrackofthetopofthestack.
D. Thecompilerknows/determineswherethelocaldataforeachfunctionwillbeasitgeneratescode.
E. Theaddressisdeterminedsomeotherway.
![Page 13: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/13.jpg)
• Compiletime(static)• Informationthatisknownbyanalyzingyourprogram• Independentofthemachineandinputs
• Runtime(dynamic)• Informationthatisn’tknownuntilprogramisrunning• Dependsonmachinecharacteristicsanduserinput
![Page 14: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/14.jpg)
TheCompilerCan…
• Determinehowmuchspaceyouneedonthestacktostorelocalvariables.
• InsertIA32instructionsforyoutosetupthestackforfunctioncalls.• Createstackframesonfunctioncall• Restorestacktopreviousstateonfunctionreturn
• Performtypechecking,etc.
![Page 15: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/15.jpg)
CurrentStackFrame
LocalVariables
• CompilercanallocateNbytesonthestackbysubtractingNfromthe“stackpointer”:%esp
CurrentStackFrame
esp
esp - N
Nbytes
![Page 16: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/16.jpg)
TheCompilerCan’t…• Predictuserinput.
int main() {
int x = get_user_input();
if (x > 5) {
funcA(x);
} else {
funcB();
}
}
main
0xFFFFFFFF
funcBfuncA ???
![Page 17: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/17.jpg)
TheCompilerCan’t…• Predictuserinput.
• Assumeafunctionwillalwaysbeatacertainaddressonthestack.
Alternative:createstackframesrelativetothecurrent(dynamic)stateofthestack.
main
0xFFFFFFFF
funcBfuncA ???
funcB
![Page 18: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/18.jpg)
StackFrameLocation
• Whereinmemoryisthecurrentstackframe?
main
0xFFFFFFFF
function1
function2
Currenttopofstack
Currentbottomofstack
![Page 19: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/19.jpg)
Recall:IA32Registers
%eip
Generalpurposeregisters
Currentstacktop
Currentstackframe
Instructionpointer(PC)
CF ZF SF OF Conditioncodes
%eax
%ecx
%edx
%ebx
%esi
%edi
%esp
%ebp
![Page 20: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/20.jpg)
StackFrameLocation
• Whereinmemoryisthecurrentstackframe?
• Maintaininvariant:• Thecurrentfunction’sstackframeisalwaysbetweentheaddressesstoredin%esp and%ebp
• %esp:stackpointer• %ebp:framepointer(basepointer) main
0xFFFFFFFF
function1
function2
%esp
%ebp
![Page 21: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/21.jpg)
StackFrameLocation
• Compilerensuresthatthisinvariantholds.• We’llseehowabitlater.
• Thisiswhyalllocalvariableswe’veseeninIA32arerelativeto%ebp or%esp!
main
0xFFFFFFFF
function1
function2
%esp
%ebp
![Page 22: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/22.jpg)
HowwouldweimplementpushingxtothetopofthestackinIA32?A. Increment%esp
Storexat(%esp)
B. Storexat(%esp)Increment%esp
C. Decrement%espStorexat(%esp)
D. Storexat(%esp)Decrement%esp
E. Copy%esp to%ebpStorexat(%ebp) main
0xFFFFFFFF
function1
function2
Xgoeshere%esp
(Topofstack)
%ebp(Framestart)
![Page 23: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/23.jpg)
Push&Pop
• IA32providesconvenientinstructions:• pushl src
• Movestackpointerupby4bytes subl $4, %esp• Copy‘src’tocurrenttopofstack movl src, (%esp)
• popl dst• Copycurrenttopofstackto‘dst’ movl (%esp), dst• Movestackpointerdown4bytes addl $4, %esp
• src anddst arethecontentsofanyregister
![Page 24: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/24.jpg)
LocalVariables
• Moregenerally,wecanmakespaceonthestackforNbytesbysubtractingNfrom%esp
CurrentStackFrame
CurrentStackFrame
esp esp - NNbytes
Newvariable
![Page 25: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/25.jpg)
LocalVariables
• Moregenerally,wecanmakespaceonthestackforNbytesbysubtractingNfrom%esp• Whenwe’redone,freethespacebyaddingNbackto%esp
CurrentStackFrame
CurrentStackFrame
esp
esp - N
Nbytes
Newvariable
![Page 26: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/26.jpg)
StackFrameContents
• Whatneedstobestoredinastackframe?• Alternatively:Whatmust afunctionknow?
• Localvariables• Previousstackframebaseaddress• Functionarguments• Returnvalue• Returnaddress
• Savedregisters• Spilledtemporaries
main
0xFFFFFFFF
function1
function2
![Page 27: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/27.jpg)
StackFrameRelationships
• Iffunction1callsfunction2:• function1isthecaller• function2isthecallee
• Withrespecttomain:• mainisthecaller• function1isthecallee
main
0xFFFFFFFF
function1(caller)
function2(callee)
![Page 28: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/28.jpg)
Whereshouldwestoreallthisstuff?
A. InregistersB. OntheheapC. Inthecaller’sstackframeD. Inthecallee’s stackframeE. Somewhereelse
PreviousstackframebaseaddressFunctionargumentsReturnvalueReturnaddress
![Page 29: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/29.jpg)
CallingConvention
• Youcouldstorethisstuffwhereveryouwant!• ThehardwaredoesNOTcare.• Whatmatters:everyoneagreesonwheretofindthenecessarydata.
• Callingconvention:agreeduponsystemforexchangingdatabetweencallerandcallee
![Page 30: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/30.jpg)
IA32CallingConvention(gcc)
• Inregister%eax:• Thereturnvalue
• Inthecallee’s stackframe:• Thecaller’s%ebp value(previousframepointer)
• Inthecaller’sframe(sharedwithcallee):• Functionarguments• Returnaddress(savedPCvalue)
![Page 31: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/31.jpg)
IA32CallingConvention(gcc)
• Inregister%eax:• Thereturnvalue
• Inthecallee’s stackframe:• Thecaller’s%ebp value(previousframepointer)
• Inthecaller’sframe(sharedwithcallee):• Functionarguments• Returnaddress(savedPCvalue)
![Page 32: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/32.jpg)
IA32CallingConvention(gcc)
• Inregister%eax:• Thereturnvalue
• Inthecallee’s stackframe:• Thecaller’s%ebp value(previousframepointer)
• Inthecaller’sframe(sharedwithcallee):• Functionarguments• Returnaddress(savedPCvalue)
![Page 33: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/33.jpg)
FramePointer
• Mustmaintaininvariant:• Thecurrentfunction’sstackframeisalwaysbetweentheaddressesstoredin%esp and%ebp
• Mustadjust%esp,%ebp oncall/return.
caller
%esp
%ebp …
![Page 34: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/34.jpg)
callee
FramePointer
• Mustmaintaininvariant:• Thecurrentfunction’sstackframeisalwaysbetweentheaddressesstoredin%esp and%ebp
• Immediatelyuponcallingafunction:1. pushl %ebp
caller
%esp
…%ebp
caller’s%ebp value
![Page 35: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/35.jpg)
callee
FramePointer
• Mustmaintaininvariant:• Thecurrentfunction’sstackframeisalwaysbetweentheaddressesstoredin%esp and%ebp
• Immediatelyuponcallingafunction:1. pushl %ebp2. Set%ebp =%esp
caller
%esp
…%ebp
caller’s%ebp value
![Page 36: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/36.jpg)
callee
FramePointer
• Mustmaintaininvariant:• Thecurrentfunction’sstackframeisalwaysbetweentheaddressesstoredin%esp and%ebp
• Immediatelyuponcallingafunction:1. pushl %ebp2. Set%ebp =%esp3. SubtractNfrom%esp
caller
%esp
…%ebp
caller’s%ebp value
Callee cannowexecute.
![Page 37: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/37.jpg)
callee
FramePointer
• Mustmaintaininvariant:• Thecurrentfunction’sstackframeisalwaysbetweentheaddressesstoredin%esp and%ebp
• Toreturn,reversethis:
caller
%esp
…%ebp
caller’s%ebp value
![Page 38: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/38.jpg)
FramePointer
• Mustmaintaininvariant:• Thecurrentfunction’sstackframeisalwaysbetweentheaddressesstoredin%esp and%ebp
• Toreturn,reversethis:1. set%esp =%ebp
caller
%esp
…%ebp
caller’s%ebp value
![Page 39: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/39.jpg)
FramePointer
• Mustmaintaininvariant:• Thecurrentfunction’sstackframeisalwaysbetweentheaddressesstoredin%esp and%ebp
• Toreturn,reversethis:1. set%esp =%ebp2. popl %ebp
caller
%esp
…%ebp
caller’s%ebp value
![Page 40: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/40.jpg)
FramePointer
• Mustmaintaininvariant:• Thecurrentfunction’sstackframeisalwaysbetweentheaddressesstoredin%esp and%ebp
• Toreturn,reversethis:1. set%esp =%ebp2. popl %ebp
caller
%esp
…%ebpBacktowherewestarted.
IA32hasanotherconvenienceinstructionforthis:leave
![Page 41: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/41.jpg)
Recall:AssemblyWhileLoop
some_function:pushl %ebpmovl %esp, %ebp
# Your code here
movl $10, %eaxleaveret
Setupthestackframeforthisfunction.
Storereturnvaluein%eax.
Restorecaller’s%esp,%ebp.
![Page 42: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/42.jpg)
Lab4:swap.s
swap:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
# Your code here
leave
ret
![Page 43: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/43.jpg)
IA32CallingConvention(gcc)
• Inregister%eax:• Thereturnvalue
• Inthecallee’s stackframe:• Thecaller’s%ebp value(previousframepointer)
• Inthecaller’sframe(sharedwithcallee):• Functionarguments• Returnaddress(savedPCvalue)
![Page 44: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/44.jpg)
FunctionArguments
• Argumentsarepushedontothestackbeforethecallinstructionjumpsintothecallee.
callee
caller
…
caller’s%ebp valueCallee arguments
![Page 45: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/45.jpg)
InstructionsinMemory
0x0
0xFFFFFFFF
Operatingsystem
Stack
TextData
Heap
funcA:…call funcB…
funcB:pushl %ebpmovl %esp, %ebp…
FunctionA
FunctionB
…
![Page 46: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/46.jpg)
ProgramCounter
ProgramCounter(PC)
funcA:addl $5, %ecxmovl %ecx, -4(%ebp)…call funcBaddl %eax, %ecx…
funcB:pushl %ebpmovl %esp, %ebp…movl $10, %eaxleaveret
TextMemoryRegionRecall:PCstorestheaddressofthenextinstruction.(Apointertothenextinstruction.)
Whatdowedonow?
FollowPC,fetchinstruction:
addl $5, %ecx
![Page 47: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/47.jpg)
ProgramCounter
ProgramCounter(PC)
funcA:addl $5, %ecxmovl %ecx, -4(%ebp)…call funcBaddl %eax, %ecx…
funcB:pushl %ebpmovl %esp, %ebp…movl $10, %eaxleaveret
TextMemoryRegionRecall:PCstorestheaddressofthenextinstruction.(Apointertothenextinstruction.)
Whatdowedonow?
FollowPC,fetchinstruction:
addl $5, %ecx
UpdatePCtonextinstruction.
Executetheaddl.
![Page 48: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/48.jpg)
ProgramCounter
ProgramCounter(PC)
funcA:addl $5, %ecxmovl %ecx, -4(%ebp)…call funcBaddl %eax, %ecx…
funcB:pushl %ebpmovl %esp, %ebp…movl $10, %eaxleaveret
Recall:PCstorestheaddressofthenextinstruction.(Apointertothenextinstruction.)
Whatdowedonow?
FollowPC,fetchinstruction:
movl $ecx, -4(%ebp)
TextMemoryRegion
![Page 49: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/49.jpg)
ProgramCounter
ProgramCounter(PC)
funcA:addl $5, %ecxmovl %ecx, -4(%ebp)…call funcBaddl %eax, %ecx…
funcB:pushl %ebpmovl %esp, %ebp…movl $10, %eaxleaveret
Recall:PCstorestheaddressofthenextinstruction.(Apointertothenextinstruction.)
Whatdowedonow?
FollowPC,fetchinstruction:
movl $ecx, -4(%ebp)
UpdatePCtonextinstruction.
Executethemovl.
TextMemoryRegion
![Page 50: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/50.jpg)
ProgramCounter
ProgramCounter(PC)
funcA:addl $5, %ecxmovl %ecx, -4(%ebp)…call funcBaddl %eax, %ecx…
funcB:pushl %ebpmovl %esp, %ebp…movl $10, %eaxleaveret
Recall:PCstorestheaddressofthenextinstruction.(Apointertothenextinstruction.)
Whatdowedonow?
Keepexecutinginastraightlinedownwardslikethisuntil:
Wehitajumpinstruction.Wecallafunction.
TextMemoryRegion
![Page 51: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/51.jpg)
ChangingthePC:Jump
• Onajump:• Checkconditioncodes• SetPCtoexecuteelsewhere(notnextinstruction)
• Doweeverneedtogobacktotheinstructionafterthejump?
Maybe(andifso,we’dhavealabeltojumpbackto),butusuallynot.
![Page 52: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/52.jpg)
ChangingthePC:Functions
ProgramCounter(PC)
funcA:addl $5, %ecxmovl %ecx, -4(%ebp)…call funcBaddl %eax, %ecx…
funcB:pushl %ebpmovl %esp, %ebp…movl $10, %eaxleaveret
Whatwe’dlikethistodo:
TextMemoryRegion
![Page 53: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/53.jpg)
ChangingthePC:Functions
ProgramCounter(PC)
funcA:addl $5, %ecxmovl %ecx, -4(%ebp)…call funcBaddl %eax, %ecx…
funcB:pushl %ebpmovl %esp, %ebp…movl $10, %eaxleaveret
Whatwe’dlikethistodo:
SetupfunctionB’sstack.
TextMemoryRegion
![Page 54: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/54.jpg)
ChangingthePC:Functions
ProgramCounter(PC)
funcA:addl $5, %ecxmovl %ecx, -4(%ebp)…call funcBaddl %eax, %ecx…
funcB:pushl %ebpmovl %esp, %ebp…movl $10, %eaxleaveret
Whatwe’dlikethistodo:
SetupfunctionB’sstack.
ExecutethebodyofB,produceresult(storedin%eax).
TextMemoryRegion
![Page 55: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/55.jpg)
ChangingthePC:Functions
ProgramCounter(PC)
funcA:addl $5, %ecxmovl %ecx, -4(%ebp)…call funcBaddl %eax, %ecx…
funcB:pushl %ebpmovl %esp, %ebp…movl $10, %eaxleaveret
Whatwe’dlikethistodo:
SetupfunctionB’sstack.
ExecutethebodyofB,produceresult(storedin%eax).
RestorefunctionA’sstack.
TextMemoryRegion
![Page 56: Functions and the Stack - Swarthmore College · “A” Stack • A stack is a basic data structure • Last in, first out behavior (LIFO) • Two operations • Push (add item to](https://reader035.fdocuments.in/reader035/viewer/2022071210/6021d75b89f4ab40d71fb39c/html5/thumbnails/56.jpg)
ChangingthePC:Functions
ProgramCounter(PC)
funcA:addl $5, %ecxmovl %ecx, -4(%ebp)…call funcBaddl %eax, %ecx…
funcB:pushl %ebpmovl %esp, %ebp…movl $10, %eaxleaveret
Whatwe’dlikethistodo:
Return:GobacktowhatweweredoingbeforefuncB started.
Unlikejumping,weintendtogoback!
TextMemoryRegion