Slide S2-01 Chapter 2 - ASSEMBLERS ------------ --------------- | Program in | ------------- | Program in | | assembly | | | | machine | | language |----->| assembler |----->| language | | (source | | | | (object code) | | code) | ------------- --------------- ------------ - there are certain functions that any assembler must perform - design of an assembler depends upon the source language and the machine language - an assembler depends upon machine structure ====================================================================== Slide S2-02 Translation of source program to object code - convert mnemonic operation codes to their machine language equivalents - convert symbolic operands to their equivalent machine addresses Assembler Pass 1 - assign addresses to labels Pass 2 - perform the translation Pass 1 (define symbols) - assign addresses to all statements in the program - save the values (addresses) assigned to all labels for use in pass 2 - perform some processing of assembler directives Slide S2-03 Pass 2 (assemble instructions and generate object program) - assemble instructions (translating operation codes and looking up addresses) - generate data values defined by BYTE, WORD, etc. - perform processing of assembler directives not done during pass 1 - write the object program and the assembly listing Slide S2-04 Assembler Data Structures Operation Code Table (OPTAB) OPTBL contains operations and their machine language equivalents Symbol Table (SYMTAB) SYMTBL contains addresses assigned to labels Location Counter (LOCCTR) used in assigning addresses to labels Assembler Logic Pass 1 - Page 53 of text Pass 2 - Page 54 of text ====================================================================== Slide S2-05 Section 2.2.1 Instruction Formats and Addressing Modes -------- | op | -------- ------------------ | op | r1 | r2 | ------------------ ----------------------------------------- | op | n | i | x | b | p | e | disp | ----------------------------------------- -------------------------------------------- | op | n | i | x | b | p | e | address | -------------------------------------------- Flag Bits n, i n = 0, i = 1 immediate addressing n = 1, i = 0 indirect addressing n = 1, i = 1 otherwise (simple addressing) x x = 1 indexed addressing x = 0 otherwise b, p b = 1, p = 0 base relative addressing b = 0, p = 1 program-counter relative addressing b = 0, p = 0 otherwise (direct addressing) e e = 0 Format 3 e = 1 Format 4 Slide S2-06 - put registers in symbol table A 0 X 1 L 2 . . . register instructions COMPR A, S / \ 0 4 - register-memory instructions are assembled using either PC relative or base relative addressing (attempt PC relative first) During execution of instructions, the PC is advanced AFTER each instruction is fetched and BEFORE it is executed PC relative 10 0000 FIRST STL RETADR 17 2 02D \ \ \ \ \ PC=03 30 ni=11 \ 30-03 bp=01 -------------------------------------- | op | n | i | x | b | p | e | -------------------------------------- Slide S2-07 base relative 160 104E STCH BUFFER,X 57 C 003 \ \ \ \ 36 ni=11 \ 36-33 x=1 | bp=10 (B) - if neither PC relative nor base relative addressing can be used, then the 4-byte extended instruction format must be used 15 0006 CLOOP +JSUB RDREC 4B 1 01036 \ \ \ 1036 \ \ ni=11 e=1 bp=0 immediate addressing 55 0020 LDA #3 01 0 003 \ ni=01 12 0003 LDB #LENGTH 69 2 02D \ / / | | PC=06 33 ni=01 bp=01 33-06 Note LENGTH is relative If operand is absolute +LDT #MAXLEN 75 1 01000 \ \ ni=01 e=1 bp=00 MAXLEN EQU 4096 L1-L2 Slide S2-08 indirect addressing 70 002A J @RETADR 3E 2 003 \ \ / / | PC=2D 30 ni=10 bp=01 30-2D ====================================================================== Slide S2-09 Section 2.2.2 Program Relocation Absolute program (Absolute assembly) program must be loaded starting at a specific address Relocatable program program can be loaded anywhere . assembler assembles the program as if the program is to be loaded starting at address 0 . assembler provides information for the loader indicating the address fields (of the instructions) that need to be modified at load time - only direct addresses require modification at load times; relative addresses don't ====================================================================== Slide S2-10 Section 2.3.1 Literals LDA =C'ABC' LDA =X'0A' Literal vs. Immediate operand immediate operand (immediate addressing) the operand value is assembled as part of the instruction literal the assembler generates the specified value as a constant at some other memory location; the address of this generated constant is used in the instruction Literal Pool literal operands used in a program are put into one or more literal pools; normally, literals are placed into a pool at the end of the program LTORG directive useful for keeping the literal operand close to the instruction that uses it Slide S2-11 Data Structure and Logic literal table (LITTAB) LITTBL Pass 1 - put literals in LITTAB - when end of program or LTORG scan LITTAB, assign address Pass 2 - search LITTAB for operand address - put literals in the address specified Slide S2-12 45 001A ENDFIL LDA =C'EOF' 03 2 010 \ / | / PC=1D ni=11 bp=01 / / 2D-1D / \ / PC literal address 002D * =C'EOF' 454F46 ====================================================================== Slide S2-13 Section 2.3.2 Symbol-Defining Statements symbol EQU value assembler puts the symbol and value in symbol table 1000 L1 EQU 500 L2 * L2-L3 Section 2.3.3 Expressions assemblers generally allow +, -, *, / Absolute expression either only absolute terms or if relative terms, they occur in pairs with opposite signs relative terms may not enter into a multiplication or division operation Relative expression relative terms occur in pairs as above; one extra relative term (positive sign) ====================================================================== ======================================================================