Question: I have been tasked with writing an LC 3 assembly language program that can add 2 numbers with up to 4 digits. The code I

I have been tasked with writing an LC3 assembly language program that can add 2 numbers with up to 4 digits. The code I have works in online LC3 simulators, but when I load the obj file in the LC3sim on my computer it doesnt run. It creates TRAP #34 and 37 instructions that crash the program. I dont know why this happens and how to fix it. Ill provide the code but Im unsure if its an issue with the code or my simulator.
heres my code:
.ORIG x3000
num_X .FILL #0
num_Y .FILL #0
num_temp .FILL #0
RunProgram
JSR ClearRegisters
JSR ClearOutputArray
AND R0, R0, #0
ST R0, num_R
LEA R0, prompt1 ; loads prompt1 into R0
PUTS ; prints to console
JSR ReadInput ;runs subroutine to read the input from user
LD R1, num_temp
ST R1, num_X
; reset temp variable
AND R0, R0, #0
ST R0, num_temp
; Get second number
LEA R0, prompt2
PUTS
JSR ReadInput
LD R2, num_temp
ST R2, num_Y
AND R0, R0, #0
ST R0, num_temp
; prompt for operator
LEA R0, prompt3
PUTS
; get operator
GETC ; Character is stored in R0
OUT
AND R6, R6, #0
LD R1, num_X
LD R2, num_Y
LEA R5, codesArray ; load codesArray address
LDR R3, R5, #0 ; loads first symbol from R7 into R3
ADD R4, R0, R3 ; add contents of R0(user input) and R3
BRz Addition
LDR R3, R5, #3
ADD R4, R0, R3
BRz DivisionF
OpComplete
LEA R0, result1
PUTS
ADD R6, R6, #0
Brn printNeg
ST R6, num_R
JSR DetermineSize
JSR NumToASCII
LEA R0, outputArray
PUTS
LEA R0, endStrg
PUTS
GETC
OUT
LD R5, minus30
ADD R0, R0, R5
Brz RunProgram
; End of program
HALT
num_R .FILL #0
prompt1.STRINGZ "
Input first number: "
prompt2.STRINGZ "
Input second number: "
prompt3.STRINGZ "
Input operation (+,/): "
result1.STRINGZ "
Result ="
endStrg .STRINGZ "
Restart? 0 for YES, 1 for NO: "
codesArray .FILL #-43 ; '+'
.FILL #-47 ; '/'
;--------- Subroutines Start ---------;
; R1<- X
; R2<- Y
; R6<- result of X (operation) Y
; Use R3, R4, & R5 for extra stuff
; ------------------------------ ;
ReadInput
ST R7, saveR7
LEA R3, inputArray ; R3<- pointer to inputArray
AND R4, R4, #0 ; init R4 to 0
AND R5, R5, #0 ; init R5 to 0
LoopReadInput GETC
ADD R6, R0, #-10 ; check for return key
BRz DoneReadInput
OUT ; print character to screen
STR R0, R3, #0 ; store char into inputArray
ADD R4, R4, #1 ; inc++ size value
ST R4, inputSize ; store R4 into inputSize variable
ADD R3, R3, #1 ; inc++ array index
ADD R5, R4, #-4 ; stop when 4 char long
BRn LoopReadInput ; loop back for next character
DoneReadInput
LEA R3, inputArray ; R3<- pointer to inputArray
LEA R4, constArray ; R4<- pointer to constArray
LD R5, minus30 ; R5<- contains hex 30
LD R6, inputSize ; R6<- contains length of input
ADD R6, R6, #-1 ; length -1
LoopReadInput2
LDR R1, R3, #0 ; load ASCII digit from inputArray
ADD R1, R1, R5 ; R1<- contains integer
ADD R2, R4, R6 ; R2<- contins address of constant
LDR R2, R2, #0 ; R2<- load the number from the arrary
JSR ReadInputExpand ; R1<- Integer, R2<- Constant
LD R1, num_R ; Load mult result into R1
LD R0, num_temp ; Load the current num into R0
ADD R0, R0, R1 ; Add together
ST R0, num_temp ; Store the sum back into the temp num
ADD R3, R3, #1 ; increment pointer in inputArray
ADD R6, R6, #-1 ; decrement size
BRzp LoopReadInput2 ; loop back and continue to multiply out
LD R7, saveR7
RET
minus30.FILL x-30
inputArray .BLKW #5
ReadInputExpand
ST R6, saveR6
ST R4, saveR4
AND R6, R6, #0
AND R4, R4, #0
ADD R4, R4, R2 ; duplicate R2 so it remains unchanged
Mult
ADD R6, R6, R1
ADD R4, R4, #-1
BRp Mult
ST R6, num_R
LD R4, saveR4
LD R6, saveR6
RET
NumToASCII ; prints number in R1
ST R7, saveR7
LD R3, outputSize ; load size into R3
LEA R6, outputArray ; load address of output into R6
Convert_Loop
LEA R5, constArray ; load address of constArray -> R5
ADD R3, R3, #-1 ; decrement size of num
ADD R5, R5, R3 ; R5<- R5+ R3
LDR R2, R5, #0 ; load const from constArray at index R5
LD R1, num_R ; load the number into R1
ST R3, saveR3
ST R4, saveR4
ST R5, save

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!