In: Computer Science
using MIPs assembly, ask the user to enter two numbers. Then multiply the two numbers using bit shifting (not 'mul'). This should work multiplying 2 positive numbers, 2 negative numbers, and a positive/negative number.
Then output the result and ask user if he/she has more calculations to complete.
product.asm:
.data
textout:.asciiz "please enter 2 numbers \n"
textout1:.asciiz "please enter 1st number: "
textout2:.asciiz "please enter 2nd number: "
textout3:.asciiz "The product of the numbers is: "
.text
main:
li $s2,0 #result initialized too zero
la $a0 textout #print prompt string
li $v0 4
syscall
la $a0 textout1 #print prompt string
li $v0 4
syscall
li $v0 5
syscall
move $s0,$v0
la $a0 textout2 #print prompt string
li $v0 4
syscall
li $v0 5
syscall
move $s1,$v0
bltz $s0,a #check if 1st number is
negative if negative jump to a
bltz $s1,b #check if 2nd number is
negative if negative jump to b
j multiply
a: bltz $s1,both #check if 2nd number is negative if
both are negative jump to both
li $t0,1 #flag = 1
sub $s0,$zero,$s0 # make the 1st number positive
j multiply
b: li $t0,1 #flag = 1
sub $s1,$zero,$s1 # make the 2nd number positive
j multiply
both: li $t0,2 #if both numbers are
negative
sub $s0,$zero,$s0 # make the 1st number positive
sub $s1,$zero,$s1 # make the 2nd number positive
j multiply
multiply: beqz $s1,res
andi $t1,$s1,1
beqz $t1,add_s
add $s2,$s2,$s0
add_s: sll $s0,$s0,1
srl $s1,$s1,1
j multiply
res: beq $t0,1,neg #if flag = 1 make result
negative
j end
neg: sub $s2,$zero,$s2
j end
end: la $a0 textout3
li $v0 4
syscall
move $a0,$s2 #print result
li $v0,1
syscall
Output: