Search code examples
mipsdivisionmips32

Mips Continuous Divison


I'm trying to implement continuous division in mips

function_1:

    while:
        # a % 2 == 0
        DIV  $s0, $2 
        MFHI $t1
        bne  $t1, $0, exit

        # b % 2 == 0
        DIV  $s1, $2  
        MFHI $t2
        bne  $t2, $0, exit

        j while
   exit:

jr $ra

For some reason, the check for if a is even works, but the check for if b is even doesn't work. Even when a and b are both even, the rest of the instructions in the while loop don't execute, and a and b stay the same after calling the function.

Any input on what I'm doing wrong would be helpful.

Thanks!


Solution

  • You can check for evenness by andi $t0, $s2, 0x1 which will give a zero result in $t0 if $s2 is even. There's no need to use a div instruction. Also div $s1, $2 will divide register 16 ($s2) by register 2 ($v0), this is probably not what you want.