perform arithmetic operation for multiplication and division in LC-3 assembly la
ID: 652314 • Letter: P
Question
perform arithmetic operation for multiplication and division in LC-3 assembly language Please help
Procedure:
Integer Multiplication and Division
You will implement two functions:
Y = A x B
Z = A / B
In high level statement above, Y and Z are destinations in memory, and A and B are sources in memory. Recall that the LC-3 is a Load/Store machine. Before operating on the values stored in A and B, we must first load them into registers.
The source operand 0<A<128 is stored in location 0x3100 in memory.
The source operand 0<B<128 is stored in location 0x3101.
The result Y should be stored in location 0x3102.
The result Z should be stored in location 0x3103.
Your program should start at location 0x3000.
Explanation / Answer
Arithmetic operation for multiplication and division in LC-3 assembly language:
1. Multiplication in LC3:
ORIG x3000
AND R3, R3, #0 ;r3 stores the sum, set r3 to zero
AND R4, R4, #0 ;r4 is the counter
LD R5, INVERSE_ASCII_OFFSET ;inverse ascii offset
LD R6, DECIMAL_OFFSET ; decimal offset
;---------------------
; storing first input digits
LEA R0, display1 ; load the address of the 'display1' 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, display2 ;load the address of the 'display2' 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
display1 .STRINGZ " enter the 1st no.: "
display2 .STRINGZ " enter the 2nd no.: "
stringResult .STRINGZ " Result: "
INVERSE_ASCII_OFFSET .fill xFFD0 ; Negative of x0030.
DECIMAL_OFFSET .fill #48
.END
2. Division
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.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.