mathmipsmips32mips64

MIPS while loop


I am working on a while loop for a mips program that generates the odds of winning the lottery. Here is what I have so far. Could you advise if you think this will work? Using an example of 59 picks with 3 balls.

Load 59 into $t0
Load 3 into $t1
Load 1 into $t2 
Load 1 into $t3

Loop: while $t1 > 0
Divide $t2 by $t0 and put in $t4
Multiply $t4 by $t3 and put in $t3
Subtract $t1 by 1 #subtracts balls by 1
Subtract $t0 by 1 #subtracts max picks by
**repeat loop**

Could someone advise if this is possible in mips as I am just working on my first program and only have a Java background.


Solution

  • Yes, this is possible, but you have to put it in float-point registers $f0 ... $f31:

        li $t0 , 59  // values assignment to temporary registers
        li $t1 , 3
    
        mtc1 $t0,$f0 //move from int register to float-point register
        cvt.s.w $f0,$f0  //Convert int values to float-point values
    
        mtc1 $t1,$f1 
        cvt.s.w $f1,$f1 //Convert int values to float-point values
    
        li $t2 , 1
        li $t3 , 1
    
        mtc1 $t2, $f2
        cvt.s.w $f2,$f2
    
        mtc1 $t3, $f3
        cvt.s.w $f3,$f3
    
        li $t6 , 1
        mtc1 $t6, $f6
        cvt.s.w $f6,$f6
    
        //Start of the while loop
    
        li $t5, 0
        mtc1 $t5, $f5
        cvt.s.w $f5,$f5
    
        while : c.gt.s $f1, $f5  
        bc1f endwhile 
        //checks the intital condition and if it's false jump to endwhile
        div.s $f4,$f2,$f0 // $f4 = $f2/$f0
        mult.s $f3, $f3 ,$f4 // $f3 = $f3 * $f4
    
        sub.s $f0, $f0, $f6
        sub.s $f1, $f1, $f6
    
        b while //jump to label while
        endwhile: // your code
    

    If you have any trouble, maybe you can check this webpages, think it's a good reference : http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm http://www.cim.mcgill.ca/~langer/273/12-coprocessors.pdf