MIPS multiplication using addition

纵饮孤独 提交于 2019-12-24 15:59:23

问题


sorry, I try to multiply two integers but it doesn't work. I can't find where the problem is.Maybe because of register' names but I don't know how to correct it. I correct many times but it is not successful. Could anyone give me some points?

.data
prompt1: .asciiz "Please enter the first signed (decimal) integer: "
prompt2: .asciiz "Please enter the second signed (decimal) integer: "
result_msg: .asciiz "The result of these two 16-bit integers\' multiplication is: "
.text
.globl main
main:
      li $v0, 4        #print prompt
      la $a0, prompt1
      syscall

      li $v0, 5        #read multiplicand 
      syscall
      move $s0, $v0

      li $v0, 4        #print prompt
      la $a0, prompt2
      syscall

      li $v0, 5      #read multiplier
      syscall
      move $s1, $v0

Mult: ori $t0,$zero,1  #mask
      move $s3, $0    #initialize the result register
      move $t1, $0


loop: beq $s1, $zero, end  #if the multiplier is 0 then finished
      and $t1, $t0, $s1      #mask
      beq $t1, 1, mult_add   
      beq $t1, 0, shift

mult_add:  addu $s3, $s3, $s0     #add to get product

shift: 
      sll $s0, $s0, 1    #shift multiplicand left
      srl $s1, $s1, 1    #shift multiplier right

      j loop

end:
     jr $ra                

result:                            #input the print_string
      li $v0, 4
      la $a0, result_msg
      syscall


exit:
      li $v0, 1            #input result
      move $a0, $s3
      syscall

      li $v0, 10        #exit
      syscall

回答1:


Inspecting your code I see that you jump to label end when you are done multiplying.

The instruction at that label issues a jr $ra which "returns from a function", but I guess you just want to print the result and exit.

Therefore I'd suggest you remove that instruction so as to print the result and exit and maybe remove label result as it is not used anywhere in your code.



来源:https://stackoverflow.com/questions/33672323/mips-multiplication-using-addition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!