0-ceng255 ch2 17-v3 - ece.uvic.cakinli/ceng255/ceng255_ch2_17-partiv-revised.pdf · isa 77 fig....

Post on 27-Mar-2018

216 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA55

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

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

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

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA57

Fig. 2.14 Stack in Memory

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA58

Stack Varieties• StackGrowth:•Ascending•Descending

• Stackpointer:• Fullstack• Emptystack

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA59

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

• Implementtaskinasubroutine• Ratherthanreproduceentiresubroutineblockineachpartofprogram,useasubroutinecall• SpecialtypeofbranchwithCallinstruction

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA60

Subroutines-2• Branchingtosameblockofinstructionssavesspaceinmemory

• Mustbranchbackafterexecutingthelastinstructioninsubroutine

• UseReturn instruction• Subroutinecanbecalledfromdifferentplacesintheprogram

• HowcanReturn bedonetothecorrectplace?• =>subroutinelinkage

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA61

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

• Call instructionperformstwooperations:

1. StoreupdatedPCcontentinlinkregister

2. Branchtotarget(subroutine)address

• Return instructionbranchestoaddressinlinkregister

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA62

Fig. 2.16 Caller and Callee

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA63

2.7.1 Subroutine Nesting• Asubroutineisallowedtocallanothersubroutine• =>subroutinenesting

• Linkregister=nextaddressoffirstcall• =>overwrittenaftersecondcall

• SaveLinkregister onstackbeforesecondcall• Afterreturnfromsecondsubroutine,restoreslinkregister

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA64

2.7.2 Parameter Passing• Differentinputdata=>subroutine

• =>differentresults• Parameterpassing:Informationexchangeto/fromasubroutine

• Useregisters:Simplebutlimitedinnumber

• Alternative:useprocessorstack• Alsoforlocalvariables&registers

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA65

2.7.3 Stack Frame• Topoftheprocessorstack• =>privateworkspaceforsubroutines

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

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)

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]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

...

––

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.

– –

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.

– –

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

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA86

Branch with Link

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA87

Assignment: Stack Varieties• StackGrowth:•Ascending•Descending

• Stackpointer:• Fullstack• Emptystack

KinFunLIUniversityofVictoria

CENG255IntroductiontoComputerArchitecture

ISA88

Time and Space Tradeoff

top related