Search code examples
assemblylc3

LC-3 Trying to add 2 numbers but the results are way off


I have a program where I am adding 2 numbers at one point. I have received 2 numbers from the user, used ASCII -30 to convert them to numeral, added them together in register 0 and output the total. I have made sure for now to keep the numbers under 5 so I do not go over 9 total. But the output I get for the total is... not sure the name. Its like a thick period but in the center of the character instead of the bottom... like a bullet point. My code is below, looking at the Simulator, when I go through the program and enter 4 for both numbers... just before I output the total (or what I want to be the total) the registers are

R0 x0008 8
R1 x0004 4
R2 x0004 4
R3 x0008 8

ADTN    LD R4 CONVERT   ; Load -30 FFD0 into register 4
        GETC            ; Get charicter from user
        ADD R1, R0, R4  ; store the number in R1 after adding the neg ascii 30
        GETC            ; Get 2nd charicter
        ADD R2, R0, R4  ; Store number in R2 after adding neg ascii 30
        LEA R0, ADDMSG  ; Load into R0 message "The sum of the 2 numbers is "
        PUTS            ; Display message
        ADD R0, R1, R2  ; Add R1 and R2 and store in R0
        TRAP x21        ; Output total

Everything looks good, but for some reason it puts out the bullet point. I changed TRAP x21 to PUTS and that did not work either


Solution

  • As far as I understand, TRAP x21 outputs a character with the ASCII code given in R0. Since you have a number with the value 0..9 in R0 you need to add '0' (0x30) to it to convert it to a character in the range '0'..'9'.