Essay record

LC-3 Assembly: Negate a Number

Record
Essay / / 1 min read / 148 words
Tags
Unfiled

DISCLOSURE: Some links may be affiliate links. Details

**Problem: **Given a number in assembly, change its sign

Essentially, what we have to do is take a number and return the same number with the opposite sign (positive becomes negative and vice versa).

This is what my code looks like:

First off, we set the registers to 0 then load R1 with the given number.  Next we figure out if the given number is positive or negative.  If it’s positive, we move to the POSLOOP in which we decrement R2.  If not, R2 is incremented.

We then store the sum of the given number and R2 in R3.  If that sum is equal to zero, then we know we have negated the number and we store that number in the address location of ANSWER.  If it’s not zero, then we continue to iterate through the given loop until it is.

Built with CloudSeed Rust