06 LC3 Assembly
06 LC3 Assembly
06 LC3 Assembly
Assembly Language
(Textbook Chapter 7)
R6, R2, R6
6 - 2
6 - 3
Instructions
One instruction or declaration per line
LABEL OPCODE OPERANDS ; COMMENTS
optional
mandatory
6 - 4
Operands
6 - 5
Data types
LC-3 has 2 basic data types
Integer
Character
Both are 16 bits wide (a word), though a
character is only 8 bits in size.
6 - 6
Comments
6 - 7
Labels
Placed at beginning of line
Assign a symbolic name to their line (its address)
Symbolic names used to identify memory locations. Two
kinds:
Location of target of a branch or jump
Location of a variable for loading and storing
6 - 8
Assembler directives
Directives or psuedo-ops give information to the
assembler.
Not executed by the program
All directives start with a period .
Directive
Description
.ORIG
.FILL
.BLKW
.STRINGZ
.END
6 - 9
.ORIG
Tells simulator where to put your code in
memory (starting location)
Only one .ORIG allowed per program module
PC is set to this address at start up
Similar to the main() function in C
Example: the standard convention is
.orig
x3000
6 - 10
.FILL
Declaration and initialization of variables
One declaration per line
Always declaring words
Examples:
flag
counter
letter
letters
.FILL
.FILL
.FILL
.FILL
6 - 11
x0001
x0002
x0041
x4241
In C
type
Where
int
char
float
varname;
.FILL
type is
(integer)
(character)
(floating-point) In LC-3
varname .FILL value
6 - 12
.BLKW
Reserves (and initializes) a sequence of contiguous
memory locations (arrays)
Examples:
;set aside 3 locations
.BLKW
3
;set aside 1 location and label it
Bob .BLKW
1
; set aside 7 locations,
; label them, and init them all to 4
Num .BLKW
7
#4
CMPE12 Winter 2009 Joel Ferguson
6 - 13
.STRINGZ
Declare a string of characters
Automatically terminated with x0000
Example:
hello .STRINGZ Hello World!
6 - 14
x0048
x0065
.END
Tells the assembler where your program ends
Only one .END allowed in your program module
Thats where the assembler stops assembling, NOT
where the execution stops!
6 - 15
TRAP
(System Calls)
Very tedious and dangerous for a programmer to deal
with I/O.
This is why we like to have an OS.
6 - 16
Assembler
Name
x20
GETC
x21
OUT
x22
PUTS
x23
IN
x24
PUTSP
x25
HALT
6 - 17
To print a character
; the char must be in R0[7:0]
TRAP x21
or
Trap
Examples
OUT
or
GETC
CMPE12 Winter 2009 Joel Ferguson
6 - 18
TRAP x25
or
HALT
Loop
Done
Res
Zero
M0
M1
.ORIG
LD
LD
LD
BRz
ADD
ADD
BR
ST
HALT
.FILL
.FILL
.FILL
.FILL
.END
x3000
R2, Zero
R0, M0
R1, M1
Done
R2, R2, R0
R1, R1, -1
Loop
R2, Res
x0000
x0000
x0007
x0003
Simple LC-3
program
6 - 19
First Pass:
scan program file
find all labels and calculate the corresponding
addresses - the symbol table
Second Pass:
convert instructions to machine language, using
information from symbol table
CMPE12 Winter 2009 Joel Ferguson
6 - 20
6 - 21
Symbol
Address
x3000
x3001
x3002
x3003
x3004
x3005
x3006
x3007
x3008
x3009
x300A
x300B
x300C
6 - 22
.ORIG x3000
LD
R2, Zero
LD
R0, M0
LD
R1, M1
; begin multiply
Loop
BRz
Done
ADD
R2, R2, R0
ADD
R1, R1, #-1
BR
Loop
; end multiply
Done
ST
R2, Result
HALT
Result .FILL x0000
Zero
.FILL x0000
M0
.FILL x0007
M1
.FILL x0003
.END
6 - 23
6 - 24
6 - 25
Linking
Linking is the process of resolving symbols
between independent object files.
6 - 26
Loading
Loading is the process of copying an executable image
into memory.
more sophisticated loaders are able to relocate
images to fit into available memory
must readjust branch targets, load/store addresses
6 - 27
Running
The loader makes the CPU jump to the first
instruction -> .ORIG.
The program executes
6 - 28
Recommended exercises:
6 - 29