Top Banner
CS 110 Computer Architecture Lecture 6: More RISC-V, RISC-V Functions Instructor: Sören Schwertfeger http://shtech.org/courses/ca/ School of Information Science and Technology SIST ShanghaiTech University 1 Slides based on UC Berkley's CS61C
55

CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

May 23, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

CS110ComputerArchitecture

Lecture6:MoreRISC-V,RISC-VFunctions

Instructor:SörenSchwertfeger

http://shtech.org/courses/ca/

School of Information Science and Technology SIST

ShanghaiTech University

1Slides based on UC Berkley's CS61C

Page 2: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

LevelsofRepresentation/Interpretation

lw xt0,0(x2)lw xt1,4(x2)sw xt1,0(x2)sw xt0,4(x2)

HighLevelLanguageProgram(e.g.,C)

AssemblyLanguageProgram(e.g.,RISC-V)

MachineLanguageProgram(RISC-V)

HardwareArchitectureDescription(e.g.,blockdiagrams)

Compiler

Assembler

MachineInterpretation

temp=v[k];v[k]=v[k+1];v[k+1]=temp;

0000 1001 1100 0110 1010 1111 0101 10001010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111

ArchitectureImplementation

Anythingcanberepresentedasanumber,

i.e.,dataorinstructions

2

LogicCircuitDescription(CircuitSchematicDiagrams)

Page 3: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Processor

Control

Datapath

Review:ComponentsofaComputer

3

ProgramCounter

Registers

Arithmetic&LogicUnit(ALU)

MemoryInput

Output

Bytes

Enable?Read/Write

Address

WriteData

ReadData

Processor-MemoryInterface I/O-MemoryInterfaces

Program

Data

Page 4: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Lastlecture• InRISC-VAssemblyLanguage:

– RegistersreplaceCvariables– Oneinstruction(simpleoperation)perline– SimplerisBetter,SmallerisFaster

• InRV32,wordsare32bit• Instructions:

add, addi, sub, lw, sw, lb• Registers:

– 32registers,referredtoasx0 – x31– Zero:x0

4

Page 5: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

RISC-V:LittleEndianADDR3 ADDR2 ADDR1 ADDR0BYTE3 BYTE2 BYTE1 BYTE0

00000000 00000000 00000100 00000001• Hexadecimal number:

0xFD34AB88 (4,248,087,432ten) =>– Byte 0: 0x88 (136ten)– Byte 1: 0xAB (171ten)– Byte 2: 0x34 (52ten)– Byte 3: 0xFD (253ten)

• Little Endian: The ”Endianess” is little:– It starts with the smallest (least significant) Byte– Swapped from how we write the number

04812…

15913…

261014…

371115…

LittleEndianMostsignificantbyteinaword(numbersareaddresses)

5

0x8864

0xAB65

0x3466

0xFD67

Data:

Address: 64 addressofword(e.g.int)Address:

(E.g.,1025=0x401=0b010000000001)

Page 6: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

6

PeerInstruction

We want to translate *x = *y into RISC-Vx, y ptrs stored in: x3 x5

1: add x3, x5, zero2: add x5, x3, zero3: lw x3, 0(x5)4: lw x5, 0(x3)5: lw x8, 0(x5)6: sw x8, 0(x3)7: lw x5, 0(x8)8: sw x3, 0(x8)

12345®66®57®8

ABCDEFG

Page 7: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

RISC-VLogicalInstructions

Logical operations

Coperators

Java operators

RISC-Vinstructions

Bit-by-bit AND & & andBit-by-bit OR | | orBit-by-bit XOR ^ ^ xorBit-by-bit NOT ~ ~ xoriShift left << << sllShift right >> >> srl 7

• Usefultooperateonfieldsofbitswithinaword− e.g.,characterswithinaword(8bits)

• Operationstopack/unpackbitsintowords• Calledlogicaloperations

Page 8: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

RISC-VLogicalInstructions

• Alwaystwovariants– Register: and x5, x6, x7 # x5 = x6 & x7– Immediate: andi x5, x6, 3 # x5 = x6 & 3

• Usedfor‘masks’

– andi with0000 00FFhex isolatestheleastsignificantbyte– andi withFF00 0000hex isolatesthemostsignificantbyte

– andi with0000 0008hex isolatesthe4th bit(00001000two )

Page 9: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

YourTurn.Whatisinx11?

xor x11, x10, x10ori x11, x11, 0xFFandi x11, x11, 0xF0

0x0

0xF

0xF0

0xFF00

0xFFFFFFFF

A:B:C:D:E:

