written 2.7 years ago by |
Assembler translate assembly language programs to machine
code is called an Assembler.
• One-pass assemblers are used when
it is necessary or desirable to avoid a second pass over the source program the external storage for the intermediate file between two passes is slow or is inconvenient to use Main problem: forward references to both data and instructions One simple way to eliminate this problem: require that all areas be defined before they are referenced. It is possible, although inconvenient, to do so for data items. Forward jump to instruction items cannot be easily eliminated.
Data structures for assembler:
Op code table
Looked up for the translation of mnemonic code
key: mnemonic code
result: bits
Hashing is usually used
once prepared, the table is not changed
efficient lookup is desired
since mnemonic code is predefined, the hashing function can be tuned a priori
The table may have the instruction format and length
to decide where to put op code bits, operands bits, offset bits
for variable instruction size
used to calculate the address
Symbol table
Stored and looked up to assign address to labels
efficient insertion and retrieval is needed
deletion does not occur
Difficulties in hashing
non random keys
Problem
the size varies widely
pass 1: loop until the end of the program
1. Read in a line of assembly code
2. Assign an address to this line
increment N (word addressing or byte addressing)
3. Save address values assigned to labels
in symbol tables
4. Process assembler directives
constant declaration
space reservation
Algorithm for Pass 1 assembler:
begin
if starting address is given
LOCCTR = starting address;
else
LOCCTR = 0;
while OPCODE != END do ;; or EOF
begin
read a line from the code
if there is a label
if this label is in SYMTAB, then error
else insert (label, LOCCTR) into SYMTAB
search OPTAB for the op code
if found
LOCCTR += N ;; N is the length of this instruction (4 for MIPS)
else if this is an assembly directive
update LOCCTR as directed
else error
write line to intermediate file
end
program size = LOCCTR - starting address;
end
Load-and-go assembler • Load-and-go assembler generates their object code in memory for immediate execution. • No object program is written out, no loader is needed. • It is useful in a system oriented toward program development and testing such that the efficiency of the assembly process is an important consideration
Forward Reference: Load-and-go assembler
Omits the operand address if the symbol has not yet been defined Enters this undefined symbol into SYMTAB and indicates that it is undefined Adds the address of this operand address to a list of forward references associated with the SYMTAB entry Scans the reference list and inserts the address when the definition for the symbol is encountered. Reports the error if there are still SYMTAB entries indicated undefined symbols at the end of the program Search SYMTAB for the symbol named in the END statement and jumps to this location to begin execution if there is no error.