Forward References | Back-Patching
The assembler implements the backpatching technique as follows:
- It builds a table of incomplete instructions (TII) to record information about instructions whose operand fields left blank.
- Each entry in this table contains a pair of the form (instruction address, symbol) to indicate that the address of symbol should put in the operand field of the instruction with the address instruction address.
- By the time the END statement processed, the symbol table would contain the addresses of all symbols defined in the source program and TII would contain information describing all forward references.
- The assembler can now process each entry in TII to complete the concerned instruction.
- Alternatively, entries in TII can process on the fly during normal processing.
- In this approach, all forward references to a symbol i would be processed when the statement that defines symbol encountered.
- The instruction corresponding to the statement
MOVER BREG, ONE
contains a forward reference to ONE.
- Hence the assembler leaves the second operand field blank in the instruction that assembled to reside in location 101 of memory and makes an entry (101, ONE) in the table of incomplete instructions (TII).
- While processing the statement
ONE DC ‘1’
address of ONE, which 115, entered in the symbol table.
- After the END statement processed, the entry (101, ONE) would be processed by obtaining the address of ONE from the symbol table and inserting it in the second operand field of the instruction with assembled address 101.