Skip to content

Loading Data into Registers

An LC-3 assembly programs can only perform operations on data in the General Purpose registers

LC3Tools Simulator Registers These 8 registers, names R0 through R7, are the program's working area. If a program needs to perform any arithmetic or logical function on data, the program must first load the data into a register

Immediate Values

An Immediate Value is essentially a hard-coded, constant value. The value is copied into the designated register

asm
.ORIG x3000
  AND R0, R0, #0; Clear out whatever is currently in R0
  ADD R0, R0, #5; Set R0 to 5
Done HALT

;End of Program

;Data Declarations-------------
.END

The first line sets R0 to 0. The program does not assume a register is zero, so makes it zero

The second line can safely add the existing value (0) with the immediate value (5) and store it back in R0

This overwrites R0. It's previous value is lost

Immediate Values have a Limited Range

Immediate values are limited to 5 bits. In the LC-3's 2's complement system, this means the the programmer is limited to -16 to +15

For smaller or larger values, we will need to allocate memory

Load from/ Store to Memory

Memory locations in LC-3 contain 16 bits of data. In the 2's complement system, this means a single memory location can contain a value between -32,768 to 32,767

Allocate and Define Memory

Memory should be allocated below the ;Data Declarations------------- section of the Assembly Template. This will help make the code easier to read and debug

asm
.ORIG x3000
  LD R0, MyData1
  LD R1, MyData2
  LD R2, MyData3
Done HALT

;End of Program

;Data Declarations-------------
   MyData1 .FILL #10
   MyData2 .FILL x00FF
   MyData3 .FILL b1011010000101111
   Result  .BLKW 1
.END

MyData1 allocates 1 memory location and fills it with a 1010

MyData2 allocates 1 memory location and fills it with a FF16

MyData3 allocates 1 memory location and fills it with a 10110100001011112

Result allocates 1 memory location, but does not fill it. It is set to zero by the assembler

Memory can be filled using any of these 3 number bases. Memory stores all values as binary, however the decimal (#) and hex (x) options are provided to make code easier to understand my humans

Loading Allocated Memory into Registers

The **LD instruction copies data from a named memory location into a register

asm
.ORIG x3000
  LD R0, MyData1
  LD R1, MyData2
  LD R2, MyData3
Done HALT

;End of Program

;Data Declarations-------------
   MyData1 .FILL #10
   MyData2 .FILL x00FF
   MyData3 .FILL b1011010000101111
   Result  .BLKW 1
.END

R0 is loaded with the value in MyData1 (#10)

R1 is loaded with the value in MyData2 (xFF)

R2 is loaded with the value in MyData3 (b1011010000101111)

With data in these registers, the LC-3 can perform operations on this data

Storing Registers into Allocated Memory

The ST instruction copied the value from a register into a named memory location

asm
.ORIG x3000
  LD R0, MyData1
  LD R1, MyData2

  ADD R3, R0, R1

  ST R3, Result
Done HALT

;End of Program

;Data Declarations-------------
   MyData1 .FILL #10
   MyData2 .FILL x00FF
   Result  .BLKW 1
.END

After loading R0 and R1 with data from 2 memory location, the values are added and the result is stored in R3

The value in R3 is then copied to the named memory location Result

Condition Code Register

All Load instructions set the Condition Code (CC) register at the end of executing the instruction

This register can be used by future instructions that might decide to change the program's flow (like an If/Else -or- Loop behavior)

Condition Code Register

The CC will be in one of three possible states:

N - The last operation resulted in a Negative Value

Z - The last operation resulted in a Zero Value

P - The last operation resulted in a Positive Value

Other instructions do not change the CC, so it will holding last value until another of the above 4 instructions is executed

The contents of this E-Text were developed under an Open Textbooks Pilot grant from the Fund for the Improvement of Postsecondary Education (FIPSE), U.S. Department of Education. However, those contents do not necessarily represent the policy of the Department of Education, and you should not assume endorsement by the Federal Government.
Released under Creative Commons BY NC 4.0 International License