functions and the stack - swarthmore college · “a” stack • a stack is a basic data structure...

56
Functions and the Stack 10/4/16

Upload: others

Post on 02-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

“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

• 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

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

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

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

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

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

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

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

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

• 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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