LC-3
Assembly Language
(Textbook Chapter 7)
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Assembly and assembler
• Machine language - binary
0001110010000110
• Assembly language - symbolic
ADD R6, R2, R6 ; increment index reg.
• Assembler is a program that turns symbols into
machine instructions.
– ISA-specific: close correspondence between symbols
and instruction set
• mnemonics for opcodes
• labels for memory locations
6 - 2
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Syntax of LC-3 assembly:
Language elements
• Instructions (we have seen most of them)
• Comments
• Labels
• Declarations
• Assembler directives and trap codes
Whitespaces (between symbols) and case are ignored.
6 - 3
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Instructions
• One instruction or declaration per line
LABEL OPCODE OPERANDS ; COMMENTS
optional mandatory
6 - 4
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Opcodes and Operands
• Opcodes
– reserved symbols that correspond to LC-3 instructions
– listed in Appendix A (ex: ADD, AND, …)
• Operands
– Registers: Rn, where n is the register number
– Immediate numbers: # (decimal), x (hex), or b (binary)
– Labels: symbolic names of memory locations
– Operands are separated by spaces, tabs, or commas
– Their number, order, and type correspond to the
instruction format
6 - 5
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
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
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Comments
– Anything on a line after a semicolon is a comment
– Comments are ignored by assembler
– Used by humans to document/understand programs
– Tips for useful comments:
• avoid restating the obvious, as “decrement R1”
• provide additional insight, as in “accumulate product in R6”
• use comments to separate pieces of program
6 - 7
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
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
• Can be 1-20 characters in size
6 - 8
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
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 Where to start in placing things in memory
.FILL Declare a memory location (variable)
.BLKW Declare a group of memory locations (array)
.STRINGZ Declare a group of characters in memory (string)
.END Tells assembly where your program source ends
6 - 9
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
.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
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
.FILL
• Declaration and initialization of variables
• One declaration per line
• Always declaring words
• Examples:
flag .FILL x0001
counter .FILL x0002
letter .FILL x0041
letters .FILL x4241
6 - 11
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
In C .FILL
type varname;
Where type is
int (integer)
char (character)
float (floating-point) In LC-3
varname .FILL value
• value is required (initialize)
• type is only 16-bit integer
6 - 12
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
.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
6 - 13
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
.STRINGZ x0048
x0065
• Declare a string of characters
• Automatically terminated with x0000
• Example:
hello .STRINGZ “Hello World!”
6 - 14
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
.END
• Tells the assembler where your program ends
• Only one .END allowed in your program module
• That’s where the assembler stops assembling, NOT
where the execution stops!
6 - 15
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
TRAP
(System Calls)
Very tedious and dangerous for a programmer to deal
with I/O.
This is why we like to have an OS.
Need an instruction to get its attention.
Use the TRAP instruction and a trap vector.
6 - 16
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trap Service Routines
The LC-3 assembler provides “pseudo-instructions” for
each trap code, so you don‟t have to remember them.
Trap Assembler
Usage & Result
Vector Name
x20 GETC Read a character from console into R0, not echoed.
x21 OUT Write the character in R0[7:0] to console.
x22 Write string of characters to console. Start with
PUTS character at address contained in R0. Stops when
0x0000 is encountered.
x23 Print a prompt to console and read in a single
IN
character into R0. Character is echoed.
x24 Write a string of characters to console, 2
characters per address location. Start with
PUTSP
characters at address in R0. First [7:0] and then
[15:0]. Stops when 0x0000 is encountered.
x25 HALT Halt execution and print message to console.
6 - 17
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
To print a character
; the char must be in R0[7:0] Trap
Examples
TRAP x21
or
OUT
To end the program
To read in a character TRAP x25
; will go into R0[7:0], or
; no echo. HALT
TRAP x20
or
GETC
6 - 18
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
.ORIG x3000
LD R2, Zero
Simple LC-3
LD
LD
R0, M0
R1, M1
program
Loop BRz Done
ADD R2, R2, R0
ADD R1, R1, -1
BR Loop
Done ST R2, Res
HALT What does this
Res .FILL x0000 program do?
Zero .FILL x0000 What is in Res at
M0 .FILL x0007 the end?
M1 .FILL x0003
.END
6 - 19
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
The assembly process
• Convert assembly language file (.asm)
into an executable file (.obj) for the LC-3 simulator.
• 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
6 - 20
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
First Pass: The Symbol Table
1. Find the .ORIG statement,
which tells us the address of the first instruction.
• Initialize Location Counter (LC), which keeps track of the
current instruction.
2. For each non-empty line in the program:
a) If line contains a label, add label and LC to symbol table.
b) Increment LC.
– NOTE: If statement is .BLKW or .STRINGZ,
increment LC by the number of words allocated.
3. Stop when .END statement is reached.
NOTE: A line with only a comment is considered an empty line.
6 - 21
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Practice: Symbol Table
Build the symbol table for the .ORIG x3000
x3000 LD R2, Zero
multiply program:
x3001 LD R0, M0
x3002 LD R1, M1
; begin multiply
x3003 Loop BRz Done
Symbol Address x3004 ADD R2, R2, R0
x3005 ADD R1, R1, #-1
x3006 BR Loop
; end multiply
x3007 Done ST R2, Result
x3008 HALT
x3009 Result .FILL x0000
x300A Zero .FILL x0000
x300B M0 .FILL x0007
x300C M1 .FILL x0003
.END
6 - 22
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
2nd Pass: Generating Machine Language
• For each executable assembly language statement,
generate the corresponding machine language instruction.
– If operand is a label,
look up the address from the symbol table.
• Potential problems:
– Improper number or type of arguments
• ex: NOT R1,#7
ADD R1,R2
– Immediate argument too large
• ex: ADD R1,R2,#1023
– Address (associated with label) more than 256 from
instruction
• can‟t use PC-relative addressing mode
6 - 23
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
The LC-3 Assembler
• Using “assemble” (Unix) or LC3Edit (Windows),
generates several different output files.
6 - 24
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Multiple Object Files
• An object file is not necessarily a complete program.
– system-provided library routines
– code blocks written by multiple developers
• For LC-3 simulator, can load multiple object files into
memory, then start executing at a desired address.
– system routines, such as keyboard input, are
loaded automatically
• loaded into “system memory,” below x3000
• user code should be loaded between x3000 and xFDFF
– each object file includes a starting address
– be careful not to load overlapping object files
6 - 25
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Linking
Linking is the process of resolving symbols
between independent object files.
– Suppose we define a symbol in one module,
and want to use it in another
– The directive .EXTERNAL is used to tell the
assembler that a symbol is defined in another module
– The linker will search symbol tables of other modules
to resolve symbols and complete code generation
before loading
6 - 26
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
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
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Running
• The loader makes the CPU jump to the first
instruction -> .ORIG.
• The program executes
• When execution completes, control returns
to the OS or to the simulator
• Load again to run again with different data
(in LC3 we must assemble again, since data is
in program)
6 - 28
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Recommended exercises:
• Ex 7.1 to7.5, 7.7 to 7.9
• Especially recommended: 7.13 to 7.15, and
7.18 to 7.24 (yes, all of them except 7.16
and 7.17)
6 - 29
CMPE12 – Winter 2009 – Joel Ferguson
CuuDuongThanCong.com https://fb.com/tailieudientucntt