Skip to content

Arithmetic and Logic Instructions

Video: Arithmetic and Logic Instructions — Text Alternative

This video demonstrates the three LC-3 ALU instructions using LC3Tools. Key operations shown on screen:

  • NOT: bitwise complement of a register — every bit is flipped (0→1, 1→0); result stored in a destination register
  • AND: bitwise AND of two registers, or a register and a 5-bit immediate value; result stored in a destination register
  • ADD: sum of two registers, or a register and a 5-bit immediate value; result stored in a destination register
  • The LC3Tools Simulator showing register values (R0–R7) updating after each ALU instruction executes
  • The Condition Code (CC) register (N/Z/P) updating based on whether the result is negative, zero, or positive

The LC-3 provides three (3) Arithmetic and Logic Unit (ALU) Instructions

All ALU instructions are preformed on data in Registers and the results are stored in Registers

NOT

Performs a logical NOT on data in a register

lc-3
.ORIG x3000
  AND R0, R0, #0; Clear out whatever is currently in R0
  ADD R0, R0, #5; Set R0 to 5
  NOT R1, R0; Invert the value in R0 and store in R1
Done HALT

;End of Program

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

AND

Performs a logical AND on data in two Register, storing the result in a Register

lc-3
.ORIG x3000
  AND R0, R0, #0; Clear out whatever is currently in R0
  ADD R0, R0, #5; Set R0 to 5
  NOT R1, R0; Invert the value in R0 and store in R1
Done HALT

;End of Program

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

ADD

Performs a logical ADD on data in two Register, storing the result in a Register

lc-3
.ORIG x3000
  AND R0, R0, #0; Clear out whatever is currently in R0
  ADD R0, R0, #5; Set R0 to 5
  NOT R1, R0; Invert the value in R0 and store in R1
Done HALT

;End of Program

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

Condition Code Register

All ALU 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