![Page 1: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/1.jpg)
CS110ComputerArchitecture
RunningaProgram- CALL(Compiling,Assembling,Linking,andLoading)
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 Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/2.jpg)
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
2
![Page 3: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/3.jpg)
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 =???
3
Start counting from instruction AFTER the branch
123
3
![Page 4: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/4.jpg)
BranchExample(2/2)
• MIPSCode:Loop: beq $9,$0,End
addu $8,$8,$10addiu $9,$9,-1j Loop
End:
Fieldrepresentation(decimal):
Fieldrepresentation(binary):
4
4 9 0 331 0
000100 01001 00000 000000000000001131 0
![Page 5: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/5.jpg)
J-FormatInstructions(1/4)
• Forbranches,weassumedthatwewon’twanttobranchtoofar,sowecanspecifyachangeinthePC
• Forgeneraljumps(j andjal),wemayjumptoanywhere inmemory– Ideally,wewouldspecifya32-bitmemoryaddresstojumpto
– Unfortunately,wecan’tfitbotha6-bitopcodeanda32-bitaddressintoasingle32-bitword
5
![Page 6: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/6.jpg)
J-FormatInstructions(2/4)
• Definetwo“fields”ofthesebitwidths:
• Asusual,eachfieldhasaname:
• KeyConcepts:– Keepopcode fieldidenticaltoR-FormatandI-Formatforconsistency
– Collapseallotherfieldstomakeroomforlargetargetaddress 6
6 2631 0
opcode target address31 0
![Page 7: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/7.jpg)
J-FormatInstructions(3/4)
• Wecanspecify226 addresses– Stillgoingtoword-alignedinstructions,soadd00 aslasttwobits(multiplyby4)
– Thisbringsusto28bitsofa32-bitaddress
• Takethe4highestorderbitsfromthePC– Cannotreacheverywhere,butadequatealmostallofthetime,sinceprogramsaren’tthatlong
– Onlyproblematicifcodestraddlesa256MBboundary• Ifnecessary,use2jumpsorjr (R-Format)instead
7
![Page 8: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/8.jpg)
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
8
![Page 9: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/9.jpg)
AssemblerPseudo-Instructions• CertainCstatementsareimplementedunintuitivelyinMIPS– e.g.assignment(a=b)viaadd$zero
• MIPShasasetof“pseudo-instructions”tomakeprogrammingeasier– Moreintuitivetoread,butgettranslatedintoactualinstructionslater
• Example:move dst,src
translatedintoaddi dst,src,0
9
![Page 10: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/10.jpg)
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
10
![Page 11: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/11.jpg)
AssemblerRegister
• Problem:–Whenbreakingupapseudo-instruction,theassemblermayneedtouseanextraregister
– Ifitusesaregularregister,it’lloverwritewhatevertheprogramhasputintoit
• Solution:– Reservearegister($1 or$at for“assemblertemporary”)thatassemblerwillusetobreakuppseudo-instructions
– Sincetheassemblermayusethisatanytime,it’snotsafetocodewithit
11
![Page 12: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/12.jpg)
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)
12
![Page 13: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/13.jpg)
IntegerMultiplication(1/3)
• Paperandpencilexample(unsigned):Multiplicand 1000 8Multiplier x 1001 9
1000+0000+0000+100001001000 72
• m bitsx n bits=m +n bitproduct
![Page 14: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/14.jpg)
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
![Page 15: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/15.jpg)
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.
![Page 16: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/16.jpg)
IntegerDivision(1/2)
• Paperandpencilexample(unsigned):– 74/8=9Rest2
1001 Quotient Divisor 1000|1001010 Dividend
-1000101011010-1000
10 Remainder(or Modulo result)
• Dividend=Quotientx Divisor+Remainder
![Page 17: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/17.jpg)
• 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)
![Page 18: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/18.jpg)
MALvs.TAL
• TrueAssemblyLanguage(TAL)– Theinstructionsacomputerunderstandsandexecutes
• MIPSAssemblyLanguage(MAL)– Instructionstheassemblyprogrammercanuse(includespseudo-instructions)
– EachMALinstructionbecomes1ormoreTALinstruction
18
![Page 19: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/19.jpg)
Summary• I-Format: instructionswithimmediates,lw/sw (offsetisimmediate),andbeq/bne– Butnottheshiftinstructions– BranchesusePC-relativeaddressing
• J-Format: j andjal (butnotjr)– Jumpsuseabsoluteaddressing
• R-Format: allotherinstructions
19
opcode rs rt immediateI:
opcode target addressJ:
opcode functrs rt rd shamtR:
![Page 20: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/20.jpg)
LevelsofRepresentation/Interpretation
lw $t0,0($2)lw $t1,4($2)sw $t1,0($2)sw $t0,4($2)
High Level LanguageProgram (e.g., C)
Assembly Language Program (e.g., MIPS)
Machine Language Program (MIPS)
Hardware Architecture Description(e.g., block diagrams)
Compiler
Assembler
Machine Interpretation
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
Architecture Implementation
Anything can be representedas a number,
i.e., data or instructions
20
Logic Circuit Description(Circuit Schematic Diagrams)
+ How to take a program and run it
![Page 21: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/21.jpg)
LanguageExecutionContinuum• AnInterpreterisaprogramthatexecutesotherprograms.
• Languagetranslationgivesusanotheroption• Ingeneral,weinterpretahigh-levellanguagewhenefficiencyisnotcriticalandtranslatetoalower-levellanguagetoincreaseperformance
Easy to programInefficient to interpret
Difficult to programEfficient to interpret
Scheme Java C++ C Assembly Machine codeJava bytecode
21
![Page 22: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/22.jpg)
InterpretationvsTranslation
• Howdowerunaprogramwritteninasourcelanguage?– Interpreter:Directlyexecutesaprograminthesourcelanguage
– Translator:Convertsaprogramfromthesourcelanguagetoanequivalentprograminanotherlanguage
• Forexample,consideraPythonprogramfoo.py
22
![Page 23: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/23.jpg)
Interpretation
• Pythoninterpreterisjustaprogramthatreadsapythonprogramandperformsthefunctionsofthatpythonprogram.
23
![Page 24: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/24.jpg)
Interpretation• Anygoodreasontointerpretmachinelanguageinsoftware?
• MARS– usefulforlearning/debugging• AppleMacintoshconversion– SwitchedfromMotorola680x0instructionarchitecturetoPowerPC.• Similarissuewithswitchtox86
– Couldrequireallprogramstobere-translatedfromhighlevellanguage
– Instead,letexecutablescontainoldand/ornewmachinecode,interpretoldcodeinsoftwareifnecessary(emulation)
24
![Page 25: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/25.jpg)
Interpretationvs.Translation?(1/2)
• Generallyeasiertowriteinterpreter• Interpreterclosertohigh-level,socangivebettererrormessages(e.g.,MARS)– Translatorreaction:addextrainformationtohelpdebugging(linenumbers,names)
• Interpreterslower(10x?),codesmaller(2x?)• Interpreterprovidesinstructionsetindependence:runonanymachine
25
![Page 26: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/26.jpg)
Interpretationvs.Translation?(2/2)
• Translated/compiledcodealmostalwaysmoreefficientandthereforehigherperformance:– Importantformanyapplications,particularlyoperatingsystems.
• Translation/compilationhelps“hide”theprogram“source”fromtheusers:– Onemodelforcreatingvalueinthemarketplace(eg.Microsoftkeepsalltheirsourcecodesecret)
– Alternativemodel,“opensource”,createsvaluebypublishingthesourcecodeandfosteringacommunityofdevelopers.
26
![Page 27: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/27.jpg)
StepsincompilingaCprogram
27
![Page 28: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/28.jpg)
Compiler• Input:High-LevelLanguageCode(e.g.,foo.c)
• Output:AssemblyLanguageCode(e.g.,foo.s forMIPS)
• Note:Outputmay containpseudo-instructions• Pseudo-instructions:instructionsthatassemblerunderstandsbutnotinmachineForexample:– move $s1,$s2Þ add $s1,$s2,$zero
28
![Page 29: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/29.jpg)
WhereAreWeNow?
Compiler Class
29
![Page 30: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/30.jpg)
Inthenews:XilinxACAP
• AdaptiveComputeAccelerationPlatform– AdvancedformofFPGA– MachineLearning,Genomic,Video-coding,Data-analysis,Fintec
– 50billiontransistors– 7nm(firstchipin7nm!)– 10-100xspeedvs.CPUs– FasterthanGPU– Muchmoreenergyefficient
– Fordatacenters/supercomputing 30
![Page 31: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/31.jpg)
31
![Page 32: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/32.jpg)
Assembler• Input:AssemblyLanguageCode(MAL)(e.g.,foo.s forMIPS)
• Output:ObjectCode,informationtables(TAL)(e.g.,foo.o forMIPS)
• ReadsandUsesDirectives• ReplacePseudo-instructions• ProduceMachineLanguage• CreatesObjectFile
32
![Page 33: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/33.jpg)
AssemblerDirectives(p.A-13..A-17)• Givedirectionstoassembler,butdonotproducemachineinstructions.text: Subsequentitemsputinusertextsegment(machinecode).data: Subsequentitemsputinuserdatasegment(binaryrepofdatainsourcefile).globl sym: declaressym globalandcanbereferencedfromotherfiles.asciiz str: Storethestringstr inmemoryandnull-terminateit.word w1…wn: Storethen 32-bitquantitiesinsuccessivememorywords
33
![Page 34: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/34.jpg)
Pseudo-instructionReplacement• AssemblertreatsconvenientvariationsofmachinelanguageinstructionsasifrealinstructionsPseudo: Real:subu $sp,$sp,32 addiu $sp,$sp,-32sd $a0, 32($sp) sw $a0, 32($sp)
sw $a1, 36($sp)mul $t7,$t6,$t5 mult $t6,$t5
mflo $t7addu $t0,$t6,1 addiu $t0,$t6,1ble $t0,100,loop slti $at,$t0,101
bne $at,$0,loopla $a0, str lui $at,left(str)
ori $a0,$at,right(str)
34
![Page 35: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/35.jpg)
QuestionWhichofthefollowingisacorrectTALinstructionsequenceforla$v0,FOO?*%hi(label),tellsassemblertofillupper16bitsoflabel’saddr%lo(label),tellsassemblertofilllower16bitsoflabel’saddr
A:ori $v0,%hi(FOO)addiu $v0,%lo(FOO)
B:ori $v0,%lo(FOO)lui $v0,%hi(FOO)
C:lui $v0,%lo(FOO)ori $v0,%hi(FOO)
D:lui $v0,%hi(FOO)ori $v0,%lo(FOO)
E:la$v0,FOOisalreadyaTALinstruction
*AssumetheaddressofFOOis0xABCD0124 35
![Page 36: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/36.jpg)
ProducingMachineLanguage(1/3)
• SimpleCase– Arithmetic,Logical,Shifts,andsoon– Allnecessaryinfoiswithintheinstructionalready
• WhataboutBranches?– PC-Relative– Sooncepseudo-instructionsarereplacedbyrealones,weknowbyhowmanyinstructionstobranch
• Sothesecanbehandled
36
![Page 37: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/37.jpg)
ProducingMachineLanguage(2/3)
• “ForwardReference”problem– Branchinstructionscanrefertolabelsthatare“forward”intheprogram:
– Solvedbytaking2passesovertheprogram• Firstpassrememberspositionoflabels• Secondpassuseslabelpositionstogeneratecode
or $v0, $0, $0L1: slt $t0, $0, $a1
beq $t0, $0, L2addi $a1, $a1, -1j L1
L2: add $t1, $a0, $a1
37
![Page 38: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/38.jpg)
ProducingMachineLanguage(3/3)• Whataboutjumps(j andjal)?– Jumpsrequireabsoluteaddress– So,forwardornot,stillcan’tgeneratemachineinstructionwithoutknowingthepositionofinstructionsinmemory
• Whataboutreferencestostaticdata?– la getsbrokenupintolui andori– Thesewillrequirethefull32-bitaddressofthedata
• Thesecan’tbedeterminedyet,sowecreatetwotables…
38
![Page 39: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/39.jpg)
SymbolTable
• Listof“items”inthisfilethatmaybeusedbyotherfiles
• Whatarethey?– Labels:functioncalling– Data:anythinginthe.data section;variableswhichmaybeaccessedacrossfiles
39
![Page 40: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/40.jpg)
RelocationTable• Listof“items”thisfileneedstheaddressoflater
• Whatarethey?– Anylabeljumpedto:j orjal• internal• external(includinglibfiles)
– Anypieceofdatainstaticsection• suchasthela instruction
40
![Page 41: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/41.jpg)
ObjectFileFormat• objectfileheader:sizeandpositionoftheotherpiecesoftheobjectfile
• textsegment:themachinecode• datasegment:binaryrepresentationofthestaticdatainthesourcefile
• relocationinformation:identifieslinesofcodethatneedtobefixeduplater
• symboltable:listofthisfile’slabelsandstaticdatathatcanbereferenced
• debugginginformation• AstandardformatisELF(exceptMS)
http://www.skyfree.org/linux/references/ELF_Format.pdf 41
![Page 42: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/42.jpg)
Admin
• Project1.1willbepublishedtoday(thistimeforreal;))
42
![Page 43: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/43.jpg)
WhereAreWeNow?
43
![Page 44: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/44.jpg)
Linker(1/3)• Input:Objectcodefiles,informationtables(e.g.,foo.o,libc.o forMIPS)
• Output:Executablecode(e.g.,a.out forMIPS)
• Combinesseveralobject(.o)filesintoasingleexecutable(“linking”)
• Enableseparatecompilationoffiles– Changestoonefiledonotrequirerecompilationofthewholeprogram• WindowsNTsource was>40Mlinesofcode!
– Oldname“LinkEditor”fromeditingthe“links”injumpandlinkinstructions
44
![Page 45: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/45.jpg)
.o file 1text 1data 1info 1
.o file 2text 2data 2info 2
Linker
a.outRelocated text 1Relocated text 2Relocated data 1Relocated data 2
Linker(2/3)
45
![Page 46: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/46.jpg)
Linker(3/3)
• Step1:Taketextsegmentfromeach.o fileandputthemtogether
• Step2:Takedatasegmentfromeach.o file,putthemtogether,andconcatenatethisontoendoftextsegments
• Step3:Resolvereferences– GothroughRelocationTable;handleeachentry– Thatis,fillinallabsoluteaddresses
46
![Page 47: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/47.jpg)
FourTypesofAddresses
• PC-RelativeAddressing(beq,bne)– neverrelocate
• AbsoluteFunctionAddress(j,jal)– alwaysrelocate
• ExternalFunctionReference(usuallyjal)– alwaysrelocate
• StaticDataReference(oftenlui andori)– alwaysrelocate
47
![Page 48: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/48.jpg)
AbsoluteAddressesinMIPS
• Whichinstructionsneedrelocationediting?– J-format:jump,jumpandlink
– Loadsandstorestovariablesinstaticarea,relativetoglobalpointer
– Whataboutconditionalbranches?
– PC-relativeaddressingpreservedevenifcodemoves
j/jal xxxxx
lw/sw $gp $x address
beq/bne $rs $rt address
48
![Page 49: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/49.jpg)
ResolvingReferences(1/2)
• Linkerassumesfirstwordoffirsttextsegmentisataddress0x04000000.– (Morelaterwhenwestudy“virtualmemory”)
• Linkerknows:– lengthofeachtextanddatasegment– orderingoftextanddatasegments
• Linkercalculates:– absoluteaddressofeachlabeltobejumpedto(internalorexternal)andeachpieceofdatabeingreferenced
49
![Page 50: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/50.jpg)
ResolvingReferences(2/2)
• Toresolvereferences:– searchforreference(dataorlabel)inall“user”symboltables
– ifnotfound,searchlibraryfiles(forexample,forprintf)
– onceabsoluteaddressisdetermined,fillinthemachinecodeappropriately
• Outputoflinker:executablefilecontainingtextanddata(plusheader)
50
![Page 51: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/51.jpg)
WhereAreWeNow?
51
![Page 52: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/52.jpg)
LoaderBasics
• Input:ExecutableCode(e.g.,a.out forMIPS)
• Output:(programisrun)• Executablefilesarestoredondisk• Whenoneisrun,loader’sjobistoloaditintomemoryandstartitrunning
• Inreality,loaderistheoperatingsystem(OS)– loadingisoneoftheOStasks
52
![Page 53: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/53.jpg)
Loader…whatdoesitdo?• Readsexecutablefile’sheadertodeterminesizeoftextand
datasegments• Createsnewaddressspaceforprogramlargeenoughtohold
textanddatasegments,alongwithastacksegment• Copiesinstructionsanddatafromexecutablefileintothenew
addressspace• Copiesargumentspassedtotheprogramontothestack• Initializesmachineregisters
– Mostregisterscleared,butstackpointerassignedaddressof1stfreestacklocation
• Jumpstostart-uproutinethatcopiesprogram’sargumentsfromstacktoregisters&setsthePC– Ifmainroutinereturns,start-uproutineterminatesprogramwiththe
exitsystemcall 53
![Page 54: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/54.jpg)
QuestionAtwhatpointinprocessareallthemachinecodebitsgeneratedforthefollowingassemblyinstructions:1)addu $6, $7, $82)jal fprintf
A:1)&2)AftercompilationB:1)Aftercompilation,2)AfterassemblyC:1)Afterassembly,2)AfterlinkingD:1)Afterassembly,2)AfterloadingE:1)Aftercompilation,2)Afterlinking
54
![Page 55: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/55.jpg)
Example:CÞ AsmÞ ObjÞ ExeÞ Run
#include <stdio.h>int main (int argc, char *argv[]) {int i, sum = 0;for (i = 0; i <= 100; i++)
sum = sum + i * i;printf ("The sum of sq from 0 .. 100 is %d\n", sum);
}
C Program Source Code: prog.c
“printf” lives in “libc”
55
![Page 56: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/56.jpg)
Compilation:MAL.text.align 2.globl main
main:subu $sp,$sp,32sw $ra, 20($sp)sd $a0, 32($sp)sw $0, 24($sp)sw $0, 28($sp)
loop:lw $t6, 28($sp)mul $t7, $t6,$t6lw $t8, 24($sp)addu $t9,$t8,$t7sw $t9, 24($sp)
addu $t0, $t6, 1sw $t0, 28($sp)ble $t0,100, loopla $a0, strlw $a1, 24($sp)jal printfmove $v0, $0lw $ra, 20($sp)addiu $sp,$sp,32jr $ra.data.align 0
str:.asciiz "The sum of sq from 0 .. 100 is %d\n"
Where are7 pseudo-instructions?
56
![Page 57: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/57.jpg)
Compilation:MAL.text.align 2.globl main
main:subu $sp,$sp,32sw $ra, 20($sp)sd $a0, 32($sp)sw $0, 24($sp)sw $0, 28($sp)
loop:lw $t6, 28($sp)mul $t7, $t6,$t6lw $t8, 24($sp)addu $t9,$t8,$t7sw $t9, 24($sp)
addu $t0, $t6, 1sw $t0, 28($sp)ble $t0,100, loopla $a0, strlw $a1, 24($sp)jal printfmove $v0, $0lw $ra, 20($sp)addiu $sp,$sp,32jr $ra.data.align 0
str:.asciiz "The sum of sq from 0 .. 100 is %d\n"
7 pseudo-instructionsunderlined
57
![Page 58: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/58.jpg)
Assemblystep1:
00 addiu $29, $29,-3204 sw $31, 20($29)08 sw $4, 32($29)0c sw $5, 36($29)10 sw $0, 24($29)14 sw $0, 28($29)18 lw $14, 28($29)1c multu $14, $1420 mflo $1524 lw $24, 24($29)28 addu $25, $24,$152c sw $25, 24($29)
30 addiu $8, $14, 134 sw $8, 28($29)38 slti $1, $8, 101 3c bne $1, $0, loop40 lui $4, l.str44 ori $4, $4,r.str 48 lw $5, 24($29)4c jal printf50 add $2, $0, $054 lw $31, 20($29) 58 addiu $29, $29,325c jr $31
Remove pseudoinstructions, assign addresses
58
![Page 59: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/59.jpg)
Assemblystep2
• SymbolTableLabel address(inmodule) typemain: 0x00000000 global textloop: 0x00000018 local textstr: 0x00000000 local data
• RelocationInformationAddress Instr.type Dependency0x00000040 lui l.str0x00000044 ori r.str0x0000004c jal printf
Create relocation table and symbol table
59
![Page 60: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/60.jpg)
Assemblystep3
00 addiu $29,$29,-3204 sw $31,20($29)08 sw $4, 32($29)0c sw $5, 36($29)10 sw $0, 24($29)14 sw $0, 28($29)18 lw $14, 28($29)1c multu $14, $1420 mflo $1524 lw $24, 24($29)28 addu $25,$24,$152c sw $25, 24($29)
30 addiu $8,$14, 134 sw $8,28($29)38 slti $1,$8, 101 3c bne $1,$0, -1040 lui $4, l.str44 ori $4,$4,r.str48 lw $5,24($29)4c jal printf50 add $2, $0, $054 lw $31,20($29) 58 addiu $29,$29,325c jr $31
Resolve local PC-relative labels
60
![Page 61: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/61.jpg)
Assemblystep4
• Generateobject(.o)file:– Outputbinaryrepresentationfor• textsegment(instructions)• datasegment(data)• symbolandrelocationtables
– Usingdummy“placeholders”forunresolvedabsoluteandexternalreferences
61
![Page 62: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/62.jpg)
Textsegmentinobjectfile0x000000 001001111011110111111111111000000x000004 101011111011111100000000000101000x000008 101011111010010000000000001000000x00000c 101011111010010100000000001001000x000010 101011111010000000000000000110000x000014 101011111010000000000000000111000x000018 100011111010111000000000000111000x00001c 100011111011100000000000000110000x000020 000000011100111000000000000110010x000024 001001011100100000000000000000010x000028 001010010000000100000000011001010x00002c 101011111010100000000000000111000x000030 000000000000000001111000000100100x000034 000000110000111111001000001000010x000038 000101000010000011111111111101110x00003c 101011111011100100000000000110000x000040 001111000000010000000000000000000x000044 100011111010010100000000000000000x000048 000011000001000000000000111011000x00004c 001001000000000000000000000000000x000050 100011111011111100000000000101000x000054 001001111011110100000000001000000x000058 000000111110000000000000000010000x00005c 00000000000000000001000000100001
62
![Page 63: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/63.jpg)
Linkstep1:combineprog.o,libc.o
• Mergetext/datasegments• Createabsolutememoryaddresses• Modify&mergesymbolandrelocationtables• SymbolTable– Label Addressmain: 0x00000000loop: 0x00000018str: 0x10000430printf: 0x000003b0 …
• RelocationInformation– Address Instr.Type Dependency0x00000040 lui l.str0x00000044 ori r.str0x0000004c jal printf …
63
![Page 64: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/64.jpg)
Linkstep2:
00 addiu $29,$29,-3204 sw$31,20($29)08 sw$4, 32($29)0c sw$5, 36($29)10 sw $0, 24($29)14 sw $0, 28($29)18 lw $14, 28($29)1c multu $14, $1420 mflo $1524 lw $24, 24($29)28 addu $25,$24,$152c sw $25, 24($29)
30 addiu $8,$14, 134 sw$8,28($29)38 slti $1,$8, 101 3c bne $1,$0, -1040 lui $4, 409644 ori $4,$4,107248 lw$5,24($29)4c jal 94450 add $2, $0, $054 lw $31,20($29) 58 addiu $29,$29,325c jr$31
• Edit Addresses in relocation table • (shown in TAL for clarity, but done in binary )
64
![Page 65: CS 110 Computer Architectureopcode= 4 (look up on Green Sheet) rs= 9 (first operand) rt= 0 (second operand) immediate= ??? 3 Start counting from instruction AFTER the branch 1 2 3](https://reader030.vdocuments.net/reader030/viewer/2022040400/5e72ff69180dd25ffb62a4ce/html5/thumbnails/65.jpg)
Linkstep3:
• Outputexecutableofmergedmodules– Singletext(instruction)segment– Singledatasegment– Headerdetailingsizeofeachsegment
• NOTE:– TheprecedingexamplewasamuchsimplifiedversionofhowELFandotherstandardformatswork,meantonlytodemonstratethebasicprinciples.
65