Assembler 4 Presentation

  • View
    216

  • Download
    0

Embed Size (px)

DESCRIPTION

SIC and SIC / XE Assembler design

Text of Assembler 4 Presentation

  • AssemblersSystem Softwareby Leland L. Beck

  • Role of AssemblerSourceProgramAssemblerObjectCodeLoaderExecutable CodeLinker

  • Introduction to AssemblersFundamental functionstranslating mnemonic operation codes to their machine language equivalentsassigning machine addresses to symbolic labels

    Machine dependencydifferent machine instruction formats and codes

  • Assembler DirectivesPseudo-InstructionsNot translated into machine instructionsProviding information to the assemblerBasic assembler directivesSTARTENDBYTEWORDRESBRESW

  • Assemblers functionsConvert mnemonic operation codes to their machine language equivalentsConvert symbolic operands to their equivalent machine addresses Build the machine instructions in the proper formatConvert the data constants to internal machine representationsWrite the object program and the assembly listing

  • Example of Instruction Assemble

    Forward reference STCH BUFFER,X (54)16 1 (001)2 (039)16549039

    8

    1

    15

    opcode

    x

    address

    m

  • Difficulties: Forward ReferenceForward reference: reference to a label that is defined later in the program.

    LocLabelOperatorOperand

    1000FIRSTSTLRETADR1003CLOOPJSUBRDREC 1012JCLOOP 1033RETADRRESW1

  • Two Pass AssemblerPass 1Assign addresses to all statements in the programSave the values assigned to all labels for use in Pass 2Perform some processing of assembler directivesPass 2Assemble instructionsGenerate data values defined by BYTE, WORDPerform processing of assembler directives not done in Pass 1Write the object program and the assembly listing

  • Two Pass Assembler Read from input lineLABEL, OPCODE, OPERAND

  • Data StructuresOperation Code Table (OPTAB)Symbol Table (SYMTAB)Location Counter(LOCCTR)

  • OPTAB (operation code table)Contentmenmonic, machine code (instruction format, length) etc.Characteristicstatic tableImplementationarray or hash table, easy for search

  • SYMTAB (symbol table)Contentlabel name, value, flag, (type, length) etc.Characteristicdynamic table (insert, delete, search)Implementationhash table, non-random keys, hashing functionCOPY1000FIRST 1000CLOOP1003ENDFIL1015EOF1024THREE102DZERO1030RETADR1033LENGTH1036BUFFER1039RDREC2039

  • TutorialADD3START3000BEGLDAVAL1ADDVAL2ADDVAL3STASUMVAL1WORD3VAL2WORD5VAL3WORD4SUMRESW1ENDBEG

  • TutorialSUMSTART2000FIRSTLDXZEROLDAZEROLOOPADDTABLE,XTIXCOUNTJLTLOOPSTATOTALRSUBTABLERESW4096COUNTRESW1ZEROWORD0TOTALRESW1ENDFIRST

  • Object ProgramHeaderCol. 1HCol. 2~7Program nameCol. 8~13Starting address (hex)Col. 14-19Length of object program in bytes (hex)Text Col.1 TCol.2~7Starting address in this record (hex)Col. 8~9Length of object code in this record in bytes (hex)Col. 10~69Object code (69-10+1)/6=10 instructionsEndCol.1ECol.2~7Address of first executable instruction (hex)(END program_name)

  • Fig. 2.3H COPY 001000 00107AT 001000 1E 141033 482039 001036 281030 301015 482061 ...T 00101E 15 0C1036 482061 081044 4C0000 454F46 000003 000000T 002039 1E 041030 001030 E0205D 30203F D8205D 281030 T 002057 1C 101036 4C0000 F1 001000 041030 E02079 302064 T 002073 07 382064 4C0000 05E 001000

  • Assembler DesignMachine Dependent Assembler Featuresinstruction formats and addressing modesprogram relocationMachine Independent Assembler Featuresliteralssymbol-defining statementsexpressionsprogram blockscontrol sections and program linking

  • Machine-dependent Assembler FeaturesSec. 2-2 Instruction formats and addressing modes Program relocation

  • Instruction Format and Addressing ModeSIC/XEPC-relative or Base-relative addressing: op mIndirect addressing: op @mImmediate addressing: op #cExtended format: +op mIndex addressing: op m,xregister-to-register instructionslarger memory -> multi-programming (program allocation)Example program

  • TranslationRegister translationregister name (A, X, L, B, S, T, F, PC, SW) and their values (0,1, 2, 3, 4, 5, 6, 8, 9)preloaded in SYMTABAddress translationMost register-memory instructions use program counter relative or base relative addressingFormat 3: 12-bit address fieldbase-relative: ( Limits ) 0~4095pc-relative: ( Limits ) -2048~2047Format 4: 20-bit address field

  • PC-Relative Addressing ModesPC-relative100000FIRSTSTLRETADR17202D

    (14)16 1 1 0 0 1 0(02D) 16 displacement= RETADR - PC = 30-3 = 2D400017JCLOOP3F2FEC

    (3C)16 1 1 0 0 1 0(FEC) 16 displacement= CLOOP-PC= 6 - 1A= -14= FEC

    Sheet1

    Format 3e=0

    op(6)nIxbpedisp(12)

    Sheet1

    Format 3e=0

    op(6)nIxbpedisp(12)

  • Base-Relative Addressing ModesBase-relativebase register is under the control of the programmer12LDB#LENGTH13BASELENGTH160104ESTCHBUFFER, X57C003

    ( 54 )16 1 1 1 1 0 0( 003 ) 16 (54) 1 1 1 0 1 0 0036-1051= -101B16displacement= BUFFER - B = 0036 - 0033 = 3NOBASE is used to inform the assembler that the contents of the base register no longer be relied upon for addressing

    Sheet1

    Format 3e=0

    op(6)nIxbpedisp(12)

  • New assembler directivesBASE XYZInforms the assembler that the base register will contain the address of XYZ

    NOBASEUsed to inform the assembler that the contents of the base register can no longer be relied upon for addressing.

  • Immediate Address TranslationImmediate addressing550020LDA#3010003

    ( 00 )16 0 1 0 0 0 0( 003 ) 16

    133103C +LDT#409675101000

    ( 74 )16 0 1 0 0 0 1( 01000 ) 16

    Sheet1

    Format 3e=0

    op(6)nIxbpedisp(12)

    Sheet1

    Format 3e=0

    op(6)nIxbpedisp(20)

  • Program RelocationExample Fig. 2.1 (SIC)Absolute program, starting address 1000e.g. 55101BLDATHREE00102DRelocate the program to 2000e.g. 55101BLDATHREE00202DEach Absolute address should be modifiedExample Fig. 2.5: Except for absolute address, the rest of the instructions need not be modifiednot a memory address (immediate addressing)PC-relative, Base-relativeThe only parts of the program that require modification at load time are those that specify direct addresses

  • Example

  • Relocatable ProgramModification recordCol 1MCol 2-7 Starting location of the address field to be modified, relative to the beginning of the programCol 8-9 length of the address field to be modified, in half- bytes

  • Object Code

  • Machine-Independent Assembler FeaturesLiteralsSymbol Defining StatementExpressionsProgram BlocksControl Sections and Program Linking

  • LiteralsDesign ideaLet programmers to be able to write the value of a constant operand as a part of the instruction that uses it. This avoids having to define the constant elsewhere in the program and make up a label for it.Examplee.g. 45001AENDFIL LDA =CEOF032010 93 LTORG------------------------------------------------------------ 002D* =CEOF454F46e.g. 2151062WLOOP TD =X05E32011

  • Literals vs. Immediate OperandsImmediate OperandsThe operand value is assembled as part of the machine instructione.g. 550020LDA#3010003LiteralsThe assembler generates the specified value as a constant at some other memory locatione.g. 45001AENDFILLDA=CEOF032010Compare (Fig. 2.6)e.g. 45001AENDFILLDAEOF032010 80002DEOFBYTECEOF454F46

  • Literal - Implementation (1/3)Literal poolsNormally literals are placed into a pool at the end of the programsee Fig. 2.10 (END statement)In some cases, it is desirable to place literals into a pool at some other location in the object programassembler directive LTORGreason: keep the literal operand close to the instruction

  • Literal - Implementation (2/3)Duplicate literalse.g. 2151062WLOOPTD=X05e.g. 230106BWD=X05The assemblers should recognize duplicate literals and store only one copy of the specified data valueComparison of the defining expressionSame literal name with different value, e.g. LOCCTR=*Comparison of the generated data value The benefits of using generate data value are usually not great enough to justify the additional complexity in the assembler

  • Literal - Implementation (3/3)LITTABliteral name, the operand value and length, the address assigned to the operand Pass 1build LITTAB with literal name, operand value and length, leaving the address unassignedwhen LTORG statement is encountered, assign an address to each literal not yet assigned an addressPass 2search LITTAB for each literal operand encounteredgenerate data values using BYTE or WORD statementsgenerate modification record for literals that represent an address in the program

  • Symbol-Defining StatementsLabels on instructions or data areasthe value of such a label is the address assigned to the statementDefining symbolssymbol EQUvaluevalue can be: constant, other symbol, expressionmaking the source program easier to understandno forward reference

  • Symbol-Defining StatementsExample 1MAXLENEQU4096+LDT#MAXLENExample 2 (Many general purpose registers)BASEEQUR1COUNTEQUR2INDEXEQU R3Example 3MAXLENEQUBUFEND-BUFFER+LDT#4096

  • ORG (origin)Indirectly assign values to symbolsReset the location counter to the specified valueORGvalueValue can be: constant, other symbol, expressionNo forward reference

  • ExpressionsExpressions can be classified as absolute expressions or relative expressionsMAXLENEQUBUFEND-BUFFERBUFEND and BUFFER both are relative terms, representing addresses within the programHowever the expression BUFEND-BUFFER represents an absolute valueWhen relative terms are paired with opposite signs, th