running a program -call (compiling, assembling, linking...
TRANSCRIPT
![Page 1: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/1.jpg)
CS61C:GreatIdeasinComputerArchitecture
RunningaProgram - CALL(Compiling,Assembling,Linking,andLoading)
1
Instructors:NickWeaver&VladimirStojanovic
http://inst.eecs.Berkeley.edu/~cs61c/sp16
![Page 2: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/2.jpg)
IntegerMultiplication(1/3)
• Paperandpencilexample(unsigned):Multiplicand 1000 8Multiplier x1001 9
100000000000
+100001001000 72
• m bitsx n bits=m +n bitproduct
2
![Page 3: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/3.jpg)
IntegerMultiplication(2/3)• InMIPS,wemultiplyregisters,so:
– 32-bitvaluex 32-bitvalue=64-bitvalue• SyntaxofMultiplication(signed):
– mult register1,register2– Multiplies32-bitvaluesinthoseregisters&puts64-bitproductinspecialresultregisters:• putsproductupperhalfinhi,lowerhalfinlo
– hi andlo are2registersseparate fromthe32generalpurposeregisters
– Usemfhi register&mflo registertomovefromhi,lotoanotherregister
• Thisisn’tjustaboutsizebutalsoperformance:– Multiplicationisslow,byseparatingoutwhenyoustartfromwhenyouneeditthiscanpotentiallyimproveperformance
3
![Page 4: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/4.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
4
![Page 5: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/5.jpg)
IntegerDivision(1/2)
• Paperandpencilexample(unsigned):1001 Quotient
Divisor 1000|1001010 Dividend-1000
101011010
-100010 Remainder
(or Modulo result)
• Dividend=Quotientx Divisor+Remainder5
![Page 6: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/6.jpg)
IntegerDivision(2/2)• 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↔$s3div $s2,$s3 # lo=c/d, hi=c%d
mflo $s0 # get quotientmfhi $s1 # get remainder
6
![Page 7: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/7.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
7
Logic Circuit Description(Circuit Schematic Diagrams)
+ How to take a program and run it
![Page 8: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/8.jpg)
LanguageExecutionContinuum• AnInterpreterisaprogramthatexecutesotherprograms.
• Languagetranslationgivesusanotheroption• Ingeneral,weinterpretahigh-levellanguagewhen
efficiencyisnotcriticalandtranslatetoalower-levellanguagetoincreaseperformance– Althoughthisisbecominga“distinctionwithoutadifference”
Manyintepreters doa“justintime”runtimecompilationtobytecode thateitherisemulatedordirectlycompiledtomachinecode(e.g.LLVM)
Easy to programInefficient to interpret
Difficult to programEfficient to interpret
Scheme Java C++
C
Assembly Machine codeJava bytecode
8
![Page 9: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/9.jpg)
InterpretationvsTranslation
• Howdowerunaprogramwritteninasourcelanguage?– Interpreter:Directlyexecutesaprograminthesourcelanguage
– Translator:Convertsaprogramfromthesourcelanguagetoanequivalentprograminanotherlanguage
• Forexample,consideraPythonprogramfoo.py
9
![Page 10: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/10.jpg)
Interpretation
• Pythoninterpreterisjustaprogramthatreadsapythonprogramandperformsthefunctionsofthatpythonprogram– Well,that’sanexaggeration,theinterpreterconvertstoasimplebytecode thattheinterpreterruns…Savedcopiesendupin.pycfiles
10
![Page 11: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/11.jpg)
Interpretation• Anygoodreasontointerpretmachinelanguageinsoftware?
• MARS– usefulforlearning/debugging• AppleMacintoshconversion– SwitchedfromMotorola680x0instructionarchitecturetoPowerPC.• Similarissuewithswitchtox86
– Couldrequireallprogramstobere-translatedfromhighlevellanguage
– Instead,letexecutablescontainoldand/ornewmachinecode,interpretoldcodeinsoftwareifnecessary(emulation)
11
![Page 12: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/12.jpg)
Interpretationvs.Translation?(1/2)
• Generallyeasiertowriteinterpreter• Interpreterclosertohigh-level,socangivebettererrormessages(e.g.,MARS)– Translatorreaction:addextrainformationtohelpdebugging(linenumbers,names)
• Interpreterslower(10x?),codesmaller(2x?)• Interpreterprovidesinstructionsetindependence:runonanymachine
12
![Page 13: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/13.jpg)
Interpretationvs.Translation?(2/2)
• Translated/compiledcodealmostalwaysmoreefficientandthereforehigherperformance:– Importantformanyapplications,particularlyoperatingsystems.
• Compiledcodedoesthehardworkonce:duringcompilation–Whichiswhymost“interpreters”thesedaysarereally“justintimecompilers”:don’tthrowawaytheworkprocessingtheprogram
13
![Page 14: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/14.jpg)
StepsincompilingaCprogram
14
![Page 15: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/15.jpg)
Compiler• Input:High-LevelLanguageCode(e.g.,foo.c)
• Output:AssemblyLanguageCode(e.g.MAL)(e.g.,foo.s forMIPS)
• Note:Outputmay containpseudo-instructions• Pseudo-instructions:instructionsthatassemblerunderstandsbutnotinmachineForexample:– move $s1,$s2⇒ add $s1,$s2,$zero
15
![Page 16: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/16.jpg)
WhereAreWeNow?
CS164
16
![Page 17: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/17.jpg)
Assembler• Input:AssemblyLanguageCode(e.g.MAL)(e.g.,foo.s forMIPS)
• Output:ObjectCode,informationtables(TAL)(e.g.,foo.o forMIPS)
• ReadsandUsesDirectives• ReplacePseudo-instructions• ProduceMachineLanguage• CreatesObjectFile
17
![Page 18: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/18.jpg)
AssemblerDirectives(p.A-51..A-53)• Givedirectionstoassembler,butdonotproducemachineinstructions.text: Subsequentitemsputinusertextsegment(machinecode).data: Subsequentitemsputinuserdatasegment(binaryrepofdatainsourcefile).globl sym: declaressym globalandcanbereferencedfromotherfiles.asciiz str: Storethestringstr inmemoryandnull-terminateit.word w1…wn: Storethen 32-bitquantitiesinsuccessivememorywords
18
![Page 19: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/19.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)
19
![Page 20: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/20.jpg)
Clicker/PeerInstructionWhichofthefollowingisacorrectTALinstructionsequenceforla$v0,FOO?*(akastorethe32-bitimmediatevalueFOOin$v0)%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 20
![Page 21: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/21.jpg)
Administrivia• Startingnextweek,section124willbeW3-4in310soda– Thissectionshouldberatherlight,soifyouwantquestionsanswered,thismaybeagoodchoicetoattend
• Project2-1duedatewillbefirmedupinthestaffmeetingtoday(probablyaSaturdayduedate)
21
![Page 22: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/22.jpg)
ProducingMachineLanguage(1/3)
• SimpleCase– Arithmetic,Logical,Shifts,andsoon– Allnecessaryinfoiswithintheinstructionalready
• WhataboutBranches?– PC-Relative– Sooncepseudo-instructionsarereplacedbyrealones,weknowbyhowmanyinstructionstobranch
• Sothesecanbehandled
22
![Page 23: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/23.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
23
![Page 24: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/24.jpg)
ProducingMachineLanguage(3/3)• Whataboutjumps(j andjal)?– Jumpsrequireabsoluteaddress– So,forwardornot,stillcan’tgeneratemachineinstructionwithoutknowingthepositionofinstructionsinmemory
• Whataboutreferencestostaticdata?– la getsbrokenupintolui andori– Thesewillrequirethefull32-bitaddressofthedata
• Thesecan’tbedeterminedyet,sowecreatetwotables…
24
![Page 25: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/25.jpg)
SymbolTable
• Listof“items”inthisfilethatmaybeusedbyotherfiles
• Whatarethey?– Labels:functioncalling– Data:anythinginthe.data section;variableswhichmaybeaccessedacrossfiles
25
![Page 26: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/26.jpg)
RelocationTable• Listof“items”thisfileneedstheaddressoflater
• Whatarethey?– Anylabeljumpedto:j orjal• internal• external(includinglibfiles)
– Anypieceofdatainstaticsection• suchasthela instruction
26
![Page 27: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/27.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 27
![Page 28: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/28.jpg)
WhereAreWeNow?
28
![Page 29: Running a Program -CALL (Compiling, Assembling, Linking ...inst.eecs.berkeley.edu/~cs61c/sp16/lec/11/2016Sp-CS61C-L11-CALL-post.pdf · Many intepretersdo a “just in time” runtime](https://reader030.vdocuments.us/reader030/viewer/2022040908/5e7fcb88edc1d33f421a769f/html5/thumbnails/29.jpg)
InConclusion…§ Compiler converts a single HLL file
into a single assembly language file.§ Assembler removes pseudo-
instructions, converts what it can to machine language, and creates a checklist for the linker (relocation table). A .s file becomes a .o file.ú Does 2 passes to resolve addresses,
handling internal forward references
§ Linker combines several .o files and resolves absolute addresses.ú Enables separate compilation, libraries
that need not be compiled, and resolves remaining addresses
§ Loader loads executable into memory and begins execution.
29