Need Help creating LC-3 Program

I am currently working on a Program in lc-3 where I take the user input for two numbers 1-9 and multiply the two together and print the result. Within the program I also need to check the 2 numbers and make sure they are actually numbers between 1-9.

What I have so far can take the input of two numbers but doesn’t verify if their numbers and will not print out a result over 9 correctly. Any help?

.ORIG x3000

AND R3, R3, #0          ;r3 stores the sum, set r3 to zero
AND R4, R4, #0          ;r4 is the counter

;-----------------------------------------------------------------------------------------------------------------------------
;storing first input digits
LEA R0, Num1        ;load the address of the 'Num1' message string
PUTS                ;Prints the message string

GETC                ;get the first number
OUT             ;print the first number
ADD R1, R0, #0          ;store input value(ascii) to r1
ADD R1, R1, R5          ;get real value of r1


;storing second input digits
LEA R0, Num2        ;load the address of the 'Num2' message string
PUTS                ;Prints the message string

GETC                ;get the first number
OUT             ;print the first number
ADD R2, R0, #0          ;store input value(ascii) to r2
ADD R2, R2, R5          ;get real value of r2
;---------------------------------------------------------------------------------------------------------------------------

ADD R4, R2, #0          ;fill counter with multiplier


MULTIPLICATION:
ADD R3, R3, R1          ;add to sum
ADD R4, R4, #-1         ;decrease counter by one
BRp MULTIPLICATION      ;continue loop until multiplier is 0

LEA R0, stringResult
PUTS

ADD R0, R3, R6          ;move result to r0
OUT             ;print result

HALT
;-----------------------------------------------------------------------------------------------------------------------------

Num1 .STRINGZ "\nenter the 1st no.: "
Num2 .STRINGZ "\nenter the 2nd no.: "
stringResult .STRINGZ "\nResult: "


.END

  • ADD R1, R1, R5 ;get real value of r1 This code is using the R5 register without initializing it first. I think you meant to initialize this to the negative version of ASCII zero.

    – 

  • R6 is also used but not initialized. Try using single step in the debugger to watch each instruction do its thing and verify that it’s doing something correct and useful. You could have found these two uninitialized registers yourself using a variety of methods, single step is a really good discipline to force you to read your own code. It can also be helpful to have C or pseudo code for what you’re trying to do, so you have something to compare with when you’re single step debugging.

    – 




  • Btw, this pair of instructions: ADD R1, R0, #0 ; ADD R1, R1, R5 first does copy to another register, then adjust that register. The pair can be replaced by ADD R1, R0, R5 which does copy & adjust in one instruction.

    – 

  • Your multiplication code looks ok except it won’t work when the second input is 0. Write out some simple C code that checks for digits being out of bounds, and for printing out the answer digits, as there may be 1 or 2 of them and so that will require some logic. Once you have some pseudo or C code for these logics, it will be easier to write the assembly code.

    – 




  • Stack overflow questions should have titles that help future readers figure out if they might have the same problem. This one doesn’t do that at all; edit it to say something about what your program does, or about the specific problem you’re running into as part of implementing it.

    – 

Leave a Comment