Assembler Directives and the Symbol Table

Download Report

Transcript Assembler Directives and the Symbol Table

Assembly Language Programming
Assembler Directives
and
The Symbol Table
CEG 320/520: Computer Organization and Assembly Language Programming
1
Assembler Directives: Introduction
• Assembler directives give information to the assembler.
• They can be used to declare variables, create storage space for
results, to declare constants, and to label locations in the code to
be used as branch destinations.
• Most assemblers are ‘two-pass assemblers’.
– They read through the code once, looking for labels, and use them to
create the ‘symbol table’.
• The symbol table is a mapping between labels and values.
– On the second pass, they replace each occurrence of a label with its value
from the symbol table.
• Coding tip: Labels MUST be left justified, not indented. First
character must be a letter (not a number). Assembler only looks
at first 8 characters.
CEG 320/520: Computer Organization and Assembly Language Programming
2
Assembler Directives: Origin (ORG)
• Tells the assembler where to place your code in
memory.
• Operand given is the desired memory location.
–
ORG $001000
; sets origin to be $001000
• Generally, we’ll put our code at $001000 and our data
(declared with DC, DS, etc) at $002000.
CEG 320/520: Computer Organization and Assembly Language Programming
3
Assembler Directives: Declare Constant (DC)
• Declares a named variable in memory.
• The term ‘constant’ is misleading, because the contents
of that memory location can be changed.
• Use DC.B, DC.W, DC.L to declare different sizes of
variables.
• Operand given is initial value of variable.
– VAR DC.W $40 ; places $40 in memory location pointed
; to by VAR.
• Symbol table: the value of a label declared with DC is
the memory location of the label.
CEG 320/520: Computer Organization and Assembly Language Programming
4
Assembler Directives: Declare Storage (DS)
• Reserves space in memory for the result of a future
operation or future variables.
• Use DS.B, DS.W, DS.L to declare different sizes of
empty storage.
• Operand given is number of spaces to allocate, where
the size of a space is determined by the extension
(B,W,L)
– ARR DS.W 5
; reserves 5 words in memory for ARR
• Symbol table: the value of a label declared with DS is
the memory location of the label.
CEG 320/520: Computer Organization and Assembly Language Programming
5
Assembler Directives: equal to (EQU)
• Declares a constant value.
• No need for extension here, assembler will treat the
number appropriately.
• Operand given is value to assign to label.
– CONST EQU 5
; sets the value of CONST to 5
• Symbol table: the value of a label declared with EQU is
the value, NOT THE MEMORY LOCATION.
• EQU does not cause anything to be stored in memory.
CEG 320/520: Computer Organization and Assembly Language Programming
6
Assembler Directives: Code labels
• Labels an instruction in the program so that it can be
used as a branch target.
• All instructions are indented; a code label is added by
putting a left justified label on the same line.
– LOOP ADD D1,D0
; LOOP is the code label
• Symbol table: the value of a code label is the memory
location of the instruction on that line.
CEG 320/520: Computer Organization and Assembly Language Programming
7
Assembler Directives: end of code (END)
• Tells the assembler to stop looking for more code.
• This should be the last line in any code file you write.
CEG 320/520: Computer Organization and Assembly Language Programming
8
The Symbol Table
• Most assemblers are two-pass assemblers.
– On the first pass, any labels found in the program are put into the symbol
table along with the corresponding numerical value.
• Code labels – memory location of instruction
• Variable labels (DS, DC) – memory location of reserved memory
• Constants (EQU) – value of constant
– On the second pass, all labels in the code are replaced with their values
from the symbol table.
• Why use two passes?
– If the program contains a branch to a later location in the code (a forward
branch), the assembler won’t know where to find the branch target
because it would not have seen it yet.
CEG 320/520: Computer Organization and Assembly Language Programming
9
Example: An Assembly Language Program
VAL1
VAL2
CONST
ORG
MOVE
MOVE
MUL
ADD
END
ORG
DC
DC
EQU
$001000
VAL1, D0
VAL2, D1
D0, D1
#CONST, D1
$002000
3
12
5
• What does the symbol table look like after the first
pass of the assembler?
• What does the code look like after the second pass
of the assembler?
CEG 320/520: Computer Organization and Assembly Language Programming
10
Example: An Assembly Language Program
ORG
$001000
START
CLR.L
D0
; sum = 0;
MOVEA.L #ARRAY,A0
; ptr points to the 1st array element
MOVE.W
#SIZE,D1
; counter = SIZE;
LOOP
ADD.W
(A0)+,D0
; sum += *ptr++;
SUBQ.W
#1,D1
; counter -= 1;
BGT
LOOP
; repeat while counter > 0;
MOVE.W
D0,SUM
; Save the sum in memory
MOVE.W
#228,D7
; Magic exit code
TRAP
#14
; more magic exit code
;--------DATA AND CONSTANTS-------------------------------------------ORG
$002000
SIZE
EQU
9
; the size of the array
ARRAY
DC.W
3,7,-5,12,23,15,-12,82,5 ; the array to sum
SUM
DS.W
1
; the final sum
END
• What does the symbol table look like after the first
pass of the assembler?
• What does the code look like after the second pass
of the assembler?
CEG 320/520: Computer Organization and Assembly Language Programming
11