Assembly
The LC-3 assembly language
Instructions and operands #
Arithmetic & logic #
ADD
adds two numbersADD R1 R1 #0
sets the condition codes for R1ADD R3 R2 #0
copy R2 to R3
AND
bitwise ANDAND R1 R1 #0
clears R1
Memory #
LEA
calculates memory offset, does not actually load anything, useful to create a pointerLD
load value from memory address specified by offsetLDR
load value from memory adddress specified by register + offestLDI
load value from memory address stored in memory location specified by offestST
store value of register in memory location specified by offsetSTR
store value of register in memory location specified by register + offsetSTI
store value of register in memory address stored in memory location specified by offset
Control #
BR(znp)
branch to memory location in LABEL depending on flagsJMP
unconditionally jumps to memory location specified in registerRET
whenJMP
is used with R7, used to return from subroutines
JSR(R)
savePC
and jump to location, theJSRR
variant uses a register
Trap #
HALT
stops the programGETC
read a character from keyboardIN
read a character from keyboard with promptOUT
write a character to the displayPUTS
write a string to the displayPUTSP
write a string to display, 2 ASCII codes per memory location
Comments #
Comments are prefixed with a semicolumn ;
Assembler Directives #
Also called Pseudo-Ops, not actual instructions but helps the assembler translate the program into machine language. They start with a dot '.' to distinguish them from the other instructions.
.ORIG
where in memory to place the program.END
indicates end of program.FILL
fill memory location.BLKW
blockword, set aside memory.STRINGZ
put a string in memory
Example #
This program prints 'Hello world!' to the console.
.ORIG x3000
; Creates a pointer to the message in R0
LEA R0 MESSAGE
; Prints the string referenced by the address in R0
PUTS
HALT
; Puts the string 'Hello world!' in memory
MESSAGE .STRINGZ "Hello world!"
.END