Page 10: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Logic Shifting• ShiftLeft:sll x11,x12,2 #x11=x12<<2

– Storeinx11 thevaluefromx12 shifted2bitstotheleft(theyfalloffend),inserting0’s onright;<<inC.Before:00000002hex00000000000000000000000000000010twoAfter: 00000008hex00000000000000000000000000001000two

Whatarithmeticeffectdoesshiftlefthave?multiplywith2n

• ShiftRight:srl isoppositeshift;>>

10

Page 11: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

ArithmeticShifting• Shiftrightarithmeticmovesn bitstotheright(inserthighordersignbitintoemptybits)

• Forexample,ifregisterx10contained11111111111111111111111111100111two=-25ten

• Ifexecutedsra x10,x10,4,resultis:11111111111111111111111111111110two=-2ten

• Unfortunately,thisisNOTsameasdividingby2n− Failsforoddnegativenumbers− Carithmeticsemanticsisthatdivisionshouldroundtowards0

11

Page 12: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

YourTurn.Whatisinx12?

x10 holds 0x34FF

slli x12, x10, 0x10srli x12, x12, 0x08and x12, x12, x10

0x0

0x3400

0x4F0

0xFF00

0x34FF

A:B:C:D:E:

Page 13: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

HelpfulRISC-VAssemblerFeatures

• Symbolicregisternames– E.g.,a0-a7 forargumentregisters(x10-x17)– E.g.,zero forx0– E.g.,t0-t6 (temporary) s0-s11 (saved)

• Pseudo-instructions– Shorthandsyntaxforcommonassemblyidioms– E.g., mv rd, rs = addi rd, rs, 0– E.g., li rd, 13 = addi rd, x0, 13

Page 14: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

ComputerDecisionMaking

• Basedoncomputation,dosomethingdifferent• Inprogramminglanguages:if-statement

• RISC-V:if-statementinstructionisbeq register1, register2, L1

means:gotostatementlabeledL1if(valueinregister1)==(valueinregister2)….otherwise,gotonextstatement

• beq standsforbranchifequal• Otherinstruction:bne forbranchifnotequal 14

Page 15: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

TypesofBranches

• Branch – changeofcontrolflow

• ConditionalBranch – changecontrolflowdependingonoutcomeofcomparison– branchifequal(beq)orbranchifnot equal(bne)– Alsobranchiflessthan(blt)andbranchifgreaterthanorequal(bge)

• UnconditionalBranch – alwaysbranch– aRISC-Vinstructionforthis:jump (j),asinj label

15

Page 16: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Label• Holdstheaddressofdataorinstructions

– Think:”constantpointer”– Willbereplacedbytheactualaddress(number)duringassembly(orlinking)

• AlsoavailableinCfor”goto”:

• NEVER usegoto !!!!Verybadprogrammingstyle! 16

Page 17: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

17

Label

Page 18: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Exampleif Statement• Assumingtranslationsbelow,compileif blockf→x10 g→x11 h→x12i →x13 j→x14

if (i == j) bne x13,x14,Exitf = g + h; add x10,x11,x12

Exit:

• Mayneedtonegatebranchcondition18

Page 19: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Exampleif-else Statement

• Assumingtranslationsbelow,compilef→x10 g→x11 h→x12i →x13 j→x14

if (i == j) bne x13,x14,Else f = g + h; add x10,x11,x12

else j Exit f = g – h; Else: sub x10,x11,x12

Exit:

19

Page 20: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Magnitude Compares in RISC-V

• Untilnow,we’veonlytestedequalities(==and!=inC);Generalprogramsneedtotest<and>aswell.

• RISC-Vmagnitude-comparebranches:“BranchonLessThan”Syntax:blt reg1,reg2, labelMeaning: if(reg1<reg2)//treatregistersassignedintegers

goto label;• “BranchonLessThanUnsigned”

Syntax:bltu reg1,reg2, labelMeaning: if(reg1<reg2)//treatregistersasunsignedintegers

goto label;

20

Page 21: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

CLoopMappedtoRISC-VAssembly

int A[20];int sum = 0;for (int i=0; i < 20; i++)

sum += A[i];

add x9, x8, x0 # x9=&A[0]add x10, x0, x0 # sum=0add x11, x0, x0 # i=0addi x13,x0, 20 # x13=20

Loop:bge x11,x13,Donelw x12, 0(x9) # x12=A[i]add x10,x10,x12 # sum+=addi x9, x9,4 # &A[i+1]addi x11,x11,1 # i++j Loop

Done:

21

