Tutorial 5
Tutorial 5
Symbol table and machine code generation
Symbol table
The symbol table contains information to locate and relocate symbolic definitions and references.
Showing the mapping between labels and addresses
Symbol Address
label1 x30F0
label2 x30F3
label3 x30F4
label4 x30F5
label5 x30F6
Example 1
Create the symbol table for the following assembly program.
.ORIG x2000
;The first instruction is at x2000
LD R1, SIX
LD R2, NUM
AND R3, R3, #0
LOOP ADD R3, R3, R2
ADD R1, R1, #-1
BRp LOOP
HALT
STR .STRINGZ “HELLO”
NUM .FILL #2
ARR .BLKW 3
SIX .FILL x0006
.END
Solution
Step 1: identify and list out all the labels
Symbol Address
LOOP
STR
NUM
ARR
SIX
Solution
Step 2: initialize location counter (LC) by looking at .ORIG statement
Set the LC to x2000
.ORIG x2000
;The first instruction is at x2000
LD R1, SIX
LD R2, NUM
AND R3, R3, #0
LOOP ADD R3, R3, R2
ADD R1, R1, #-1
BRp LOOP
HALT
STR .STRINGZ “HELLO”
NUM .FILL #2
ARR .BLKW 3
SIX .FILL x0006
.END
Solution
Step 3: Starting counting the LC until reaching the first label
.ORIG x2000
;The first instruction is at x2000
LD R1, SIX
LD R2, NUM
AND R3, R3, #0
LOOP ADD R3, R3, R2
ADD R1, R1, #-1
BRp LOOP
HALT
STR .STRINGZ “HELLO”
NUM .FILL #2
ARR .BLKW 3
SIX .FILL x0006
.END
Symbol Address
LOOP x2003
STR
NUM
ARR
SIX
Solution
Repeat step 3 and find out the address for the second label
Symbol Address
LOOP x2003
STR x2007
NUM
ARR
SIX
.ORIG x2000
;The first instruction is at x2000
LD R1, SIX
LD R2, NUM
AND R3, R3, #0
LOOP ADD R3, R3, R2
ADD R1, R1, #-1
BRp LOOP
HALT
STR .STRINGZ “HELLO”
NUM .FILL #2
ARR .BLKW 3
SIX .FILL x0006
.END
Solution
Since the label “STR” is a string, the assembler will allocate n+1 memory location for it.
Each character occupies a memory location (incl. null character).
For string “HELLO”, it has 6 characters.
Therefore, the next label “NUM” should be at x2007+6 = x200D
Symbol Address
LOOP x2003
STR x2007
NUM x200D
ARR
SIX
.ORIG x2000
;The first instruction is at x2000
LD R1, SIX
LD R2, NUM
AND R3, R3, #0
LOOP ADD R3, R3, R2
ADD R1, R1, #-1
BRp LOOP
HALT
STR .STRINGZ “HELLO”
NUM .FILL #2
ARR .BLKW 3
SIX .FILL x0006
.END
Solution
The 4th label is “ARR”
.ORIG x2000
;The first instruction is at x2000
LD R1, SIX
LD R2, NUM
AND R3, R3, #0
LOOP ADD R3, R3, R2
ADD R1, R1, #-1
BRp LOOP
HALT
STR .STRINGZ “HELLO”
NUM .FILL #2
ARR .BLKW 3
SIX .FILL x0006
.END
Symbol Address
LOOP x2003
STR x2007
NUM x200D
ARR x200E
SIX
Solution
Since the label “ARR” is .BLKW, the assembler will allocate n memory location for it.
n is 3 in this case
Therefore, the next label “SIX” should be at x200E+3 = x2011
.ORIG x2000
;The first instruction is at x2000
LD R1, SIX
LD R2, NUM
AND R3, R3, #0
LOOP ADD R3, R3, R2
ADD R1, R1, #-1
BRp LOOP
HALT
STR .STRINGZ “HELLO”
NUM .FILL #2
ARR .BLKW 3
SIX .FILL x0006
.END
Symbol Address
LOOP x2003
STR x2007
NUM x200D
ARR x200E
SIX x2011
Tips
Don’t count the comment line
Be careful when handling .BLKW and .STRINGZ, it can occupy more than 1 location.
Machine code generation
Find out the machine code for the highlighted instructions.
.ORIG x2000
;The first instruction is at x2000
LD R1, SIX
LD R2, NUM
AND R3, R3, #0
LOOP ADD R3, R3, R2
ADD R1, R1, #-1
BRp LOOP
HALT
STR .STRINGZ “HELLO”
NUM .FILL #2
ARR .BLKW 3
SIX .FILL x0006
.END
Solution
LD R1, SIX
PC = x2000
PCoffset9 = Label “SIX” – (PC+1)
= x2011 – (x2000 + 1)
= 16
= b000010000
Machine code format
Opcode DR PCoffset9
[15:12] [11:9] [8:0]
0010 001 000010000
Symbol Address
LOOP x2003
STR x2007
NUM x200D
ARR x200E
SIX x2011
Solution
LD R2, NUM
PC = x2001
PCoffset9 = Label “NUM” – (PC+1)
= x200D – (x2001 + 1)
= 11
= b000001011
Machine code format
Symbol Address
LOOP x2003
STR x2007
NUM x200D
ARR x200E
SIX x2011
Opcode DR PCoffset9
[15:12] [11:9] [8:0]
0010 010 000001011
Solution
BRp LOOP
PC = x2005
PCoffset9 = Label “LOOP” – (PC+1)
= x2003 – (x2005 + 1)
= -3
= b111111101
Machine code format
Symbol Address
LOOP x2003
STR x2007
NUM x200D
ARR x200E
SIX x2011
Opcode n z p PCoffset9
[15:12] [11] [10] [9] [8:0]
0000 0 0 1 111111101
/docProps/thumbnail.jpeg