Reputation: 81
I'm very new to the assembly language and in class we're working on a division program using the LC3 simulator. Below is my code for my division algorithm.
DIVISION:
AND R3, R3, 0 ; Zero out R3 /This is the remainder
AND R4, R4, 0 ; Zero out R4 /This is the quotient
NOT R3, R2 ; Takes the inverse of 2nd input ->R3
ADD R3, R3 #1 ; Add one to the inverse (for 2s comp)
LOOPD
ADD R4, R4, #1 ; Add 1 to R4 repeatedly
ADD R1, R1, R3 ; Subtract input2 from R1
BRN NEGATIVE
BRZ ZERO
BRP LOOPD
NEGATIVE
ADD R4, R4, #-1
ADD R3, R1, R2
; Done with divison algorithm.
ZERO
LD R0, DECCONV ; Load Decimal converter
ADD R3, R3, R0 ; Convert back to ASCII
ADD R4, R4, R0 ; Convert back to ASCII
ST R3, REMRESULT ; Store the remainder result
ST R4, DIVRESULT ; Store the division result.
LD R0, DIVRESULT ; Load Division result into R0
PUTC ; Print it.
LEA R0, DIVSTRING ; Load the string for division.
PUTS ; Print the string.
LD R0, REMRESULT ; Load Remainder result into R0
PUTC ; Print it.
LEA R0, REMSTRING ; Load the string for remainder
PUTS ; Print the string.
When I enter two inputs, for example: 4 and 2. I get 2 for the quotient and 1 for the remainder. When I enter 9,3 I get 6 remainder 1..
Any ideas?
Upvotes: 0
Views: 7759
Reputation: 58802
Use the simulator to single step the code and find where it goes wrong.
That said, it's obvious that the ZERO
case still has the remainder in R1
so using R3
is wrong. I recommend you keep the remainder in R1
for the NEGATIVE
block too and adjust the final part of the code for this.
The quotient should have been fine, I don't know why you got 6
for the 9/3
(if you really did).
Upvotes: 1