繁体   English   中英

通过移位和加法在 MIPS 中进行乘法,而不是乘法

[英]Multiplication in MIPS by shifting and adding, without mult

我有以下代码,但我一直收到算术溢出错误。 我试图解决的问题是将两个 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(十进制)

这是一个可能的实现。

与您的代码的差异:

  • 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM