cs 110 computer architecture
TRANSCRIPT
CS110ComputerArchitectureMIPSInstructionFormats
Instructor:SĂśrenSchwertfeger
http://shtech.org/courses/ca/
School of Information Science and Technology SIST
ShanghaiTech University
1Slides based on UC Berkley's CS61C
LevelsofRepresentation/Interpretation
lw $t0,0($2)lw $t1,4($2)sw $t1,0($2)sw $t0,4($2)
HighLevelLanguageProgram(e.g.,C)
AssemblyLanguageProgram(e.g.,MIPS)
MachineLanguageProgram(MIPS)
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)
Review:AllocatingSpaceonStack⢠Chastwostorageclasses:automaticandstaticâ Automatic variablesarelocaltofunctionanddiscardedwhenfunctionexits
â Staticvariablesexistacrossexitsfromandentriestoprocedures
⢠Usestackforautomatic(local)variablesthatdonâtfitinregisters
⢠Procedureframeor activationrecord:segmentofstackwithsavedregistersandlocalvariables
⢠SomeMIPScompilersuseaframepointer($fp)topointtofirstwordofframe
3
StackBefore,During,AfterCall
4
RecursiveFunctionFactorialint fact (int n)
{
if (n < 1) return (1);
else return (n * fact(n-1));
}
5
RecursiveFunctionFactorialFact:
# adjust stack for 2 itemsaddi $sp,$sp,-8# save return addresssw $ra, 4($sp) # save argument nsw $a0, 0($sp) # test for n < 1slti $t0,$a0,1 # if n >= 1, go to L1beq $t0,$zero,L1# Then part (n==1) return 1addi $v0,$zero,1 # pop 2 items off stackaddi $sp,$sp,8 # return to callerjr $ra
L1: # Else part (n >= 1) # arg. gets (n â 1)addi $a0,$a0,-1 # call fact with (n â 1)jal Fact # return from jal: restore nlw $a0, 0($sp)# restore return addresslw $ra, 4($sp) # adjust sp to pop 2 itemsaddi $sp, $sp,8 # return n * fact (n â 1)mul $v0,$a0,$v0# return to the callerjr $ra
6mul isapseudoinstruction
LevelsofRepresentation/Interpretation
lw $t0,0($2)lw $t1,4($2)sw $t1,0($2)sw $t0,4($2)
HighLevelLanguageProgram(e.g.,C)
AssemblyLanguageProgram(e.g.,MIPS)
MachineLanguageProgram(MIPS)
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
7
LogicCircuitDescription(CircuitSchematicDiagrams)
ENIAC(U.Penn.,1946)FirstElectronicGeneral-PurposeComputer
8
⢠Blazinglyfast(multiplyin2.8ms!)â 10decimaldigitsx10decimaldigits
⢠Butneeded2-3daystosetupnewprogram,asprogrammedwithpatchcordsandswitches
BigIdea:Stored-Program
Computer
â Instructionsarerepresentedasbitpatterns- canthinkoftheseasnumbers
â Therefore,entireprogramscanbestoredinmemorytobereadorwrittenjustlikedata
â Canreprogramquickly(seconds),donâthavetorewirecomputer(days)
â KnownastheâvonNeumannâcomputersafterwidelydistributedtechreportonEDVACproject⢠Wrote-updiscussionsofEckertandMauchly⢠AnticipatedearlierbyTuringandZuse
FirstDraftofaReportontheEDVACby
JohnvonNeumannContractNo.Wâ670âORDâ4926
BetweentheUnitedStatesArmyOrdnanceDepartmentandthe
UniversityofPennsylvaniaMooreSchoolofElectricalEngineering
UniversityofPennsylvania
June 30,1945
9
Consequence#1:EverythingAddressed
⢠Sinceallinstructionsanddataarestoredinmemory,everythinghasamemoryaddress:instructions,datawordsâ bothbranchesandjumpsusethese
⢠Cpointersarejustmemoryaddresses:theycanpointtoanythinginmemoryâ Unconstraineduseofaddressescanleadtonastybugs;upto
youinC;limitedinJavabylanguagedesign⢠Oneregisterkeepsaddressofinstructionbeingexecuted:
âProgramCounterâ(PC)â Basicallyapointertomemory:IntelcallsitInstructionPointer(a
bettername)
10
Consequence#2:BinaryCompatibility
⢠Programsaredistributedinbinaryformâ Programsboundtospecificinstructionsetâ DifferentversionforMacintoshesandPCs
⢠Newmachineswanttorunoldprograms(âbinariesâ)aswellasprogramscompiledtonewinstructions
⢠Leadstoâbackward-compatibleâinstructionsetevolvingovertime
⢠SelectionofIntel8086in1981for1st IBMPCismajorreasonlatestPCsstilluse80x86instructionset;couldstillrunprogramfrom1981PCtoday
11
InstructionsasNumbers(1/2)
⢠Currentlyalldataweworkwithisinwords(32-bitchunks):â Eachregisterisaword.â lw andsw bothaccessmemoryonewordatatime.
⢠Sohowdowerepresentinstructions?â Remember:Computeronlyunderstands1sand0s,soâadd $t0,$0,$0âismeaningless.
â MIPS/RISCseekssimplicity:sincedataisinwords,makeinstructionsbefixed-size32-bitwordsalso
12
InstructionsasNumbers(2/2)
⢠Onewordis32bits,sodivideinstructionwordintoâfieldsâ.
⢠Eachfieldtellsprocessorsomethingaboutinstruction.
⢠Wecoulddefinedifferentfieldsforeachinstruction,butMIPSseekssimplicity,sodefine3basictypesofinstructionformats:â R-formatâ I-formatâ J-format
13
InstructionFormats
⢠I-format:usedforinstructionswithimmediates,lw andsw (sinceoffsetcountsasanimmediate),andbranches(beq andbne)â (butnottheshiftinstructions;later)
⢠J-format:usedforj andjal⢠R-format:usedforallotherinstructions⢠Itwillsoonbecomeclearwhytheinstructionshavebeenpartitionedinthisway
14
R-FormatInstructions(1/5)
⢠Defineâfieldsâofthefollowingnumberofbitseach:6+5+5+5+5+6=32
⢠Forsimplicity,eachfieldhasaname:
⢠Important:Ontheseslidesandinbook,eachfieldisviewedasa5- or6-bitunsignedinteger,notaspartofa32-bitintegerâ Consequence:5-bitfieldscanrepresentanynumber0-31,while
6-bitfieldscanrepresentanynumber0-63
6 5 5 5 65
opcode rs rt rd functshamt
15
R-FormatInstructions(2/5)
⢠Whatdothesefieldintegervaluestellus?â opcode:partiallyspecifieswhatinstructionitis⢠Note:Thisnumberisequalto0 forallR-Formatinstructions
â funct:combinedwithopcode,thisnumberexactlyspecifiestheinstruction
16
⢠Question:Whyarenâtopcode andfunct asingle12-bitfield?âWeâllanswerthislater
R-FormatInstructions(3/5)
⢠Morefields:â rs (SourceRegister):usually usedtospecifyregistercontainingfirstoperand
â rt (TargetRegister):usually usedtospecifyregistercontainingsecondoperand(notethatnameismisleading)
â rd (DestinationRegister):usually usedtospecifyregisterwhichwillreceiveresultofcomputation
17
R-FormatInstructions(4/5)
⢠Notesaboutregisterfields:â Eachregisterfieldisexactly5bits,whichmeansthatitcanspecifyanyunsignedintegerintherange0-31.Eachofthesefieldsspecifiesoneofthe32registersbynumber.
â Thewordâusuallyâwasusedbecausethereareexceptionsthatweâllseelater
18
R-FormatInstructions(5/5)⢠Finalfield:â shamt:Thisfieldcontainstheamountashiftinstructionwillshiftby.Shiftinga32-bitwordbymorethan31isuseless,sothisfieldisonly5bits(soitcanrepresentthenumbers0-31)
â Thisfieldissetto0 inallbuttheshiftinstructions
⢠Foradetaileddescriptionoffieldusageforeachinstruction,seegreeninsertinCOD(Youcanbringwithyoutoallexams)
19
R-FormatExample(1/2)⢠MIPSInstruction:
add $8,$9,$10
opcode =0(lookupintableinbook)funct =32(lookupintableinbook)rd =8(destination)rs =9(firstoperand)rt =10(secondoperand)shamt =0(notashift)
20
R-FormatExample(2/2)⢠MIPSInstruction:
add $8,$9,$10Decimalnumberperfieldrepresentation:
Binarynumberperfieldrepresentation:
hexrepresentation: 012A 4020hex
CalledaMachineLanguageInstruction
0 9 10 8 320
000000 01001 01010 01000 10000000000hex
21
opcode rs rt rd functshamt
= 19,546,144ten
I-FormatInstructions(1/4)
⢠Whataboutinstructionswithimmediates?â 5-bitfieldonlyrepresentsnumbersuptothevalue31:immediatesmaybemuchlargerthanthis
â Ideally,MIPSwouldhaveonlyoneinstructionformat(forsimplicity):unfortunately,weneedtocompromise
⢠DefinenewinstructionformatthatispartiallyconsistentwithR-format:â Firstnoticethat,ifinstructionhasimmediate,thenitusesatmost2registers.
22
I-FormatInstructions(2/4)⢠Defineâfieldsâofthefollowingnumberofbitseach:6+5+5+16=32bits
â Again,eachfieldhasaname:
â KeyConcept:OnlyonefieldisinconsistentwithR-format.Mostimportantly,opcode isstillinsamelocation.
6 5 5 16
opcode rs rt immediate
23
I-FormatInstructions(3/4)⢠Whatdothesefieldsmean?â opcode:sameasbeforeexceptthat,sincethereâsnofunct field,opcode uniquelyspecifiesaninstructioninI-format
â ThisalsoanswersquestionofwhyR-formathastwo6-bitfieldstoidentifyinstructioninsteadofasingle12-bitfield:inordertobeconsistentaspossiblewithotherformatswhileleavingasmuchspaceaspossibleforimmediatefield.
â rs:specifiesaregisteroperand(ifthereisone)â rt:specifiesregisterwhichwillreceiveresultofcomputation(thisiswhyitâscalledthetarget registerârtâ)orotheroperandforsomeinstructions.
24
I-FormatInstructions(4/4)⢠TheImmediateField:â addi,slti,sltiu,theimmediateissign-extended to32bits.Thus,itâstreatedasasignedinteger.
â 16bitsè canbeusedtorepresentimmediateupto216 differentvalues
â Thisislargeenoughtohandletheoffsetinatypicallw orsw,plusavastmajorityofvaluesthatwillbeusedintheslti instruction.
â Later,weâllseewhattodowhenavalueistoobigfor16bits
25
I-FormatExample(1/2)⢠MIPSInstruction:
addi $21,$22,-50
opcode =8(lookupintableinbook)rs =22(registercontainingoperand)rt =21(targetregister)immediate =-50(bydefault,thisisdecimalinassemblycode)
26
I-FormatExample(2/2)⢠MIPSInstruction:
addi $21,$22,-50
8 22 21 -50
001000 10110 10101 1111111111001110
Decimal/field representation:
Binary/field representation:
hexadecimal representation: 22D5 FFCEhex
27
QuestionWhichinstructionhassamerepresentationasinteger35ten?
a) add$0,$0,$0b) subu $s0,$s0,$s0c) lw $0,0($0)d) addi $0,$0,35e) subu $0,$0,$0
Registersnumbersandnames:0:$0,..8:$t0,9:$t1,..15:$t7,16:$s0,17:$s1,..23:$s7
Opcodesandfunctionfields:add:opcode =0,funct =32subu:opcode =0,funct =35addi:opcode =8lw:opcode =35
opcode rs rt offset
rd functshamtopcode rs rt
opcode rs rt immediate
rd functshamtopcode rs rt
rd functshamtopcode rs rt
28
DealingWithLargeImmediates
⢠Howdowedealwith32-bitimmediates?â Sometimeswanttouseimmediates >Âą 215 withaddi,lw,sw andslti
â Bitwiselogicoperationswith32-bitimmediates
⢠Solution:Donâtmesswithinstructionformats,justaddanewinstruction
⢠LoadUpperImmediate (lui)â lui reg,immâMoves16-bitimm intoupperhalf(bits16-31)ofreg andzerosthelowerhalf(bits0-15)
29
lui Example
⢠Want:addiu $t0,$t0,0xABABCDCDâ Thisisapseudo-instruction!
⢠Translatesinto:lui $at,0xABAB # upper 16ori $at,$at,0xCDCD # lower 16addu $t0,$t0,$at # move
⢠Nowwecanhandleeverythingwitha16-bitimmediate!
30
Onlytheassemblergetstouse$at($1)
BranchingInstructions
⢠beq andbneâ Needtospecifyatargetaddressifbranchtakenâ Alsospecifytworegisterstocompare
⢠UseI-Format:
â opcode specifiesbeq (4)vs.bne (5)â rs andrt specifyregistersâ Howtobestuseimmediate tospecifyaddresses?
31
opcode rs rt immediate31 0
BranchingInstructionUsage
⢠Branchestypicallyusedforloops(if-else,while,for)â Loopsaregenerallysmall(<50instructions)â Functioncallsandunconditionaljumpshandledwithjumpinstructions(J-Format)
⢠Recall: Instructionsstoredinalocalizedareaofmemory(Code/Text)â Largestbranchdistancelimitedbysizeofcodeâ Addressofcurrentinstructionstoredintheprogramcounter(PC)
32
PC-RelativeAddressing
⢠PC-RelativeAddressing: UsetheimmediatefieldasatwoâscomplementoffsettoPCâ BranchesgenerallychangethePCbyasmallamount
â CanspecifyÂą 215 addressesfromthePC
33
BranchCalculation
⢠Ifwedonât takethebranch:â PC = PC + 4 = nextinstruction
⢠Ifwedo takethebranch:â PC = (PC+4) + (immediate*4)
⢠Observations:â immediate isnumberofinstructionstojump(remember,specifieswords)eitherforward(+)orbackwards(â)
â BranchfromPC+4 forhardwarereasons;willbeclearwhylaterinthecourse
34
BranchExample(1/2)
⢠MIPSCode:Loop: beq $9,$0,End
addu $8,$8,$10addiu $9,$9,-1j Loop
End:
⢠I-Formatfields:opcode =4 (lookuponGreenSheet)rs =9 (firstoperand)rt =0 (secondoperand)immediate =???
35
StartcountingfrominstructionAFTERthebranch
123
3
BranchExample(2/2)
⢠MIPSCode:Loop: beq $9,$0,End
addu $8,$8,$10addiu $9,$9,-1j Loop
End:
Fieldrepresentation(decimal):
Fieldrepresentation(binary):
36
4 9 0 331 0
000100 01001 00000 000000000000001131 0
QuestionsonPC-addressing
⢠Doesthevalueinbranchimmediatefieldchangeifwemovethecode?â Ifmovingindividuallinesofcode,thenyesâ Ifmovingallofcode,thenno
⢠Whatdowedoifdestinationis>215instructionsawayfrombranch?â Otherinstructionssaveusâ beq $s0,$0,far bne $s0,$0,next# next instr Ă j far
next: # next instr37
J-FormatInstructions(1/4)
⢠Forbranches,weassumedthatwewonâtwanttobranchtoofar,sowecanspecifyachangeinthePC
⢠Forgeneraljumps(j andjal),wemayjumptoanywhere inmemoryâ Ideally,wewouldspecifya32-bitmemoryaddresstojumpto
â Unfortunately,wecanâtfitbotha6-bitopcodeanda32-bitaddressintoasingle32-bitword
38
J-FormatInstructions(2/4)
⢠Definetwoâfieldsâofthesebitwidths:
⢠Asusual,eachfieldhasaname:
⢠KeyConcepts:â Keepopcode fieldidenticaltoR-FormatandI-Formatforconsistency
â Collapseallotherfieldstomakeroomforlargetargetaddress 39
6 2631 0
opcode target address31 0
J-FormatInstructions(3/4)
⢠Wecanspecify226 addressesâ Stillgoingtoword-alignedinstructions,soadd00 aslasttwobits(multiplyby4)
â Thisbringsusto28bitsofa32-bitaddress
⢠Takethe4highestorderbitsfromthePCâ Cannotreacheverywhere,butadequatealmostallofthetime,sinceprogramsarenâtthatlong
â Onlyproblematicifcodestraddlesa256MBboundary⢠Ifnecessary,use2jumpsorjr (R-Format)instead
40
J-FormatInstructions(4/4)
⢠Jumpinstruction:â NewPC={ (PC+4)[31..28],targetaddress,00}
⢠Notes:â {,,}meansconcatenation{4bits,26bits,2bits}=32bitaddress⢠Bookuses||instead
â Arrayindexing:[31..28]meanshighest4bitsâ Forhardwarereasons,usePC+4insteadofPC
41
AssemblerPseudo-Instructions⢠CertainCstatementsareimplementedunintuitivelyinMIPSâ e.g.assignment(a=b)viaadd$zero
⢠MIPShasasetofâpseudo-instructionsâtomakeprogrammingeasierâ Moreintuitivetoread,butgettranslatedintoactualinstructionslater
⢠Example:move dst,src
translatedintoaddi dst,src,0
42
AssemblerPseudo-Instructions
⢠Listofpseudo-instructions:http://en.wikipedia.org/wiki/MIPS_architecture#Pseudo_instructions
â Listalsoincludesinstructiontranslation⢠LoadAddress(la)â la dst,labelâ Loadsaddressofspecifiedlabelintodst
⢠LoadImmediate (li)â li dst,immâ Loads32-bitimmediateintodst
⢠MARShasadditionalpseudo-instructionsâ SeeHelp(F1)forfulllist
43
AssemblerRegister
⢠Problem:âWhenbreakingupapseudo-instruction,theassemblermayneedtouseanextraregister
â Ifitusesaregularregister,itâlloverwritewhatevertheprogramhasputintoit
⢠Solution:â Reservearegister($1 or$at forâassemblertemporaryâ)thatassemblerwillusetobreakuppseudo-instructions
â Sincetheassemblermayusethisatanytime,itâsnotsafetocodewithit
44
MultiplyandDivide⢠Examplepseudo-instruction:
mul $rd,$rs,$rtâ Consistsofmult whichstorestheoutputinspecialhiandloregisters,andamovefromtheseregistersto$rd
mult $rs,$rtmflo $rd
⢠mult anddiv havenothingimportantintherd fieldsincethedestinationregistersarehi andlo
⢠mfhi andmflo havenothingimportantinthers andrt fieldssincethesourceisdeterminedbytheinstruction(seeCOD)
45
IntegerMultiplication(1/3)
⢠Paperandpencilexample(unsigned):Multiplicand 1000 8 Multiplier x1001 9
10000000
0000+100001001000
⢠m bitsx n bits=m +n bitproduct
IntegerMultiplication(2/3)
⢠InMIPS,wemultiplyregisters,so:â 32-bitvaluex 32-bitvalue=64-bitvalue
⢠SyntaxofMultiplication(signed):â mult register1,register2âMultiplies32-bitvaluesinthoseregisters&puts64-bitproductinspecialresultregs:⢠putsproductupperhalfinhi,lowerhalfin lo
â hi andlo are2registersseparatefromthe32generalpurposeregisters
â Usemfhi register&mflo register tomovefromhi,lo toanotherregister
IntegerMultiplication(3/3)⢠Example:â inC: a = b * c;â inMIPS:
⢠letb be$s2;letc be$s3;andleta be$s0 and$s1 (sinceitmaybeupto64bits)
mult $s2,$s3 # b*cmfhi $s0 # upper half of
# product into $s0mflo $s1 # lower half of
# product into $s1â˘Note:Often,weonlycareaboutthelowerhalfoftheproduct.â˘Pseudo-inst.mul expandstomult/mflo.
IntegerDivision(1/2)
⢠Paperandpencilexample(unsigned):1001 Quotient Divisor
1000|1001010 Dividend-1000
101011010
-100010 Remainder
(or Modulo result)⢠Dividend=Quotientx Divisor+Remainder
⢠SyntaxofDivision(signed):â div register1,register2
â Divides32-bitregister1by32-bitregister2:
â putsremainderofdivisioninhi,quotientinlo
⢠ImplementsCdivision(/)andmodulo(%)
⢠ExampleinC: a = c / d; b = c % d;
⢠inMIPS:a$s0;b$s1;c$s2;d$s3
div $s2,$s3 # lo=c/d, hi=c%dmflo $s0 # get quotientmfhi $s1 # get remainder
IntegerDivision(2/2)
MALvs.TAL
⢠TrueAssemblyLanguage(TAL)â Theinstructionsacomputerunderstandsandexecutes
⢠MIPSAssemblyLanguage(MAL)â Instructionstheassemblyprogrammercanuse(includespseudo-instructions)
â EachMALinstructionbecomes1ormoreTALinstruction
51
Question
WhichofthefollowingplacetheaddressofLOOPin$v0?1) la $t1, LOOP
lw $v0, 0($t1)
2) jal LOOPLOOP: addu $v0, $ra, $zero
3) la $v0, LOOP
52
1 2 3A)T, T, TB)T, T, FC)F, T, TD)F, T, FE)F, F, T
Summary⢠I-Format: instructionswithimmediates,lw/sw (offsetisimmediate),andbeq/bneâ Butnottheshiftinstructionsâ BranchesusePC-relativeaddressing
⢠J-Format: j andjal (butnotjr)â Jumpsuseabsoluteaddressing
⢠R-Format: allotherinstructions
53
opcode rs rt immediateI:
opcode target addressJ:
opcode functrs rt rd shamtR: