【问题标题】:MIPS: multiplying two 32 bit numbers, getting a 64 bitMIPS:将两个 32 位数字相乘,得到一个 64 位
【发布时间】:2015-04-09 19:50:59
【问题描述】:

我试图在 MIPS 中将两个 32 位数字相乘。我这里有一些使用 mult 操作的代码。我使用 mfhi 和 mflo 操作从 mult 获取这两个部分,但我不确定如何正确地将它们组合在一起。例如,我让他们分别打印出操作。

我一直使用的输入数字是: 1143330295和999999223

我从这个程序得到的输出(显然需要一些工作)是: 266202121 -1483441327

正确的输出应该是 1143329406632360785,或者如果这样更容易的话,我可以用二进制输出。

这段代码来自我正在处理的一个更大的项目,它必须使用移位和相加算法将两个 32 位数字相乘,而不使用 mult 函数。我只是想首先代表 64 位数字。感谢您的任何建议/意见。

.data
     getA:    .asciiz "Please enter the first number(multiplicand): "
     getB:    .asciiz "Please enter the second number(multiplier): "
     space:    .asciiz " "
     promptStart:    .asciiz "This program multiplies two numbers. "
     mipMult:   .asciiz "The product, using MIPs mult is: "
     endLine: .asciiz "\n"
.text
main:
    li  $v0,4           
    la  $a0,promptStart 
    syscall            


    li  $v0,4           
    la  $a0,endLine     
    syscall            

    #prompt for multiplicand
    li  $v0,4          
    la  $a0,getA        
    syscall             

    #acquire multiplicand
    li  $v0,5          
    syscall             
    move    $s0,$v0     
    move    $s5,$s0    

    #prompt for multiplier
    li  $v0,4           
    la  $a0,getB        
    syscall             

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

    move    $s6,$s1     # copy 

    mult    $s5, $s6
    mfhi    $t0
    mflo    $t1

    li  $v0,4        
    la  $a0,mipMult    
    syscall

    # print out the result (This is obviously not correct, I need some help)
    li  $v0,1         
    move    $a0,$t0         
    syscall      


    li  $v0,4          
    la  $a0,space       
    syscall            

    # print out the result
    li  $v0,1          
    move    $a0,$t1        
    syscall          

    # print the line feed
    li  $v0,4           
    la  $a0,endLine     
    syscall             


    li  $v0,10          
    syscall             # exit program

【问题讨论】:

    标签: assembly mips


    【解决方案1】:

    我猜你正在处理无符号整数,所以你应该使用multu 而不是mult

    要以十进制打印 64 位数字,我认为您可以实现一种算法,该算法采用 64 位数字的模 10,将其存储在内存中的某个位置,然后用商重复直到商为零。然后以相反的顺序遍历该字符串以逐位打印数字。

    在您的问题中,您说可以以二进制形式打印数字。这要容易得多,因为您可以从左到右为两个寄存器打印每个位的值(首先是高位,然后是低位)。

    要做到这一点,除了使用multu 而不是mult 改变你的电话

    # print out the result (This is obviously not correct, I need some help)
        li  $v0,1         
        move    $a0,$t0         
        syscall  
        li  $v0,4          
        la  $a0,space       
        syscall            
        # print out the result
        li  $v0,1          
        move    $a0,$t1        
        syscall
    

    move $t2, $t0
    jal print_binary
    move $t2, $t1
    jal print_binary
    

    并添加这个子程序:

    # $t2 should hold the number to print in binary
    print_binary:
         li $t3, 32
         li $v0, 1
    print_bit:
         subiu $t3, $t3, 1
         srlv $a0, $t2, $t3
         andi $a0, $a0, 1
         syscall
         bgtz $t3, print_bit
         jr $ra
    

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多