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 LC assembly language program that can add numbers with up to digits. The code I have works in online LC simulators, but when I load the obj file in the LCsim on my computer it doesnt run. It creates TRAP # and 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 x
numX FILL #
numY FILL #
numtemp FILL #
RunProgram
JSR ClearRegisters
JSR ClearOutputArray
AND R R #
ST R numR
LEA R prompt ; loads prompt into R
PUTS ; prints to console
JSR ReadInput ;runs subroutine to read the input from user
LD R numtemp
ST R numX
; reset temp variable
AND R R #
ST R numtemp
; Get second number
LEA R prompt
PUTS
JSR ReadInput
LD R numtemp
ST R numY
AND R R #
ST R numtemp
; prompt for operator
LEA R prompt
PUTS
; get operator
GETC ; Character is stored in R
OUT
AND R R #
LD R numX
LD R numY
LEA R codesArray ; load codesArray address
LDR R R # ; loads first symbol from R into R
ADD R R R ; add contents of Ruser input and R
BRz Addition
LDR R R #
ADD R R R
BRz DivisionF
OpComplete
LEA R result
PUTS
ADD R R #
Brn printNeg
ST R numR
JSR DetermineSize
JSR NumToASCII
LEA R outputArray
PUTS
LEA R endStrg
PUTS
GETC
OUT
LD R minus
ADD R R R
Brz RunProgram
; End of program
HALT
numR FILL #
promptSTRINGZ
Input first number:
promptSTRINGZ
Input second number:
promptSTRINGZ
Input operation :
resultSTRINGZ
Result
endStrg STRINGZ
Restart? for YES, for NO:
codesArray FILL # ;
FILL # ;
; Subroutines Start ;
; R X
; R Y
; R result of X operation Y
; Use R R & R for extra stuff
; ;
ReadInput
ST R saveR
LEA R inputArray ; R pointer to inputArray
AND R R # ; init R to
AND R R # ; init R to
LoopReadInput GETC
ADD R R # ; check for return key
BRz DoneReadInput
OUT ; print character to screen
STR R R # ; store char into inputArray
ADD R R # ; inc size value
ST R inputSize ; store R into inputSize variable
ADD R R # ; inc array index
ADD R R # ; stop when char long
BRn LoopReadInput ; loop back for next character
DoneReadInput
LEA R inputArray ; R pointer to inputArray
LEA R constArray ; R pointer to constArray
LD R minus ; R contains hex
LD R inputSize ; R contains length of input
ADD R R # ; length
LoopReadInput
LDR R R # ; load ASCII digit from inputArray
ADD R R R ; R contains integer
ADD R R R ; R contins address of constant
LDR R R # ; R load the number from the arrary
JSR ReadInputExpand ; R Integer, R Constant
LD R numR ; Load mult result into R
LD R numtemp ; Load the current num into R
ADD R R R ; Add together
ST R numtemp ; Store the sum back into the temp num
ADD R R # ; increment pointer in inputArray
ADD R R # ; decrement size
BRzp LoopReadInput ; loop back and continue to multiply out
LD R saveR
RET
minusFILL x
inputArray BLKW #
ReadInputExpand
ST R saveR
ST R saveR
AND R R #
AND R R #
ADD R R R ; duplicate R so it remains unchanged
Mult
ADD R R R
ADD R R #
BRp Mult
ST R numR
LD R saveR
LD R saveR
RET
NumToASCII ; prints number in R
ST R saveR
LD R outputSize ; load size into R
LEA R outputArray ; load address of output into R
ConvertLoop
LEA R constArray ; load address of constArray R
ADD R R # ; decrement size of num
ADD R R R ; R R R
LDR R R # ; load const from constArray at index R
LD R numR ; load the number into R
ST R saveR
ST R saveR
ST R save
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
