Assembly

The LC-3 assembly language

Instructions and operands #

Arithmetic & logic #

  • ADD adds two numbers
    • ADD R1 R1 #0 sets the condition codes for R1
    • ADD R3 R2 #0 copy R2 to R3
  • AND bitwise AND
    • AND R1 R1 #0 clears R1

Memory #

  • LEA calculates memory offset, does not actually load anything, useful to create a pointer
  • LD load value from memory address specified by offset
  • LDR load value from memory adddress specified by register + offest
  • LDI load value from memory address stored in memory location specified by offest
  • ST store value of register in memory location specified by offset
  • STR store value of register in memory location specified by register + offset
  • STI 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 flags
  • JMP unconditionally jumps to memory location specified in register
    • RET when JMP is used with R7, used to return from subroutines
  • JSR(R) save PC and jump to location, the JSRR variant uses a register

Trap #

  • HALT stops the program
  • GETC read a character from keyboard
  • IN read a character from keyboard with prompt
  • OUT write a character to the display
  • PUTS write a string to the display
  • PUTSP 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