Posted on **06-19-2006**, by **Tim**

LC-3 does not natively support subtraction... we are going to have to figure out how to do this on our own.

What is subtraction? It is simply adding the negative value of a number.

A - B is the same as A + (-B). Using the NOT instruction, we are able to achieve this in LC-3. Let these be our labels:

**Code:** |

FIRST_NUMBER .fill #5
SECOND_NUMBER .fill #3 |

Now here is the magic of subtraction (5 - 3):

**Code:** |

LD R1, FIRST_NUMBER
LD R2, SECOND_NUMBER
; create the negative of R2
NOT R2, R2
ADD R2, R2, #1
; subtract - R3 now holds #2
ADD R3, R1, R2 |

In the operation of (5 - 3), we need to find the negative of 3. Granted we could always create the label that holds #-3 and then added, there will be many situations in which one of our operands will not be an immediate value.

Why do we get the negative value of SECOND_NUMBER and then add 1? This is because LC-3, like all other microprocessors, uses the two's complement representation system.

Now let's view the whole program:

**Code:** |

.ORIG x3000
; subtract #5 - #3
LD R1, FIRST_NUMBER
LD R2, SECOND_NUMBER
; create the negative of R2
NOT R2, R2
ADD R2, R2, #1
; subtract - R3 now holds #2
ADD R3, R1, R2
HALT
FIRST_NUMBER .fill #5
SECOND_NUMBER .fill #3
.END |

Difficulty:

**Intermediate** - Views:

**16918**