Running a Program -CALL (Compiling, Assembling, Linking...
Transcript of Running a Program -CALL (Compiling, Assembling, Linking...
CS61C:GreatIdeasinComputerArchitecture
RunningaProgram - CALL(Compiling,Assembling,Linking,andLoading)
1
Instructors:NickWeaver&VladimirStojanovic
http://inst.eecs.Berkeley.edu/~cs61c/sp16
IntegerMultiplication(1/3)
• Paperandpencilexample(unsigned):Multiplicand 1000 8Multiplier x1001 9
100000000000
+100001001000 72
• m bitsx n bits=m +n bitproduct
2
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
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
IntegerDivision(1/2)
• Paperandpencilexample(unsigned):1001 Quotient
Divisor 1000|1001010 Dividend-1000
101011010
-100010 Remainder
(or Modulo result)
• Dividend=Quotientx Divisor+Remainder5
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
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
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
InterpretationvsTranslation
• Howdowerunaprogramwritteninasourcelanguage?– Interpreter:Directlyexecutesaprograminthesourcelanguage
– Translator:Convertsaprogramfromthesourcelanguagetoanequivalentprograminanotherlanguage
• Forexample,consideraPythonprogramfoo.py
9
Interpretation
• Pythoninterpreterisjustaprogramthatreadsapythonprogramandperformsthefunctionsofthatpythonprogram– Well,that’sanexaggeration,theinterpreterconvertstoasimplebytecode thattheinterpreterruns…Savedcopiesendupin.pycfiles
10
Interpretation• Anygoodreasontointerpretmachinelanguageinsoftware?
• MARS– usefulforlearning/debugging• AppleMacintoshconversion– SwitchedfromMotorola680x0instructionarchitecturetoPowerPC.• Similarissuewithswitchtox86
– Couldrequireallprogramstobere-translatedfromhighlevellanguage
– Instead,letexecutablescontainoldand/ornewmachinecode,interpretoldcodeinsoftwareifnecessary(emulation)
11
Interpretationvs.Translation?(1/2)
• Generallyeasiertowriteinterpreter• Interpreterclosertohigh-level,socangivebettererrormessages(e.g.,MARS)– Translatorreaction:addextrainformationtohelpdebugging(linenumbers,names)
• Interpreterslower(10x?),codesmaller(2x?)• Interpreterprovidesinstructionsetindependence:runonanymachine
12
Interpretationvs.Translation?(2/2)
• Translated/compiledcodealmostalwaysmoreefficientandthereforehigherperformance:– Importantformanyapplications,particularlyoperatingsystems.
• Compiledcodedoesthehardworkonce:duringcompilation–Whichiswhymost“interpreters”thesedaysarereally“justintimecompilers”:don’tthrowawaytheworkprocessingtheprogram
13
StepsincompilingaCprogram
14
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
WhereAreWeNow?
CS164
16
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
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
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
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
Administrivia• Startingnextweek,section124willbeW3-4in310soda– Thissectionshouldberatherlight,soifyouwantquestionsanswered,thismaybeagoodchoicetoattend
• Project2-1duedatewillbefirmedupinthestaffmeetingtoday(probablyaSaturdayduedate)
21
ProducingMachineLanguage(1/3)
• SimpleCase– Arithmetic,Logical,Shifts,andsoon– Allnecessaryinfoiswithintheinstructionalready
• WhataboutBranches?– PC-Relative– Sooncepseudo-instructionsarereplacedbyrealones,weknowbyhowmanyinstructionstobranch
• Sothesecanbehandled
22
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
ProducingMachineLanguage(3/3)• Whataboutjumps(j andjal)?– Jumpsrequireabsoluteaddress– So,forwardornot,stillcan’tgeneratemachineinstructionwithoutknowingthepositionofinstructionsinmemory
• Whataboutreferencestostaticdata?– la getsbrokenupintolui andori– Thesewillrequirethefull32-bitaddressofthedata
• Thesecan’tbedeterminedyet,sowecreatetwotables…
24
SymbolTable
• Listof“items”inthisfilethatmaybeusedbyotherfiles
• Whatarethey?– Labels:functioncalling– Data:anythinginthe.data section;variableswhichmaybeaccessedacrossfiles
25
RelocationTable• Listof“items”thisfileneedstheaddressoflater
• Whatarethey?– Anylabeljumpedto:j orjal• internal• external(includinglibfiles)
– Anypieceofdatainstaticsection• suchasthela instruction
26
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
WhereAreWeNow?
28
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