- Instruction
- A instruction
- C instruction
- White space -> ignore
- Symbols
- A instructions
- Labels
- Predefined symbols
// example 1
@21
// example 2
@x
value
is decimal constant- Convert to 15-bit binary equivalent and
leftPad
a0
to denote an A instruction
- Convert to 15-bit binary equivalent and
value
is a symbol- Discussed in symbol handling
// example
MD = D + 1
- All
C instructions
start with111
- Next
7 bits
correspond tocomp
- Refer table for exact code
- Next
3 bits
correspond todest
- Refer table for exact code
- Next
3 bits
correspond todest
- Refer table for exact code
- Add all the label symbols to the symbol table
- Add all the variable symbols to the symbol table if they haven't been encountered before
- To resolve a symbol, look it up from the symbol table
// example 1
@R0
// example 2
@KBD
Convert to corresponding value
// example 1
(LOOP)
// example 2
@STOP
(XXX)
will refer to the next instruction in the program@labelSymbol
can just be replaced with its corresponding value
// example
@i
- The variable gets mapped to an address in the RAM starting from
16
at first occurrence - If the variable has already been seen, it gets replaced by the corresponding value
- Initialisation
- First pass
- Scan the entire program
- For instruction of the form
(XXX)
- Add the pair
(XXX, address)
to the symbol table, whereaddress
is the number of the instruction following(XXX)
- Add the pair
- Second pass
- Set
n = 16
- Scan the entire program again
- For each instruction
- If the instruction is
@symbol
, look up thesymbol
in the symbol table- If
(symbol, value)
is found, usevalue
to complete the instruction's translation - If not found
- Add
(symbol, n)
to the symbol table - Use
n
to complete the instruction translation n++
- Add
- If
- If the instruction is a C-instruction, complete the translation
- Write the instruction in the output file
- If the instruction is
- Set