Page 22: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Administrivia• HW2Autolab isonline– dueinoneweek!March14

– STARTlatesttoday!– GotoOHifyouhaveproblems– don’taskyourfellow

students– Usepiazzafrequently.

• HW3andProject1.1willbepublishedthisweekend!

• MidtermIisinonemonthduringlecturehours…(April3rd)

22

Page 23: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Schedule2weeksforHWandProjects;startearly!

23

Page 24: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

ProceduresinRISC-V

24

Page 25: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

HowProgramisStored

25

Memory

Bytes

Program

Data

OneRISC-VInstruction=32bits

Page 26: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

AssemblertoMachineCode(morelaterincourse)

26

foo.S bar.S

Assembler Assembler

foo.o bar.o

Linker lib.o

a.out

Assemblersourcefiles(text)

Machinecodeobjectfiles

Pre-builtobjectfilelibraries

Machinecodeexecutablefile

Assemblerconvertshuman-readableassemblycodetoinstructionbitpatterns

Page 27: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Processor

Control

Datapath

ExecutingaProgram

27

PC

Registers

Arithmetic&LogicUnit(ALU)

Memory

BytesInstructionAddress

ReadInstructionBits

Program

Data

• ThePC(programcounter)isinternalregisterinsideprocessorholdingbyteaddressofnextinstructiontobeexecuted.

• Instructionisfetchedfrommemory,thencontrolunitexecutesinstructionusingdatapath andmemorysystem,andupdatesprogramcounter(defaultisadd+4bytestoPC,tomovetonextsequentialinstruction)

Page 28: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

CFunctionsmain() {

int i,j,k,m;...i = mult(j,k); ... m = mult(i,i); ...

}

/* really dumb mult function */int mult (int mcand, int mlier){

int product = 0;while (mlier > 0) {product = product + mcand;mlier = mlier -1; }

return product;}

What information mustcompiler/programmer

keep track of?

What instructions can accomplish this?

Page 29: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

SixFundamentalStepsinCallingaFunction

1. Putparametersinaplacewherefunctioncanaccessthem

2. Transfercontroltofunction3. Acquire(local)storageresourcesneededfor

function4. Performdesiredtaskofthefunction5. Putresultvalueinaplacewherecallingcode

canaccessitandrestoreanyregistersyouused6. Returncontroltopointoforigin,sincea

functioncanbecalledfromseveralpointsinaprogram

29

Page 30: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

RISC-VFunctionCallConventions

• Registersfasterthanmemory,sousethem

• a0–a7 (x10-x17):eightargumentregisterstopassparametersandreturnvalues(a0-a1)

• ra:onereturnaddressregistertoreturntothepointoforigin(x1)

• Alsos0-s1 (x8-x9) ands2-s11 (x18-x27):savedregisters(moreaboutthoselater)

30

Page 31: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

InstructionSupportforFunctions(1/4)

... sum(a,b);... /* a, b: s0, s1 */}int sum(int x, int y) {return x+y;

}address (shown in decimal)1000 1004 1008 1012 1016 …2000 2004

C

InRISC-V,allinstructionsare4bytes,andstoredinmemoryjustlikedata.Sohereweshowtheaddressesofwheretheprogramsarestored.

31

RIS

C-V

Page 32: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

InstructionSupportforFunctions(2/4)

... sum(a,b);... /* a, b: s0, s1 */}int sum(int x, int y) {return x+y;

}address (shown in decimal)1000 add a0, s0, x0 # x = a1004 mv a1, s1 # y = b1008 addi ra, zero, 1016 # ra=10161012 j sum # jump to sum1016 … # next instruction…2000 sum: add a0, a0, a12004 jr ra # new instr. “jump register”

C

32

RIS

C-V

Page 33: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

InstructionSupportforFunctions(3/4)

... sum(a,b);... /* a,b:$s0,$s1 */}int sum(int x, int y) {return x+y;

}

2000 sum: add a0, a0, a12004 jr ra # new instr. “jump register”

• Question:Whyuse jr here?Whynot usej?

• Answer:summightbecalledbymanyplaces,sowecan’treturntoafixedplace.Thecallingproctosummustbeabletosay“returnhere”somehow.

C

33

RIS

C-V

Page 34: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

InstructionSupportforFunctions(4/4)• Singleinstructiontojumpandsavereturnaddress:jumpandlink(jal)

• Before:1008 addi ra, zero, 1016 # $ra=10161012 j sum # goto sum

• After:1008 jal sum # ra=1012, goto sum

