assemblymipsqtspim

Solving a multiplication problem in mips without using MUL command


What I want to do?

Translate this code without using mul command but only the other possible commands like add, subtruct and logical shift commands.

.text
.globl main
main:              # execution starts here
    li $t0,19057        
    li $t1,123               # input data in $t1
    sll $t0,$t0,27
    srl $t0,$t0,26
    mul $t2,$t1,$t0
    li $v0,10   
    syscall 

The program created must have the same exit as the initial one in $t2 register without using the mul command. With what commands should I replace the mul command in order to have the same output?

Note: $t1 register can take any number from 1 to 10.000.000.


Solution

  • I solved it by using a loop that simulates how the MUL command works. It then stores the final result in the register $t2.

    .text
    .globl main
    
    main:              # execution starts here
    li $t0,19057        
    li $t1,123               # input data in $t1
    
    # shift left by 27 bits
    sll $t0,$t0,27
    
    # shift right by 26 bits
    srl $t0,$t0,26
    
    # initialize result to 0
    li $t2, 0
    
    # loop until the multiplier is 0
    loop:
    beq $t0, $0, end
    
    # add the multiplicand to the result
    add $t2, $t2, $t1
    
    # decrement the multiplier
    addi $t0, $t0, -1
    
    # jump to the beginning of the loop
    j loop
    
    # end of the multiplication
    end:
    
    li $v0,10   
    syscall