1AssemblerCS217CompilationPipeline• Compiler(gcc):.c .stranslateshigh-levellanguagetoassemblylanguage• Assembler(as):.s .otranslatesassemblylanguagetomachinelanguage• Archiver(ar):.o .acollectsobjectfilesintoasinglelibrary• Linker(ld):.o + .a a.outbuildsanexecutablefilefromacollectionofobjectfiles• Execution(execlp)loadsanexecutablefileintomemoryandstartsit2CompilationPipelineCompilerAssemblerArchiverLinker/LoaderExecution.c.s.o.aa.outAssembler• PurposeTranslatesassemblylanguageintomachinelanguageStoreresultinobjectfile(.o)• AssemblylanguageAsymbolicrepresentationofmachineinstructions• MachinelanguageContainseverythingneededtolink,load,andexecute theprogram3Translatingtomachinecode• Assemblylanguage: addcc %r3,%r7,%r2• addcc %r3,1000,%r2• Machinelanguage:0 unused(0) rs2rd op3 rs11031 29 24 18 13 12 4 01 simm13rd op3 rs11031 29 24 18 13 12 0000000000 0011100010010000000111031 29 24 18 13 12 4 01000111110100000010010000000111031 29 24 18 13 12 0AssemblyLanguage• Assemblylanguagestatements…declarativestatements specifyassemblytimeactions;e.g.,reservespace,definesymbols,identifysegments,andinitializedata(theydonotyieldmachineinstructionsbuttheymayaddinformationtotheobjectfilethatisusedbythelinker)imperativestatements specifyinstructions;typicallymap1imperativestatementto1machineinstructionsyntheticinstructions aremappedtooneormoremachineinstructions4MainTask• Mostimportantfunction:symbolmanipulationCreatelabelsandremembertheiraddresses• Forwardreferenceproblemloop:cmpi,n .section“.text”bgedone setcount,%l0nop ...... .section“.data”inci count:.word0done:Dealingwithforwardreferences• MostassemblershavetwopassesPass1:symboldefinitionPass2:instructionassembly• Or,alternatively,Pass1:instructionassemblyPass2:patchthecross-reference5Dealingwithforwardreferences• MostassemblershavetwopassesPass1:symboldefinitionPass2:instructionassembly• Or,alternatively,Pass1:instructionassemblyPass2:patchthecross-referencePass1• Stateloc(locationcounter);initially0symtab (symboltable);initiallyempty• Foreachlineofinput.../*Updatesymboltable*/iflinecontainsalabelenter<label,loc>intosymtab/*Updatelocationcounter*/iflinecontainsadirectiveadjustlocaccordingtodirectiveelseloc+=length_of_instruction6Pass2• Statelc (locationcounter);resetto0symtab (symboltable);filledfrompreviouspass• Foreachlineofinput/*Outputmachinelanguagecode*/iflinecontainsadirectiveprocess/outputdirectiveelseassemble/outputinstructionusingsymtab/*Updatelocationcounter*/iflinecontainsadirectiveadjustlocaccordingtodirectiveelseloc+=length_of_instructionExampleassembly.globallooploop:cmp%r16,%r24bge donenopcallfnopba loopinc%r16done:opdisp30:?......0≥≥≥≥010 disp22:?000always010 disp22:?004:8:12:16:20:24:0:28:.........7ExamplePass1.globallooploop:cmp%r16,%r24bge donenopcallfnopba loopinc%r16done:loop0defdone4disp22loop20disp22done28deff12disp30loopdoneExamplePass2.globallooploop:cmp%r16,%r24bge donenopcallfnopba loopinc%r16done:4:8:12:16:20:24:0:28:loop0defdone4disp22loop20disp22done28deff12disp30loopdoneopdisp30:?......0≥≥≥≥010 +24000always010 -2000.........8Relocationrecords.globallooploop:cmp%r16,%r24bge donenopcallfnopba loopinc%r16done:opdisp30:?......0≥≥≥≥010 +24000always010 -20004:8:12:16:20:24:0:28:.........loop0deff12disp30Dealingwithforwardreferences• MostassemblershavetwopassesPass1:symboldefinitionPass2:instructionassembly• Or,alternatively,Pass1:instructionassemblyPass2:patchthecross-reference9InstructionAssembly.globallooploop:cmp%r16,%r24bge donenopcallfnopba loopinc%r16done:opdisp30:?......0≥≥≥≥010 disp22:?000always010 disp22:?004:8:12:16:20:24:0:28:.........PatchingCross-References.globallooploop:cmp%r16,%r24bge donenopcallfnopba loopinc%r16done:4:8:12:16:20:24:0:28:loop0defdone4disp22loop20disp22done28deff12disp30loopdoneopdisp30:?......0≥≥≥≥010 +24000always010 -2000.........10ImplementinganAssembler.s fileinstructioninstructioninstructioninstruction.o fileinputassemble outputsymboltabledatasectiontextsectionbsssectiondisk inmemorystructurediskinmemorystructureInputFunctions• ReadassemblylanguageandproducelistofinstructionsThesefunctionsareprovided.s fileinstructioninstructioninstructioninstruction.o fileinputassemble outputsymboltabledatasectiontextsectionbsssection11InputFunctions• LexicalanalyzerGroupastreamofcharactersintotokensadd%g1,10,%g2• SyntacticanalyzerCheckthesyntaxoftheprogram<MNEMONIC><REG><COMMA><REG><COMMA><REG>• InstructionlistproducerProduceanin-memorylistofinstructiondatastructuresinstructioninstructionYourTaskinAssignment5• ImplementtwopassassemblerProcesslistofinstructionstoproduceobjectfileoutputstructures.s fileinstructioninstructioninstructioninstruction.o fileinputassemble outputsymboltabledatasectiontextsectionbsssection12PackingfieldsusingC• Assemblylanguage: addcc %r3,%r7,%r2•• Formatofarithmeticinstructions:0 unused(0) rs2rd op3 rs11031 29 24 18 13 12 4 0000000000
View Full Document