Chapter 7: Assembly Language
The LC-3 Assembly Language
Base Format
LABEL OPCODE OPERANDS ; COMMENTS
Opcodes and operands
ADD
Encoding:
0001 DR SR1 0 00 SR2
0001 DR SR1 1 imm5
Assembler format:
ADD DR, SR1, SR2
ADD DR, SR1, imm5
AND
Encoding:
0101 DR SR1 0 00 SR2
0101 DR SR1 1 imm5
Assembler format:
AND DR, SR1, SR2
AND DR, SR1, imm5
NOT
Encoding: 1001 DR SR 1 11111
Assembler format: NOT DR, SR
LEA
Encoding: 1110 DR PCoffset9
Assembler format: LEA DR, LABEL
LD
Encoding: 0010 DR PCoffset9
Assembler format: LD DR, LABEL
LDI
Encoding: 1010 DR PCoffset9
Assembler format: LDI DR, LABEL
LDR
Encoding: 0110 BaseR PCoffset9
Assembler format: LDR DR, BaseR, offset6
ST
Encoding: 0011 SR PCoffset9
Assembler format: ST SR, LABEL
STI
Encoding: 1011 SR PCoffset9
Assembler format: STI SR, LABEL
STR
Encoding: 0111 BaseR PCoffset9
Assembler format: STR SR, BaseR, offset6
JMP
Encoding: 1100 000 BaseR 000000
Assembler format: JMP BaseR
Note: RET
is JMP R7
, when BaseR
is 111
.
BR
Encoding: 0000 n z p PCoffset9
Assembler format: BR(n)(z)(p) LABEL
JSR / JSRR
Encoding:
0100 1 PCoffset11
0100 0 00 BaseR 000000
Assembler format:
JSR LABEL
JSRR BaseR
TRAP
Encoding: 1111 0000 trapvect8
Assembler format: TRAP trapvect8
RTI
Encoding: 1000 000000000000
Assembler format: RTI
Pseudo-ops (Assembler Directives)
.ORIG
.ORIG
tells the assembler where to put the first instruction of the program in memory.
.FILL e.g. .FILL xABCD
.FILL
tells the assembler to set aside the next location and fill it with the value of its operand.
.BLKW e.g. .BLKW #15
.BLKW
tells the assembler to set aside a number of sequential locations, specified by its operand.
.STRINGZ e.g. .STRINGZ "Hello!"
.STRINGZ
tells the assembler to initialize the next n+1 location with its operand, which is a string of length n, and the final location is initialized with x0000
.
.END
.END
tells the assembler that the assembly program ends here. Contents after it will be neglected.
The Assembly Process: A Two-Pass Process
1. Creating the Symbol Table
During the first pass, the assembler establishes correspondences of labels with their 16-bit memory addresses. It creates a symbol table for reference.
The current location is tracked by a location counter (LC). LC is incremented in every line corresponding to an actual instruction.
2. Generating the Machine Language Program
During the second pass, the assembler goes through the assembly program again with the help of the symbol table, and translates each line into machine instructions. It subtracts LC+1 (the incremented PC) from the corresponding address of the label to get the offset. If the offset is too big to fit in the instruction, an assembly error occurs.
留下评论
注意 评论系统在中国大陆加载不稳定。