cmipsspim

Expressing this statement in MIPS


I've just started on MIPS with SPIM simulator. Can someone help me converting this statement ?

if(as>47 && as<58) function();
else continue;

Thanx in advance. :)


Solution

  • My MIPS is a bit rusty, so apologies in advance if this does not work without minor tweaking, but this should hopefully give you a good idea of what you are trying to do.

    (If you do find that this does not work, please let me know so I can edit the post)

    # Assume 'as' is in $s0
    li $t2, 1           # $t2 = 1
    slti $t0, $s0, 58   # $t0 = $s0 < 58
    addi $t1, $s0, 1    # $t1 = $s0 + 1
    slti $t1, 47, $t1   # $t1 =  47 < $t1($s0 + 1) (sgti does not exist)
    and $t0, $t0, $t1   # $t0 = $t0 && $t1
    
    bne $t0, $t2, cont      # if ($t0 != $t2) goto cont
    
    function: # Label is optional.
    # If both conditions are true, the bne won't branch, so we will
    # fall through to function: and run whatever code it has.
    # otherwise, we jump to cont: and all the func code is skipped.
        # ...
    
    cont: # continue;
    
        # ...
    

    Take note that right now, function() is not actually a function. you could however jal function and have that block reside somewhere else. Here's a good reference of the MIPS instruction set.

    The trick in MIPS is that since you don't have a greater than instruction, you must use the opposite.

    Remember that the opposite of > is NOT <, it is <=.