Write an SRC assembly code to work both multiplication (*) and division (/). Ope
ID: 3544101 • 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.
;
; 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
;
bb: addi r22,r2, 0 ;save the second operand as counter
la r6, 0
loop1: add ; DEFINE YOUR CODE
addi ; DEFINE YOUR CODE ; decrement the value of r22
; DEFINE YOUR CODE ; if r22
Explanation / Answer
Given positive integers a and b, output a/b and a%b. .data str1: .asciiz "Enter a: " str2: .asciiz "Enter b: " str3: .asciiz "a/b = " str4: .asciiz "a%b = " str5: .asciiz "a*b = " newline: .asciiz " " .text main: li $v0, 4 # system call code for print_string la $a0, str1 # address of str1 syscall # print str1 #get the first number from user, put it into $s0 li $v0, 5 # system call code for read_int syscall # read an integer into $v0 from console add $s0, $v0, $zero # copy $v0 into $s0 (a) #read print_string for str2 li $v0, 4 # system call code for print_string la $a0, str2 # address of str1 syscall # print str1 # get second number from user, put it into $t1 li $v0, 5 #load syscall for read_int syscall #make the syscall move $s1, $v0 #move the number read into $s1(b) #DO THE CALCULATIONS................................................ div $s0, $s1 #diving $s0 by $s1 mflo $t0 #storing value of lo(quotient) in #register $t0 mfhi $t1 #storing value of hi(remainder) in #register $t1 mult $s0, $s1 mflo $t2 li $v0,1 move $a0, $t2 syscall li $v0,4 la $a0, str5 syscall #read print_string for str3 li $v0, 4 # system call code for print_string la $a0, str3 # address of str1 syscall # print str1 #print a/b li $v0, 1 #load syscall print_int into $v0 move $a0, $t0 #move the number to print into $t2 syscall # read print string for str4 li $v0, 4 la $a0, str4 syscall # print remainder li $v0, 1 move $a0, $t1 syscall #end of program li $v0, 10 #system call code for exit syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.