So I'm learning Assembly for the LC-3 Machine for the first time, so I'm still quite a newbie at this. I'm trying to get my program to read in 2 characters, compare them, and then output the smaller character. So far what I've done is store the first character in register 1, and the second in register 2. Then, I wanted to subtract the ASCII values, and then if the result was < 0, the first would be smaller, and if it was > 0 the second would be smaller, and if it = 0, then they would be the same character.
My problem is that I can't figure out how to subtract the 2 characters in the registers. I've tried searching online, looking in my notes, and I can't find anything to answer my question. Here's my code so far:
.orig x3000
LEA R0, msg ;loads message into R0
PUTS ;puts message to screen
GETC ;reads a char
OUT ;puts char to screen
ADD R1, R1, R0 ;R1 <- R0
LEA R0, msg ;loads message into R0 again
PUTS ;puts message to screen
GETC ;reads another char
OUT ;puts char to screen
ADD R2, R2, R0 ;R2 <- R0
ADD R1, R1, -R2 ;subtract second character from first character
;This line here is my problem!!!!
BRP elseif ;if it's positive, the second character is larger
BRZ else ;if it's zero, they are the same
if
LEA R0, msg2 ;loads second message
PUTS ;puts second message on screen
AND R0, R0, #0 ;clears R0
ADD R0, R0, R1 ;R0 <- R1
OUT ;print the first char
br endif
elseif
LEA R0, msg2 ;loads second message
PUTS ;puts second message on screen
AND R0, R0, #0 ;clears R0
ADD R0, R0, R2 ;R0 <- R2
OUT ;print the second char
br endif
else
LEA R0, msg3 ;loads third message
br endif
endif
HALT
msg .STRINGZ "\nEnter any character: "
msg2 .STRINGZ "\nThe smallest character is: "
msg3 .STRINGZ "\nThe characters are the same."
.END
Please, remember that this is like, my first Assembly program ever, so it's probably super horrible. ^_^" But since I'm still learning, I want to keep it simple and straight-forward, even if it's a dumb way to implement it.
So the line right before my break statements is where I'm having trouble. I tried adding register 1 and the negative of register 2 as you see above, but that didn't work, so basically my question is, is there a way I can subtract/compare two char values stored in 2 registers??
Any help/advice would be greatly appreciated!! :)
There is no direct operator to subtract using LC3. You need to use 2's complement.
R1 = R2-R3; //This is not possible.
// using 2's Complement method
R1 <- NOT R3;
R1 <- R1+1;
R1 <- R2+R1;
Register R1 will have the subtraction result. You can use your comparison code to print out the smaller number