Assemblers Syllabus:: Basic Assembler Functions
Assemblers Syllabus:: Basic Assembler Functions
Unit II
ASSEMBLERS Syllabus :
2.1 Basic Assembler Functions A simple SIC assembler Assembler Algorithm and Data Structures 2.2 Machine-Dependent Assembler Features Instruction Formats and Addressing Modes Program Relocation 2.3 Machine-Independent Assembler Features Literals Symbol-Definition Statements Expressions 2.4 Assembler Design Options One-pass Multi-pass 2.5 Implementation MASM Assembler Basic Assembler Functions Example SIC Assembler Language Program
1 / 10
Unit II
From the above shows an assembler language program for SIC. The line numbers are for reference only. Indexing addressing is indicated by adding the modifier ,X Lines beginning with .contain comments only. Reads records from input device (code F1) Copies them to output device (code 05) At the end of the file, writes EOF on the output device, then RSUB to the operating system
2 / 10
Unit II Pseudo-Instructions Not translated into machine instructions Providing information to the assembler Assembler Directives START -Specify name and starting address for the program. END -Indicate end of the source program and specify first executable instruction in the program. BYTE Generate character or hexadecimal constant. WORD Generate one word integer constant. RESB -Reserve indicated number of bytes for a data area. RESW -Reserve indicated number of words for a data area. Data transfer (RD, WD) A buffer is used to store record Buffering is necessary for different I/O rates The end of each record is marked with a null character (0016) Buffer length is 4096 Bytes The end of the file is indicated by a zero-length record When the end of file is detected, the program writes EOF on the output device and terminates by RSUB. Subroutines (JSUB, RSUB) RDREC, WRREC Save link (L) register first before nested jump.
From the below program shows the generated object code for each statement. Loc gives the machine address in Hex. Assume the program starting at address 1000. Translation functions Translate STL to 14. Translate RETADR to 1033. Build the machine instructions in the proper format (,X). Translate EOF to 454F46. Write the object program and assembly listing. 3 / 10
Unit II
4 / 10
Unit II A forward reference 10 1000 FIRST STL RETADR 141033 A reference to a label (RETADR) that is defined later in the program Most assemblers make two passes over the source program Most assemblers make two passes over source program. Pass 1 scans the source for label definitions and assigns address (Loc). Pass 2 performs most of the actual translation. The functions of the two passes assembler. Pass 1 (define symbol) Assign addresses to all statements (generate LOC). Check the correctness of Instruction (check with OP table). Save the values (address) assigned to all labels into SYMBOL table for Pass 2. Perform some processing of assembler directives. Pass 2 Assemble instructions (op code from OP table, address from SYMBOL table). Generate data values defined by BYTE, WORD. Perform processing of assembler directives not done during Pass 1. Write the OP (Fig. 2.3) and the assembly listing (Fig. 2.2). The object program (OP) will be loaded into memory for execution. Three types of records Header: program name, starting address, length. Text: starting address, length, object code. End: address of first executable instruction. Steps for generation of Object Program : Header record: o Col. 1 H o Col. 2-7 Program name o Col. 8-13 Starting address of object program (hexadecimal) o Col. 14-19 Length of object program in bytes (hexadecimal) Text record: o Col. 1 T o Col. 2-7 Starting address for object code in this record (hexadecimal) o Col. 8-9 Length of object code in this record in bytes (hexadecimal) o Col. 10 69 Object code, represented in hexadecimal. (6910+1)/6=10 instructions End record: o Col. 1 E o Col. 2-7 Address of first executable instruction in object program (hexadecimal) 5 / 10
Unit II
Object Program for the above SIC Program:
6 / 10
Unit II Assembler Algorithm Pass 1 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. (This includes processing that affects address assignment, such as determining the length of data areas defined by BYTE, RESW, etc. Pass 1 Algorithm: begin read first input line if OPCODE = 'START' then begin save #[OPERAND] as starting address initialized LOCCTR to starting address write line to intermediate file read next input line end {if START} else initialized LOCCTR to 0 while OPCODE != 'END' do begin if this is not a comment line then begin if there is a symbol in the LABEL field then begin search SYMTAB for LABEL if found then set error flag (duplicate symbol) else insert (LABEL, LOCCTR) into SYMTAB end {if symbol} search OPTAB for OPCODE if found then add 3 {instruction lengh} to LOCCTR else if OPCODE = 'WORD' then add 3 to LOCCTR else if OPCODE = 'RESW' then add 3 * #[OPERAND] to LOCCTR else if OPCODE = 'RESB' then add #[OPERAND] to LOCCTR else if OPCODE = 'BYTE' then begin find length of constant in bytes add length to LOCCTR end {if BYTE} else set error flag (invalid operation code) end {if not a comment} 7 / 10
Unit II write line to intermediate file read next input line end {while not END} write last line to intermediate file save (LOCCTR - starting address) as program length end Example :
Assembler Algorithm Pass 2 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
8 / 10
Unit II Pass 2 Algorithm: begin read first input file {from intermediate file} if OPCODE = 'START' then begin write listing line read next input line end {if START} write header record to object program initialized first Text record while OPCODE != 'END' do begin if this is not a comment line then begin search OPTAB for OPCODE if found then begin if there is a symbol in OPERAND field then begin search SYMTAB for OPERAND if found then store symbol value as operand address else begin store 0 as operand address set error flag (undefined symbol) end end {if symbol} else store 0 as operand address assemble the object code instruction end {if opcode found} else if OPCODE = 'BYTE' or 'WORD' then convert constant to object code if object code not fit into the current Text record then begin write Text record to object program initialized new Text record end add object code to Text record end {if not comment} write listing line read next input line end {while not END} write last Text record to object program write End record to object program write last listing line end 9 / 10
Unit II
Example :
10 / 10