Basic LC-3 Instructions
Posted on 06-14-2006, by Tim
There are 15 instructions in LC-3 assembly language. They are separated into three different categories: operation, data movement, and finally, control.

Please note: more advanced instructions such as TRAP, will be covered in a later tutorial.


Operation Instructions

ADD - Destination , Source , Source

ADD takes in two numbers and stores their sum in the Destination register. The first source must be a register, while the second source may be either a register or an immediate value.

Examples:
Code:
ADD R1, R1, R2

Code:
ADD R1, R1, #1




AND - Destination , Source , Source

AND performs a bitwise AND operation on the two sources (both registers) and stores the value in the Destination register. The second source may also be an immediate value.

Examples:
Code:
AND R1, R2, R3

Code:
AND R1, R1, #0




NOT - Destination , Source

The NOT instruction takes a value (source register) and inverts it in the destination register. For example, if it held the value of "011001," then NOTing the value would give you "100110."

Example:
Code:
NOT R1, R1




Data Movement Instructions

LD - Destination , Source

The LD instruction takes a source label and stores its address into the destination register. The source label must be within 255 lines of the LD instruction.

Example:
Code:
LD R1, LABEL




LDR - Destination , Source , Offset

LDR stores the source register's value plus an immediate value offset and stores it in the destination register.

Example:
Code:
LDR R1, R2, #6




LDI - Destination , Source

LDI treats the source register as an address and stores the contents of memory at that address in the destination register.

Examples:
Code:
LDI R3, R1

Code:
LDI R3, x4000




ST - Destination , Source

This function stores a value (source label) into a destination register.

Example:
Code:
ST R1, ADDR




STR - Destination , Source , Offset

STR stores the value in a source register into a destination register. An immediate offset may also be used. If you do not want to use an offset, simply pass a #0 as the offset.

Example:
Code:
STR R0, R1, #0

Code:
STR R3, R6, #3




STI - Destination , Source

STI treats its immediate value source, or a label as a memory address and stores the value of that address into the destination register.

Example:
Code:
STI R4, x3005




Control Instructions

BR - n z p Label

Think of the BR instruction as an if statement in higher-level languages. However, the BR command is more picky about how you use it. BR will only jump to a new memory location depending on whether or not the previous instruction's result was negative, zero, or positive. You may tell BR to jump on either zero or positive, or even all three. To tell BR to jump on a specific condition, append it to the command. For example, jumping on only negative numbers would be "BPn" whereas jumping on negative, zero or positive would be "BPnzp." It is interesting to note that these condition flags MUST be placed in this order.

Example:
Code:
LOOP:
  ADD R1, R1, #-1
BRp LOOP


If R1 contains a positive value when it continues onto the BRp line, then BR will jump back to the beginning of the LOOP label. If not, it will go to the next line.



JMP - Source

JMP will jump to a source register's location.

Example:
Code:
JMP R5




JSR - Label

JSR is used to simulate subroutines and functions where the label is an immediate value that is the beginning of the function.

Example:
Code:
JSR FUNCTION_ADD




JSRR - Destination

JSRR is nearly identical to JSR except that uses a register instead of a label.

Example:
Code:
JSRR R2
Difficulty: Beginner - Views: 52642

User Comments
chaoscreater wrote:
I think some of these are wrong? E.g.

:
ST - Destination , Source

This function stores a value (source label) into a destination register.

Example:

Code:
ST R1, ADDR



isn't ST suppose to store the contents in the register R1, into the memory address/label ADDR?? I checked all my LC3 notes and it's the opposite to what it says on this page.........some of these are opposites to my notes, could you please check them?


Copyright 2006 © LC3Help.com v1.4.7