I need help coding this lc3 assembly program. Basically I need to make a calcula
ID: 3815947 • Letter: I
Question
I need help coding this lc3 assembly program. Basically I need to make a calculator using LC3 assembly language and perform + - * and / operations.
**For the division operation I need to output both the quotient and remainder.
I have a skeleton:
.ORIG x3000
and r1, r1, #0
lea r0, Prompt
puts
getc
out
st r0, op
ld r1, PLUS
add r1, r0, r1
brz addition
;;
ld r1, MINUS
add r1, r0, r1
brz subtraction
;;
ld r1, MULT
add r1, r0, r1
brz multiplication
;;
ld r1, DIV
add r1, r0, r1
brz division
;
; If we are here, we got an invalid operation.
; Display error & exit
;
lea r0, ErrMsg
puts
br done
;;
addition
;;setup
;;Do the addition. You can call a subroutine to add
br output
subtraction
;; setup
;; Do the subtraction. You can call a subroutine to subtract
br output
multiplication
;; Setup
;; jsr Multiply
br output
division
;; Setup
;; jsr Divide
br output
;;
output
;; Display result
br done
done halt
PLUS .FILL #-43
MINUS .FILL #-45
MULT .FILL #-42
DIV .FILL #-47
Prompt .STRINGZ "Operation (+, -, *, /)? "
ErrMsg .STRINGZ " Invalid Operation. "
op .FILL #0
.end
Any help will be appreciated!!
Thanks
Explanation / Answer
initializes all registers to zero
AND R0, R0, #0
AND R1, R1, #0
AND R2, R2, #0
AND R3, R3, #0
AND R4, R4, #0
AND R5, R5, #0
AND R6, R6, #0
AND R7, R7, #0
NEXTCHARACTER GETC
OUT
;Space?
LD R1, OPPOSPACE
ADD R1, R0, R1
BRz NEXTCHARACTER
;New line?
LD R1, OPPOLINE
ADD R1, R0, R1
BRz DONE
;Multiplication?
LD R1, OPPOMULTI
ADD R1, R0, R1
BRz DECODE
Brn INVALID_CHARACTER
LD R1, OPPODECI
ADD R1, R1, R0
BRz INVALID_CHARACTER
;Operand or operator?
LD R1, OPPOZERO
ADD R1, R1, R0
BRzp PUSH
BRn DECODE
ADDITION ADD R2, R5, #0
LDR R5, R6, #0
ADD R6, R6, #1
ADD R3, R5, #0
ADD R2, R2, R3
ADD R6, R6, #-1
STR R2, R6, #0
BRnzp NEXTCHARACTER
SUBTRACTION ADD R3, R5, #0
LDR R5, R6, #0
ADD R6, R6, #1
ADD R2, R5, #0
NOT R3, R3
ADD R3, R3, #1
ADD R2, R2, R3
ADD R6, R6, #-1
STR R2, R6, #0
BRnzp NEXTCHARACTER
MULTIPLICATION ADD R2, R5, #0
LDR R5, R6, #0
ADD R6, R6, #1
ADD R3, R5, #0
MULTIPLICATIONLOOP ADD R2, R3, R2
ADD R3, R3, #-1
BRp MULTIPLICATIONLOOP
ADD R6, R6, #-1
STR R2, R6, #0
BRnzp NEXTCHARACTER
DIVISION ADD R3, R5, #0
LDR R5, R6, #0
ADD R6, R6, #1
ADD R2, R5, #0
NOT R1, R3
ADD R1, R1, #1
DIVLOOP ADD R4, R4, #1
AND R2, R2, R0
BRn NEGATIVE
BRp DIVLOOP
BRz ZERO
NEGATIVE ADD R4, R4, #-1
ADD R5, R2, R3
ADD R6, R6, #-1
STR R4, R6, #0
ZERO BRnzp NEXTCHARACTER
EXPONENTIAL ADD R2, R5, #0
LDR R5, R5, #0
ADD R6, R6, #1
ADD R3, R5, #0
ADD R1, R1, R3
OUTEREXPOLOOP ADD R4, R1, #0
INNEREXPOLOOP ADD R3, R3, R3
BRp INNEREXPOLOOP
ADD R2, R2, #-1
BRp OUTEREXPOLOOP
ADD R6, R6, #-1
STR R3, R6, #0
BRnzp NEXTCHARACTER
DONE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.