L08 [Assembly] - Skyline-9/CS2110-Notes GitHub Wiki
L08 [Assembly]
Anytime you have a PC relative addressing mode (e.g. PCOffset), we use a label.
The Assembly Process
- Objective
- Translate the Assembly Language program into ML (Machine Language)
- Each AL instruction yields one ML instruction word
- Interpret pseudo-ops correctly
- Problem
- An instruction may reference a label
- If the label hasn't been encountered yet, the assembler can't form the instruction word
- Solution
- Two pass assembly
First Pass [Generate Symbol Table]
- Scan each line
- Keep track of current address (location counter)
- Increment by 1 for each instruction
- Adjust the location counter as required for any pseudo-ops (e.g. .fill or .stringz)
- For each label, enter it into the symbol table along with the current address
- Stop when .end is encountered
Second Pass [Generate the Machine Language Program]
- Scan each line again
- Translate each AL instruction into ML
- Look up symbols in the symbol table
- Ensure that labels are no more than +256 / -255 lines from PCOffset9 instructions
- Calculate operand field for the instruction
- Update the current address (location counter)
- Fill memory locations as directed by pseudo-ops
- Stop when .end is encountered
Aliases
TRAP pseudo-instruction
- The assembler will recognize aliases for certain predefined trap instructions:
Alias | Instruction | |
---|---|---|
TRAP x25 | HALT | Stop the CPU |
TRAP x23 | IN | Input character from keyboard |
TRAP x22 | PUTS | Out |
TRAP x24 | PUTSP | |
TRAP x20 | GETC |
RET alias
RET is just an alias for JMP R7
Need LD?
LD R1, Data
...
DATA. fill 5
Just becomes
LEA R1, DATA
LDR R1, R1, #0
...
DATA .fill 5
Need LDI?
LDI R1, ADDR
...
ADDR .fill xFE00
becomes
LEA R1, ADDR
LDR R1, R1, #0
LDR R1, R1, #0
...
ADDR .fill xFE00
Converting Between Assembly And Hex
.ORIG x3000
LEA R0, ARRAY
LDR R1, R0, #5
ADD R1, R1, #4
ST R1, ARRAY
HALT
.END
.ORIG x3020
ARRAY
.fill 7
.fill 2
.fill 9
.fill 10
.fill 4
.fill 11
Try converting ST R1, ARRAY
into its hexadecimal value!
Solution
- Let's start by looking at the op code for ST
- The op code for ST is
0011
- The op code for ST is
- The SR is R1, which is
001
- Now, we just have to calculate PCOffset9
- ARRAY is at
0x3020
- Current PC is
0x3004
- ARRAY - current PC =
0x1C
=0b000011100
- ARRAY is at
- Putting this together, we get
0011-001-000011100
or0xE60F