How to do less than or equal in Assembly Language(MIPS)?

I have a C code in front of me that I have to translate into a MIPS assembly language.

I am not looking for a direct answer, but I want someone to correct the way I'm thinking about the problem.

The C code in front of me is:

x = ((++z)<=y);

I have in the given that the x, y, and z are respectively stored in the registers $6, $7, $8

The problem is I can't use an operator to compare directly less than or equal. I am limited to use the following comparing operands: bne, beq, ori, slt.

The way I approached the problem was as such:

      addi   $8,$8,1     #this will increment z by 1 to have ++z
      slt    $1,$8,$7    #compares ++z to y if ++z is < than y, it will store 1 in $1
      beq    $8,$7,Label #compares if $8 = $7, if so the code jumps to Label
Label addi   $t0,$0,1    #if ++z = y, stores 1 in $t0
      ori    $6,$t0,$1   #Or's the t0 and t1 and accordingly stores 0 or 1 in x

Is this the right approach to this problem ?


As pointed out by Michael, you should have the label one line below like this:

         addi   $8,$8,1
         slt    $1,$8,$7
         beq    $8,$7,Label
         addi   $t0,$0,1
Label    ori    $6,$t0,$1

What's interesting is that you use slt but not seq, why not? You probably could do that:

         addi   $8,$8,1
         slt    $1,$8,$7
         seq    $t0,$8,$7
         ori    $6,$t0,$1

And by avoiding a branch, the program can be slightly faster (assuming that case happens many times.)

Now, if I remember correctly, slt and seq will generate either 0 or 255. If you are programming a C/C++ compatible statement, then you need to have 0 or 1 instead. One way is to add a shift, something like this after the ori:

         srl    $6,$6,7

Need Your Help

Generating Liquibase metadata offline


I'm currently experimenting with Liquibase to generate SQL for our database migrations. Due to some constraints within our environment, we need to generate the SQL "offline" and then have that exec...