0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig....

34
Kin Fun LI University of Victoria CENG 255 Introduction to Computer Architecture ISA 55 2.6 Stacks A stack (pushdown stack) a list of data elements added/removed at top end only Last-in-first-out (LIFO) Push a new element onto the stack top Pop the top element from the stack Data stack and processor stack in memory

Transcript of 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig....

Page 1: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA55

2.6 Stacks• Astack (pushdownstack)• alistofdataelements• added/removedattopendonly

• Last-in-first-out(LIFO)• Push anewelementontothestacktop• Pop thetopelementfromthestack• Datastack andprocessorstackinmemory

Page 2: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA56

2.7.1 Processor Stack• Processorhasstackpointer(SP)registerthatpointstotopoftheprocessorstack• Pushoperationinvolvestwoinstructions:

Subtract SP,SP,#4Store Rj,(SP)

• Popoperationalsoinvolvestwoinstructions:

Load Rj,(SP)Add SP,SP,#4

Page 3: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA57

Fig. 2.14 Stack in Memory

Page 4: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA58

Stack Varieties• StackGrowth:•Ascending•Descending

• Stackpointer:• Fullstack• Emptystack

Page 5: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA59

2.7 Subroutines-1• Aparticulartaskisrepeatedandexecutedmanytimesusingdifferentdata• Examples:mathematicalfunction,listsorting

• Implementtaskinasubroutine• Ratherthanreproduceentiresubroutineblockineachpartofprogram,useasubroutinecall• SpecialtypeofbranchwithCallinstruction

Page 6: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA60

Subroutines-2• Branchingtosameblockofinstructionssavesspaceinmemory

• Mustbranchbackafterexecutingthelastinstructioninsubroutine

• UseReturn instruction• Subroutinecanbecalledfromdifferentplacesintheprogram

• HowcanReturn bedonetothecorrectplace?• =>subroutinelinkage

Page 7: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA61

2.7.1 Subroutine Linkage• Simplestmethod:placenext(afterCall)instruction’saddressinlinkregister

• Call instructionperformstwooperations:

1. StoreupdatedPCcontentinlinkregister

2. Branchtotarget(subroutine)address

• Return instructionbranchestoaddressinlinkregister

Page 8: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA62

Fig. 2.16 Caller and Callee

Page 9: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA63

2.7.1 Subroutine Nesting• Asubroutineisallowedtocallanothersubroutine• =>subroutinenesting

• Linkregister=nextaddressoffirstcall• =>overwrittenaftersecondcall

• SaveLinkregister onstackbeforesecondcall• Afterreturnfromsecondsubroutine,restoreslinkregister

Page 10: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA64

2.7.2 Parameter Passing• Differentinputdata=>subroutine

• =>differentresults• Parameterpassing:Informationexchangeto/fromasubroutine

• Useregisters:Simplebutlimitedinnumber

• Alternative:useprocessorstack• Alsoforlocalvariables&registers

Page 11: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA65

2.7.3 Stack Frame• Topoftheprocessorstack• =>privateworkspaceforsubroutines

• Astackframeisallocatedonsubroutineentryanddeallocated onsubroutineexit• Aframepointer(FP)registerenablesaccesstoprivateworkspaceforcurrentsubroutine• Subroutinenesting:thestackframealsosavesreturnaddressandFPofeachcaller

Page 12: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA66

Fig. 2.20 Stack LayoutSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

Page 13: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA67

Call Process (re: Fig. 2.20)1. Callingprogrampushes4parameters2. ThenCallinstructionexecuted• =>returnaddressonstack(↑SP)

Subroutine:3. FPisaGPregisterandsavedonstackby

a. MoveFP,-(SP)b. MoveSP,FP;↑SP=↑FP=old[FP]

4. Allocatelocalvariablesspacea. Subtract #12,SP

5. UseR2,R1sosaveonstack;↑SP=old[R1]

Page 14: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA68

Return Process (re: Fig. 2.20)• BeforeReturnfromsubroutine,Callee:

1. PopR1,R22. Add#12,SP;removelocalvariables3. Popold[FP]backtoFP

;↑SP=returnaddress• AfterReturn,Caller:

1. Popparameters;↑SP=oldTOS

Page 15: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA69

Fig. 2.20 Stack Layout-CallSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack) CC-0

CC=Caller CallSC=Sub Call

Page 16: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA70

Fig. 2.20 Stack Layout-CallSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

CC-1CC=Caller CallSC=Sub Call

Page 17: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA71

Fig. 2.20 Stack Layout-CallSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

C-Call

CC=Caller CallSC=Sub Call

