Problem 3 8 points The LC3 does not have subtract nor absolu
Problem 3 (8 points) The LC-3 does not have subtract nor absoluteValue instructions. But it has three operate instructions: ADD, AND, NOT which can be used to perform these operations. An absoluteValue operation outputs the absolute value of a number. For example: The absolute value of -5 is 5, and the absolute value of 3 is 3.
Write a LC-3 program that subtracts the absolute value of an integer at memory location 0x2000 from the absolute value of an integer at memory location 0x3000. Place the result is at the memory location 0x2000. You do not need to worry about the absolute value of -2^15 because +2^15 cannot be represented in LC-3.
Solution
LOAD R1,x2000
ADD R2 , R1 , #0 ; R2 R1
BRzp END ; If value is 0 or positive do not negate
NOT R2 , R2
ADD R2 , R2 , #1 ; R2 = R1
At this point R2=|R1|
Similarly the absolute value of second number is obtained by the code
LOAD R3,x3000
ADD R4 , R3 , #0 ; R4 R3
BRzp END ; If value is 0 or positive do not negate
NOT R4 , R4
ADD R4 , R4 , #1 ; R4 = R3
At this point R4=|R3|
Now we shall subtract R2 and R4
NOT R6,R2
ADD R6,R6, #1; R6<--- -R2
ADD R5, R2,R4; R5<--- R2-R4
Since subtraction is not done directly it is done with 2\'s compliment Negation
