LC-2 Assembly Language Documentation

<- Back to the documentation index

The assembly language recognized by lc2asm.js is closely modeled on the one used in Introduction to Computing Systems: from Bits & Gates to C and Beyond by Yale N. Patt and Sanjay J. Patel. Matt Postiff at U Michigan developed a series of notes about the LC-2 which may also be of help.

Number Specifications

Decimal numbers can be specified by #123 or 123. Binary number can be specified as %101, b101, or B101. Hexadecimal numbers can be specified as $fade, xfade, Xfade, 0xfade, or 0Xfade. Case is not sensitive.

Note that negative numbers are represented with the base specifier first, then the sign e.g. #-5 or $-a.

The LC-2 Computer

The LC-2 computer has 8 general purpose registers (R0, R1, ..., R7), and 65535 (0xffff) memory locations. The CPU has a Program Counter (PC), also known as an instruction pointer, whose value is the memory address of the next instruction to be executed. The CPU also has 3 conditional flags: negative, zero, positive; whose values are set to the sign of the result of the previous operation (for some operations).

Memory addresses 0x0000 to 0x0fff are reserved for the Operating System. 0x1000 to 0x2fff, and 0xd000 to 0xefff are reserved. 0xf000 to 0xfbff are reserved for memory mapped I/O. 0xfc00 to 0xffff are reserved for Boot ROM.

The lc2asm.js Assembler

Note that the lc2asm.js assembler instructions are not case-sensitive, and differs slightly from the original LC-2 assembler presented in the above book and notes. Line labels must be followed by a colon.

STR1:    .STRINGZ "The label on this line is valid."
STR2     .STRINGZ "The label on this line is NOT valid."
    

Trap Vectors

0x20
Read a single character from the keyboard. This character is not echoed to the console. This character's ASCII code is copied to R0. The high 8 bits of R0 are cleared.
0x21
Print the ASCII character whose value is in R0 to console output.
0x22
Write the string whose address is in R0 to the console.
0x23
Print a prompt to the console, then read a single character from the keyboard. This character is echoed to the console. This character's ASCII code is copied to R0. The high 8 bits of R0 are cleared.
0x25
Stop the computer.

Assembler Directives

.ORIGIN Loc
Loc is a memory location which specifies where the next instruction should be placed in memory, and following instructions should follow this address accordingly. Without this directive, the first instruction is placed by default at 0x3000.
.FILL Val
Fill this memory location with Val.
.STRINGZ "Str"
Str is a string of length n. Fill the next n+1 memory locations with the characters of Str, followed by a zero value.
.BLKW N Val
N is a number of memory locations. Fills the next N memory locations with Val.
.END
This simply signals to the assembler that the code has ended, and no following code will be assembled.

Assembly Macro Instructions

GETC
Equivalent to TRAP $20.
OUT
Equivalent to TRAP $21.
PUTS
Equivalent to TRAP $22.
IN
Equivalent to TRAP $23.
HALT
Equivalent to TRAP $25.

Assembly CPU Instructions

ADD Reg_dest, Reg_src1, Reg_src2
Add the values in Reg_src1 and Reg_src2 and put the result in Reg_dest. Sets the conditional flags.
ADD Reg_dest, Reg_src1, Val5
Add the values in Reg_src1 and the 5-bit value Val5, and put the result in Reg_dest. Sets the conditional flags.
AND Reg_dest, Reg_src1, Reg_src2
Logically AND the values in Reg_src1 and Reg_src2 and put the result in Reg_dest. Sets the conditional flags.
AND Reg_dest, Reg_src1, Val5
Logically AND the values in Reg_src1 and the zero-extended 5-bit value Val5, and put the result in Reg_dest. Sets the conditional flags.
NOP
Does nothing.
BR Mem9
Jump to the memory address Mem9 within this memory page, if nothing. Effectively the same as NOP. In fact, the NOP assembly instruction is translated to a BR with a zero value for Mem9.
BRn Mem9
Jump to the 9-bit memory address Mem9 within this memory page, if the negative conditional flag is set.
BRz Mem9
Jump to the 9-bit memory address Mem9 within this memory page, if the zero conditional flag is set.
BRp Mem9
Jump to the 9-bit memory address Mem9 within this memory page, if the positive conditional flag is set.
BRnp Mem9
Jump to the 9-bit memory address Mem9 within this memory page, if the either the negative or positive conditional flags are set.
BRnz Mem9
Jump to the 9-bit memory address Mem9 within this memory page, if either the negative or zero conditional flags are set.
BRzp Mem9
Jump to the 9-bit memory address Mem9 within this memory page, if either the zero or positive conditional flags are set.
BRnzp Mem9
Jump to the 9-bit memory address Mem9 within this memory page (i.e. with the upper 7 bits determined by the location of this instruction within memory), if the negative, zero, or positive conditional flags is set. Effectively, this always jumps since one of the flags is guaranteed to be set from the last calculation.
JMP Mem9
Jump to the 9-bit memory address Mem6 within this memory page. Effectively the same as BRnzp.
JMPR Reg_base, Offset6
Add the value in Reg_base to the 6-bit value Offset6, and jump to that location in memory.
JSR Mem9
Save the current current instruction address in R7, then jump to the 9-bit memory address Mem6 within this memory page.
JSRR Reg_base, Offset6
Save the current current instruction address in R7, then add the value in Reg_base to the 6-bit value Offset6, and jump to that location in memory.
LD Reg_dest, Offset9
Retrieve the value at the 9-bit memory address Offset9 within this memory page and put it into Reg_dest.
LDI Reg_dest, Offset9
Retrieve the value at the 9-bit memory address Offset9 within this memory page, then use this as a memory address. Retrieve the value at that memory address and put it into Reg_dest.
LDR Reg_dest, Reg_base, Offset6
Add the value in Reg_base to the 6-bit value Offset6, retrieve the value in that memory location, and store it in Reg_dest.
LEA Reg_dest, Offset9
Concatenate the bits of the current page (the top 7 bits in the memory address of the current instruction) with the 9-bit value Offset6 and store the result in Reg_dest.
NOT Reg_dest, Reg_src
Take the logical NOT of the value in Reg_src and store it in Reg_dest.
RET
Set the program counter (PC) to the value stored in R7.
RTI
Set the PC to the value at the memory location pointed to by R7.
ST Reg_src, Offset9
Set the value at the 9-bit memory address Offset9 within this memory page to the value in Reg_src.
STI Reg_src, Offset9
Retrieve the value at the 9-bit memory address Offset9 within this memory page, then use this as a memory address. Set the value at that memory address to the value in Reg_src.
STR Reg_src, Reg_base, Offset6
Add the value in Reg_base to the 6-bit value Offset6, set the value in that memory location to the value in Reg_src.
TRAP vector8
Activate the operating system with trap vector8. This saves the value of the PC into R7 and sets PC to the value stored at the zero-extended memory location vector8. For example, if the memory address that stores the GETC subroutine is 0x0400, then memory address 0x20 should store the value 0x400. Then, TRAP 0x20 will set the PC to 0x400.

^ Back to Top ^