MIPS中的乘法

时间:2019-03-27 05:20:42

标签: assembly mips mars-simulator

我有以下代码,但我不断收到算术溢出错误。我要解决的问题是将两个31位数字相乘并将结果存储在$ t2 $ t3中并打印出正确的结果。看来我已经编码了两个要相乘的数字,最终结果是一个31位数字。

我很想缩小自己想出问题的地方,但是老实说,我看不到需要改变的地方和内容。

# program to multiply two 31 bit binary numbers (A & B),

# using the “shift and add” .

.data

# declare the variable lables of ASCII storage type.

prompt1: .asciiz "Enter number 1: "

prompt2: .asciiz "Enter number 2: "

result: .asciiz "The multiplication of two 31 bit binary numbers is: "

.text

主要:

            #prompt1.

            li $v0, 4   

            la $a0, prompt1

            syscall

           #read number 1 and store in the register $t0

            li $v0, 5        

            syscall  

            move $t0, $v0



            #prompt2.

            li $v0, 4   

            la $a0, prompt2

            syscall



            #read number 2 and store in the register $t1

            li $v0, 5        

            syscall  

            move $t1, $v0



            li $t2, 0 # The final result of the multiplication

                            #is saved into the register $t2

            li $t3, 1 # Mask for extracting bit!

            li $s1, 0 # set the Counter to 0 for loop.   

相乘:

            #if the Counter $s1 is equal to 31, then go the lable exit

            beq $s1, 31, exit

            and $s2, $t1, $t3

            sll $t3, $t3, 1

                    beq $s2, 0, increment

            add $t2, $t2, $t0

增量:

            sll $t0, $t0, 1

            addi $s1, $s1, 1

            j multiply

退出:

            #display the result string.

            li $v0, 4   

            la $a0, result

            syscall

                            #display the result value.

            li $v0, 1

            add $a0, $t2, $zero

            syscall



            li $v0, 10 # system call code for exit = 10

            syscall   # call operating sys

样品输入A:1143330295(十进制) 样本输入B:999999223(十进制)

1 个答案:

答案 0 :(得分:1)

这是可能的实现方式。

与您的代码的不同之处:

  • 32x32乘法生成64位结果。在32位mips上,结果必须分成两个寄存器

  • 不是左移操作数,而是会导致溢出,结果是右移。被驱逐的位被保存并重新注入结果的下部

  • 使用addu进行添加。数字是无符号的,并且没有未签名的操作可能会发生

  • 以“ do while”形式更改了循环。循环计数器递减

当前显示的结果分为两部分。如果设置了LSB(可能被display int syscall视为负号),则可能会出现不正确的显示,但是大多数mips模拟器都无法显示大的未签名。

# program to multiply two 31 bit binary numbers (A & B),
# using the “shift and add” .

.data
# declare the variable lables of ASCII storage type.
prompt1: .asciiz "Enter number 1: "
prompt2: .asciiz "Enter number 2: "
result: .asciiz "The multiplication of two 31 bit binary numbers is: "
result2: .asciiz "\nand the upper part of result is: "

.text
main:
        #prompt1.
        li $v0, 4   
        la $a0, prompt1
        syscall

        #read number 1 and store in the register $t0
        li $v0, 5        
        syscall  
        move $t0, $v0

        #prompt2.
        li $v0, 4   
        la $a0, prompt2
        syscall

        #read number 2 and store in the register $t1
        li $v0, 5        
        syscall  
        move $t1, $v0

        li $t2, 0 # The final result of the multiplication is 64 bits
                  # MSB part is in register $t2
        li $t4, 0 #  and LSB part of result is in $t4
        li $t3, 1 # Mask for extracting bit!
        li $s1, 32 # set the Counter to 32 for loop.   

multiply:
        and $s2, $t1, $t3
        beq $s2, 0, increment
        addu $t2, $t2, $t0

increment:
        sll $t3, $t3, 1 # update mask
        ##sll $t0, $t0, 1 # useless, we srl result instead
        andi $s4, $t2,1  # save lsb of result
        srl $t2,$t2,1    # t2>>=1
        srl $t4,$t4,1    # t4>>=1
        sll $s4,$s4,31
        or  $t4,$t4,$s4  # reinject saved lsb of result in msb of $t4
        addi $s1, $s1, -1 # decrement loop counter
        #if the Counter $s1 reaches 0 then go the label exit
        beq $s1, $zero, exit
        j multiply

exit:
        #display the result string.
        ## must be changed to take into account the 64 bits of the result
        ## but AFAIK, there is no syscall for that
        ## can be done in two steps t4 and t2
        ## and result is t4+2**32*t2
        #display the result value.
        li $v0, 4   
        la $a0, result
        syscall
        li $v0, 1  # if using mars replace 1 by 36 to print as an unsigned
        add $a0, $t4, $zero
        syscall
        li $v0, 4   
        la $a0, result2
        syscall
        li $v0, 1 # if using mars replace 1 by 36 to print as an unsigned
        add $a0, $t2, $zero
        syscall

        li $v0, 10 # system call code for exit = 10
        syscall   # call operating sys