Page 18: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA72

Fig. 2.20 Stack Layout-CallSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

SC-3a

CC=Caller CallSC=Sub Call

Page 19: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA73

Fig. 2.20 Stack Layout-CallSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

SC-3b

CC=Caller CallSC=Sub Call

SC-3b

Page 20: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA74

Fig. 2.20 Stack Layout-CallSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

SC-4a

CC=Caller CallSC=Sub Call

SC-3b

Page 21: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA75

Fig. 2.20 Stack Layout-CallSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

SC-5

CC=Caller CallSC=Sub Call

SC-3b

Page 22: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA76

Fig. 2.20 Stack Layout-ReturnSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

SR-1

SR=Caller RetCR=Sub Ret

SC-3b

Page 23: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA77

Fig. 2.20 Stack Layout-ReturnSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

SR-2

SR=Caller RetCR=Sub Ret

SC-3b

Page 24: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA78

Fig. 2.20 Stack Layout-ReturnSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

SR=Caller RetCR=Sub Ret

SR-3SC-3b

Page 25: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA79

Fig. 2.20 Stack Layout-ReturnSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack)

SR=Caller RetCR=Sub Ret

S-RET

Page 26: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA80

Fig. 2.20 Stack Layout-ReturnSP

(stack pointer)

FP(frame pointer)

saved [R1]

saved [R0]

Stackframefor

calledsubroutineReturn address

localvar3

localvar2

localvar1

saved [FP]

Old TOS

param2

param1

param3

param4

(top-of-stack) CR-1SR=Caller RetCR=Sub Ret

Page 27: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA81

Nested Subroutines Stack Frames

FP

FP

[FP] from SUB1

2164

Stackframe

forfirst

subroutine

[R0] from Main

param3

[R3] from Main

[R2] from Main

[R1] from Main

Old TOS

2012

[FP] from Main

param1

param2

[R0] from SUB1

[R1] from SUB1

Stackframefor

secondsubroutine

Page 28: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA82

Example: Nested Subroutines Program

Main program...

2000 Move PARAM2, (SP) Place parameterson stack.2004 Move PARAM1, (SP)2008 Call SUB12012 Move (SP),RESULT Store result.2016 Add #8,SP Restorestack level.2020 next instruction

...

––

Page 29: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA83

Nested Subroutines Sub-1

2100 SUB1 Move FP, (SP) Save frame pointer register.2104 Move SP,FP Load the framepointer.2108 MoveMultiple R0 R3, (SP) Save registers.2112 Move 8(FP),R0 Get first parameter.

Move 12(FP),R1 Get secondparameter....Move PARAM3, (SP) Placeaparameteron stack.

2160 Call SUB22164 Move (SP)+,R2 Pop SUB2 result into R2.

...Move R3,8(FP) Placeansweron stack.MoveMultiple (SP)+,R0 R3 Restoreregisters.Move (SP)+,FP Restoreframe pointer register.Return Return to Main program.

– –

Page 30: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA84

Nested Subroutines Sub-2

3000 SUB2 Move FP, (SP) Save frame pointer register.Move SP,FP Load the framepointer.MoveMultiple R0 R1, (SP) Save registersR0 and R1.Move 8(FP),R0 Get the parameter....Move R1,8(FP) PlaceSUB2result on stack.MoveMultiple (SP)+,R0 R1 RestoreregistersR0 and R1.Move (SP)+,FP Restoreframe pointer register.Return Return to Subroutine 1.

– –

Page 31: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA85

Nested Subroutines Stack Frames

FP

FP

[FP] from SUB1

2164

Stackframe

forfirst

subroutine

[R0] from Main

param3

[R3] from Main

[R2] from Main

[R1] from Main

Old TOS

2012

[FP] from Main

param1

param2

[R0] from SUB1

[R1] from SUB1

Stackframefor

secondsubroutine

2000 (SP)2004200821002104 (FP1)

2108

21128

2116

12

2156216030003004 (FP2)

3008

30128

30xx

30xx+430xx+830xx+122164

21yy

21yy+421yy+821yy+12/2012

2016

Page 32: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA86

Branch with Link

Page 33: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA87

Assignment: Stack Varieties• StackGrowth:•Ascending•Descending

• Stackpointer:• Fullstack• Emptystack

Page 34: 0-CENG255 Ch2 17-v3 - ece.uvic.cakinli/ceng255/CENG255_Ch2_17-PartIV-revised.pdf · ISA 77 Fig. 2.20 Stack Layout-Return SP (stack pointer) FP (frame pointer) saved [R1] saved [R0]

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA88

Time and Space Tradeoff