(IN LC3, Assmbly) Vector multiply: multiplies a number in one vector with the co
ID: 3684593 • Letter: #
Question
(IN LC3, Assmbly) Vector multiply: multiplies a number in one vector with the corresponding number in another vector.
Simulate this behavior in LC-3. May use eight registers. To do this task, need to multiply until all numbers in each vector have been multiplied. Input vectors given as follows:
Length of the 2 vectors at 0x3040.
The start of first vector at 0x3041.
The start of second vector at 0x3051.
The answer stored starting at 0x3061.
Note: No vector will exceed 10 in length.
EX: if vectors were [3,4,5] and [1,2,3],
answer, stored at 0x3061 through 0x3063, should equal [3 x 1, 4 x 2, 5 x 3] = [3,8,15].
Explanation / Answer
Like subtraction, LC-3 does not natively support multiplication.
Multiplication is really a series of additions. Take for example 5 x 4. It can really be viewed as 5 + 5 + 5 + 5. This is how we have to treat multiplication in LC-3 programming.
We need to set up a few labels. We will need to know the two numbers we are multiplying. Let's use 4 and 5.
Code:
FIRST_NUMBER .fill #5
SECOND_NUMBER .fill #4
Now we have to start the heart of our program. We will use a blank register to store the sum of our operations. We want to enter into a loop that will decrement #4 until it reaches #0. This is the best way to keep track of how many times we have added 5 to the sum.
Code:
; R1 is our sum
AND R1, R1, #0 ; set R1 to zero
LD R2, FIRST_NUMBER
LD R3, SECOND_NUMBER
; enter loop
MULTIPLY ADD R1, R1, R2 ; add to sum
ADD R3, R3, #-1 ; decrement our counter
BRp MULTIPLY ; continue until the 2nd num is 0
Instead of multiplying two static values, let's grab two numbers from the user and multiply them. For simplicity, we will only grab one digit numbers.
Code:
.ORIG x3000
; R1 is our sum
AND R1, R1, #0 ; set R1 to zero
; R3 is our counter
AND R3, R3, #0
; R4 is our inverse ascii offset
LD R4, INVERSE_ASCII_OFFSET
; output prompt
LEA R0, PROMPT
PUTS
; get first character
GETC
OUT
; store character in R5
ADD R5, R0, #0
; get real value of R5
ADD R5, R5, R4
; output prompt
LEA R0, PROMPT
PUTS
; get second character
GETC
OUT
; store character in R2
ADD R2, R0, #0
; get real value of R2
ADD R2, R2, R4
; set R2 as our counter
ADD R3, R2, #0
; enter loop
MULTIPLY:
ADD R1, R1, R5 ; add to sum
ADD R3, R3, #-1 ; decrement our counter
BRp MULTIPLY ; continue until the 2nd num is 0
HALT
INVERSE_ASCII_OFFSET .fill #-48
PROMPT .stringz "Enter a number:"
.END
Note-by modifying the above code to load 3 input values for 1st vector and to load 3 input values for 2nd vector and repeat the addition operation in multiply block for all the three set of values. By using the above explanation the given question can be solved.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.