2
votes

I am attempting to multiply two 32 bit numbers in MIPS. I have some code here that uses the mult operation. I use the mfhi and mflo operations to get the two parts from mult, but I am not sure how to properly put these together. I have them print out the operations separately just for example.

The input numbers I have been using are: 1143330295 and 999999223

The output I get from this program (obviously needs some work) is: 266202121 -1483441327

The correct output would be 1143329406632360785 or I can do it in binary if that would be easier.

This code is from a larger project I am working on that will have to multiply two 32 bit numbers using a shift and add algorithm without using the mult function. I am just trying to wrap my head around representing the 64 bit number first. Thanks for any advice/input.

.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
1

1 Answers

2
votes

I guess you are dealing with unsigned integers, so you should use multu instead of mult.

To print the 64-bit number in decimal I think you can implement an algorithm that takes the modulus 10 of the 64 bit number, stores it somewhere in memory, and repeat with the quotient until the quotient is zero. Then traverse that string in reverse order to print the number digit-by-digit.

In your question you said it would be ok to print the number in binary. That is much easier as you can print the value of each bit from left to right for both registers (hi order first, then low order).

To do that, besides using multu instead of mult change your call

# 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

with

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

and add this subroutine:

# $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