I\'m tring to mutiply two numbers in mips by using the multiplication algorithm,
ID: 3924263 • Letter: I
Question
I'm tring to mutiply two numbers in mips by using the multiplication algorithm, this is my code below. I have no idea what wrong with it, can anyone help me to make the code wrok?
.data
newline: .asciiz " "
msg: .asciiz "Please enter two numbers one for multiplier, another for multiplicand."
msg1: .asciiz "multiplier="
msg2: .asciiz "multiplicand="
chara: .asciiz "*"
chara1: .asciiz "="
.text
.globl main
main:
#print the strings in msg
la $a0, msg
li $v0, 4
syscall
la $a0,newline
syscall
#get the multiplicand from user
la $a0, msg2
li $v0, 4
syscall
li $v0,5
syscall
move $t1, $v0
la $a0,newline
syscall
#get the multiplier from user
la $a0, msg1
li $v0, 4
syscall
li $v0,5
syscall
move $t0, $v0
la $a0,newline
syscall
move $s3,$0
move $s4,$0
li $s1,32
or $s0,$0,$0
loop: beq $s0,$s1,done
andi $t2,$a0,1
bne $t2,$0,multr1
addi $s0,$s0,1
j loop
syscall
mult0: sll $t1,$t1,1
sll $t0,$t0,1
mult1: addu $s3,$s3,$t1
sll $t1,$t1,1
sll $t0,$t0,1
done: add $a0,$t1,$0
li $v0, 1
syscall
la $a0, chara
li $v0, 4
syscall
add $a0,$t0,$0
li $v0, 1
syscall
la $a0, chara1
li $v0, 4
syscall
add $a0,$s3,$0
li $v0, 1
syscall
li $v0,10
syscall
Explanation / Answer
.data
promptStart: .asciiz "Please enter two numbers one for multiplier, another for multiplicand."
getA: .asciiz "multiplier="
getB: .asciiz "multiplicand="
space: .asciiz " "
result: .asciiz "="
newLine: .asciiz " "
.text
main:
#"welcome" screen
li $v0,4 # code for print_string
la $a0,promptStart # point $a0 to prompt string
syscall # print the prompt
li $v0,4 # code for print_string
la $a0,newLine # point $a0 to prompt string
syscall # print the prompt
#prompt for multiplicand
li $v0,4 # code for print_string
la $a0,getA # point $a0 to prompt string
syscall # print the prompt
#acquire multiplicand
li $v0,5 # code for read_int
syscall # get an int from user --> returned in $v0
move $s0,$v0 # move the resulting int to $s0
move $s5,$s0 # copy of multiplicand to use in multu
#prompt for multiplier
li $v0,4 # code for print_string
la $a0,getB # point $a0 to prompt string
syscall # print the prompt
#acquire multiplier
li $v0,5 # code for read_int
syscall # get an int from user --> returned in $v0
move $s1,$v0 # move the resulting int to $s0
move $s6,$s1 # copy of multiplier to use in multu
jal MyMult
j print
MyMult:
move $s3, $0 # lw product
move $s4, $0 # hw product
beq $s1, $0, done
beq $s0, $0, done
move $s2, $0 # extend multiplicand to 64 bits
loop:
andi $t0, $s0, 1 # LSB(multiplier)
beq $t0, $0, next # skip if zero
addu $s3, $s3, $s1 # lw(product) += lw(multiplicand)
sltu $t0, $s3, $s1 # catch carry-out(0 or 1)
addu $s4, $s4, $t0 # hw(product) += carry
addu $s4, $s4, $s2 # hw(product) += hw(multiplicand)
next:
# shift multiplicand left
srl $t0, $s1, 31 # copy bit from lw to hw
sll $s1, $s1, 1
sll $s2, $s2, 1
addu $s2, $s2, $t0
srl $s0, $s0, 1 # shift multiplier right
bne $s0, $0, loop
done:
jr $ra
print:
# print result string
li $v0,4 # code for print_string
la $a0,result # point $a0 to string
syscall # print the result string
# print out the result
li $v0,1 # code for print_int
move $a0,$s4 # put result in $a0
syscall # print out result
li $v0,4 # code for print_string
la $a0,space # point $a0 to string
syscall # print the result string
li $v0,1 # code for print_int
move $a0,$s3 # put result in $a0
syscall # print out result
# print the line feed
li $v0,4 # code for print_string
la $a0,newLine # point $a0 to string
syscall # print the linefeed