• Whyhaveajal?– Makethecommoncasefast:functioncalls verycommon.– Reduceprogramsize– Don’thavetoknowwhere codeis inmemorywithjal!

34

Page 35: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

RISC-VFunctionCallInstructions• Invokefunction:jumpandlinkinstruction(jal)

(reallyshouldbelaj “linkandjump”)– “link”meansformanaddressorlinkthatpointstocallingsitetoallowfunctiontoreturntoproperaddress

– Jumpstoaddressandsimultaneouslysavestheaddressofthefollowing instructioninregisterra (x1)jal FunctionLabel

• Returnfromfunction:jumpregisterinstruction(jr)– Unconditionaljumptoaddressspecifiedinregisterjr ra

– Assemblershorthand: ret = jr ra35

Page 36: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

NotesonFunctions• Callingprogram(caller)putsparametersintoregistersa0-a7 andusesjal X toinvoke(callee)ataddresslabeledX

• Musthaveregisterincomputerwithaddressofcurrentlyexecutinginstruction– InsteadofInstructionAddressRegister (bettername),historicallycalledProgramCounter (PC)

– It’saprogram’scounter;itdoesn’tcountprograms!

• Whatvaluedoesjal X placeintora?????• jr ra putsaddressinsidera backintoPC

36

Page 37: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

WhereAreOldRegisterValuesSavedtoRestoreThemAfterFunctionCall?

• Needaplacetosaveoldvaluesbeforecallfunction,restorethemwhenreturn,anddelete

• Idealisstack:last-in-first-outqueue(e.g.,stackofplates)– Push:placingdataontostack– Pop:removingdatafromstack

• Stackinmemory,soneedregistertopointtoit• sp isthestackpointerinRISC-V(x2)• Conventionisgrowfromhightolowaddresses

– Push decrementssp,Pop incrementssp

37

Page 38: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Stack

• Stackframeincludes:• Return“instruction”address• Parameters• Spaceforotherlocalvariables

• Stackframescontiguousblocksofmemory;stackpointertellswherebottomofstackframeis

• Whenprocedureends,stackframeistossedoffthestack;freesmemoryforfuturestackframes

frame

frame

frame

frame

$sp

0xBFFFFFF0

Page 39: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Exampleint Leaf(int g, int h, int i, int j)

{int f;f = (g + h) – (i + j);return f;

}• Parametervariablesg,h,i,andj inargumentregistersa0,a1,a2,anda3,andf ins0

• Assumeneedonetemporaryregisters1

39

Page 40: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

StackBefore,During,AfterFunction

• Needtosaveoldvaluesofs0 ands1

sp

Beforecall

spSaved s1

Duringcall

Saved s0

sp

Aftercall

Saved s1Saved s0

Page 41: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

RISC-VCodeforLeaf()

41

Leaf: addi sp, sp, -8 # adjust stack for 2 itemssw s1, 4(sp) # save s1 for use afterwardssw s0, 0(sp) # save s0 for use afterwards

add s0, a0, a1 # f = g + hadd s1, a2, a3 # s1 = i + jsub a0, s0, s1 # return value (g + h) – (i + j)

lw s0, 0(sp) # restore register s0 for caller lw s1, 4(sp) # restore register s1 for calleraddi sp, sp, 8 # adjust stack to delete 2 itemsjr ra # jump back to calling routine

Page 42: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

NestedProcedures(1/2)

int sumSquare(int x, int y) {return mult(x,x)+ y;

}• SomethingcalledsumSquare,nowsumSquare iscallingmult

• Sothere’savalueinra thatsumSquarewantstojumpbackto,butthiswillbeoverwrittenbythecalltomult

42

NeedtosavesumSquare returnaddressbeforecalltomult

Page 43: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

NestedProcedures(2/2)

• Ingeneral,mayneedtosavesomeotherinfoinadditiontora.

• WhenaCprogramisrun,thereare3importantmemoryareasallocated:– Static:Variablesdeclaredonceperprogram,ceasetoexistonlyafterexecutioncompletes- e.g.,Cglobals

– Heap:Variablesdeclareddynamicallyviamalloc– Stack:Spacetobeusedbyprocedureduringexecution;thisiswherewecansaveregistervalues

43

Page 44: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

RegisterConventions(1/2)

• CalleR:thecallingfunction• CalleE:thefunctionbeingcalled• Whencallee returnsfromexecuting,thecallerneedstoknowwhichregistersmayhavechangedandwhichareguaranteedtobeunchanged.

• RegisterConventions:Asetofgenerallyacceptedrulesastowhichregisterswillbeunchangedafteraprocedurecall(jal)andwhichmaybechanged.

Page 45: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

RegisterConventions(2/2)

Toreduceexpensiveloadsandstoresfromspillingandrestoringregisters,RISC-Vfunction-callingconventiondividesregistersintotwocategories:

1. Preservedacrossfunctioncall– Callercanrelyonvaluesbeingunchanged– sp,gp,tp, “savedregisters”s0- s11 (s0 isalso fp)

2. Notpreservedacrossfunctioncall– Callercannotrelyonvaluesbeingunchanged– Argument/returnregistersa0-a7,ra,

“temporaryregisters”t0-t6

Page 46: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

RISC-VSymbolicRegisterNamesNumbers:hardwareunderstands

Human-friendlysymbolicnamesinassemblycode

Page 47: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

Question

• WhichstatementisFALSE?

47

B: jal savesPC+1inra

C: Thecallee canusetemporaryregisters(ti)withoutsavingandrestoringthem

D: Thecallercanrelyonsaveregisters(si)withoutfearofcallee changingthem

A:RISC-Vusesjal toinvokeafunctionandjr toreturnfromafunction

Page 48: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

AllocatingSpaceonStack

• Chastwostorageclasses:automaticandstatic– Automatic variablesarelocaltofunctionanddiscardedwhenfunctionexits

– Staticvariablesexistacrossexitsfromandentriestoprocedures

• Usestackforautomatic(local)variablesthatdon’tfitinregisters

• Procedureframeor activationrecord:segmentofstackwithsavedregistersandlocalvariables

48

Page 49: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

StackBefore,During,AfterFunction

sp

Beforecall

sp

Duringcall

Savedargumentregisters(ifany)

Savedreturnaddress(ifneeded)

Savedsavedregisters(ifany)

Localvariables(ifany)

sp

Aftercall

Page 50: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

UsingtheStack(1/2)

• Wehavearegistersp whichalwayspointstothelastusedspaceinthestack.

• Tousestack,wedecrementthispointerbytheamountofspaceweneedandthenfillitwithinfo.

• So,howdowecompilethis?int sumSquare(int x, int y) {

return mult(x,x)+ y;}

50

Page 51: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

UsingtheStack(2/2)

sumSquare: addi sp, sp, -8 # space on stacksw ra, 4(sp) # save ret addrsw a1, 0(sp) # save ymv a1, a0 # mult(x,x)jal mult # call multlw a1, 0(sp) # restore yadd a0, a0, a1 # mult()+ylw ra, 4(sp) # get ret addraddi sp, sp, 8 # restore stackjr ra

mult: ...

int sumSquare(int x, int y) {return mult(x,x)+ y; }

“push”

“pop”

Page 52: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

BasicStructureofaFunction

entry_label: addi sp,sp, -framesizesw ra, framesize-4(sp) # save rasave other regs if need be

...

restore other regs if need belw ra, framesize-4(sp) # restore $raaddi sp, sp, framesizejr ra

Epilogue

Prologue

Body (call other functions…)

ra

memory

52

Page 53: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

WhereistheStackinMemory?• RV32convention(RV64andRV128havedifferentmemorylayouts)• Stackstartsinhighmemoryandgrowsdown

– Hexadecimal:bfff_fff0hex

– Stackmustbealignedon16-byteboundary(nottrueinexamplesabove)

• RV32programs(textsegment)inlowend– 0001_0000hex

• staticdatasegment(constantsandotherstaticvariables)abovetextforstaticvariables– RISC-Vconventionglobalpointer(gp)pointstostatic– RV32gp =1000_0000hex

• Heapabovestaticfordatastructuresthatgrowandshrink;growsuptohighaddresses

Page 54: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

RV32MemoryAllocation

Page 55: CS 110 Computer Architecture Lecture 6...foo.S bar.S Assembler Assembler foo.o bar.o Linker lib.o a.out Assembler source files (text) Machine code object files Pre-built object file

“AndinConclusion…”• Registersweknowsofar(Allofthem!)

– a0-a7forfunctionarguments,a0-a1forreturnvalues– sp,stackpointer,ra returnaddress– s0-s11savedregisters– t0-t6temporaries– zero

• Instructionsweknow:– Arithmetic:add,addi,sub– Logical:sll,srl,sla,slli,srli,slai,and,or,xor,andi,ori,xori– Decision:beq,bne,blt,bge– Unconditionalbranches(jumps):j,jr– Functionscalledwithjal,returnwithjr ra.

• Thestackisyourfriend:Useittosaveanythingyouneed.Justleaveitthewayyoufoundit!