Instructions

NOT & Subtraction

Bitwise NOT and how to subtract without a SUB instruction.

NOT Instruction

NOT DR, SR — Flips every bit of SR and stores the result in DR.

This is the LC-3's only unary operation.

NOT R1, R0    ; R1 = bitwise complement of R0
              ; If R0 = x000F (0000000000001111)
              ; then R1 = xFFF0 (1111111111110000)

Subtraction via Two's Complement

The LC-3 has no SUB instruction. But we can subtract using:

A - B = A + (-B) = A + (NOT B) + 1

Steps to compute R2 = R0 - R1:

NOT R1, R1      ; R1 = ~R1 (one's complement)
ADD R1, R1, #1  ; R1 = ~R1 + 1 = -R1 (two's complement)
ADD R2, R0, R1  ; R2 = R0 + (-R1) = R0 - R1

This destroys the original value of R1. If you need R1 later, copy it to a temporary register first.

Logical OR

The LC-3 also has no OR instruction. We use De Morgan's Law:

A OR B = NOT(NOT A AND NOT B)
; R2 = R0 OR R1
NOT R0, R0      ; R0 = ~R0
NOT R1, R1      ; R1 = ~R1
AND R2, R0, R1  ; R2 = ~R0 & ~R1
NOT R2, R2      ; R2 = ~(~R0 & ~R1) = R0 | R1
Exercise

Write a program that computes R2 = R0 - R1 where R0 = 20 and R1 = 7. Print the result as a character (it should print the ASCII character for 13... but since 13 is a control character, try R0 = 7, R1 = 2 so the difference is 5. Add #48 to convert to ASCII "5" and print it).

1
R0
x00000
R1
x00000
R2
x00000
R3
x00000
R4
x00000
R5
x00000
R6
x00000
R7
x00000
PCx3000
CC
NZP