Greater than, less than equal, greater than equal in MIPS

jaynp picture jaynp · Mar 3, 2013 · Viewed 94.8k times · Source

Given two registers $s0, $s1, how can I convert the following pseudocode into MIPS assembly language using only the slt (set on less than) and beq and bne (branch if equal, branch if not equal) instructions.

   if ($s0 > $s1) { goto label1 }
   if ($s0 >= $s1) { goto label2 }
   if ($s0 <= $s1) { go to label3 }

Answer

glew picture glew · Mar 3, 2013
slt $t1,$s1,$s0      # checks if $s0 > $s1
beq $t1,1,label1     # if $s0 > $s1, goes to label1
beq $s1,$s2,label2   # if $s0 = $s2, goes to label2 
beq $t1,$zero,label3 # if $s0 < $s1, goes to label3

I'm assuming that the pseudocode executes sequentially, and that you can't go to two different labels.