Write an SRC assembly code to work both multiplication (*) and division (/). Ope
ID: 2248901 • Letter: W
Question
Write an SRC assembly code to work both multiplication (*) and division (/). Operands are defined as constants, and the operator is defined as an ASCII character as follows:
Character ASCII
* 42
/ 47
op1 * op2 OR op1 / op2
Your program must read first operand, the operator, and then the second operand as above. Store the result in the memory location for result. When the operation is division (/), the result is the integer part of the division (e.g., 9/4 = 2). If the second operand in division is zero, store -1 in the result.
You may start with the following template.
;
; CPE 221 HW 4 ; date:
; student name:
; Multiplication and division (integer division) using addition and ; subtraction in SRC assembly language.
; If the second operand in division is zero, store -1 in the result.
;
.org 1000
ld r1, op1 ; r1 holds operand1 ld r2, op2 ; r2 holds operand2 ld r3, operator ; r3 holds the operator lar r4, result ; r4 points to the location of result
lar r8, end
lar r10, multiplication lar r12, division
addi r5,r3,-42 ; check for multiplication operation brzr r10,r5 ; if yes, branch to multiplication
addi r5,r3,-47 ; check for division operation
brzr r12,r5 ; if yes, branch to division br r8
multiplication:
lar r13, aa lar r14, bb lar r15, loop1
lar r16, store
la r20, 0 ; r20: number of negative values in op1 and op2 brpl r13,r1 ; check the sign of the first operand
neg r1,r1 ; if the sign is negative, make it to be positive
addi r20,r20,1 ;
aa: ; DEFINE YOUR CODE ; check the sign of the second operand ; DEFINE YOUR CODE ; if the sign is negative, make it to be positive
; DEFINE YOUR CODE
bb: addi r22,r2, 0 ;save the second operand as counter la r6, 0 ; sum is initialized to 0
loop1: add ; DEFINE YOUR CODE
addi ; DEFINE YOUR CODE ; decrement the value of r22
; DEFINE YOUR CODE ; if r22’s value is nonzero, go to loop1
addi r20, r20, -1 ; r20's possible value was 0, 1, or 2
brnz r16,r20 ; if r20 = 0,
neg ; DEFINE YOUR CODE ; one operand was negative and the other was positive store: st ; DEFINE YOUR CODE br ; DEFINE YOUR CODE
division:
;
; CONTINUE YOUR CODING FOR DIVISION
;
end: stop ; exit
.org 3000
op1: .dc 20 ; it can be any integer value. operator: .dc "*" ; it can be either ‘*’ or ‘/’ op2: .dc -7 ; it can be any integer value.
result: .dw 1 ; storage for result
;
; end of the program
;
Explanation / Answer
DATA SEGMENT
NUM1 DB ?
NUM2 DB ?
RESULT DB ?
MSG1 DB 10,13,"ENTER FIRST NUMBER TO MULTIPLY : $"
MSG2 DB 10,13,"ENTER SECOND NUMBER TO MULTIPLY : $"
MSG3 DB 10,13,"RESULT OF MULTIPLICATION IS : $"
ENDS
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.