Branch Instructions and Conditional Statements


MIPS does not have if statements, nor does it have for or while statements. To create conditional processing and loops it is necessary to build the control structure with branch statements. A branch statement changed the flow of control. Normally, the execution of a statement is following by the execution of the next statement in the code. A branch instruction causes execution to jump to a different statement. Some branches are conditional -- they only branch if a specified condition is met. Others are unconditional -- they always cause a branch to occur.

Branch Instructions

In the table below, Rs and Rt represent registers.

 
Name
 
Format
 
Meaning
 
Example
Branch If Equal beq   Rs,Rt,label if Rs == Rt branch to label beq   $t1,$t2,next
Branch If Not Equal bne   Rs,Rt,label if Rs != Rt branch to label bne   $t1,$t2,else
Branch If Greater Than bgt   Rs,Rt,label if Rs > Rt branch to label bgt   $t1,$t2,top
Branch If Less Than blt   Rs,Rt,label if Rs < Rt branch to label blt   $t1,$t2,two
Branch If Greater Than or Equal bge   Rs,Rt,label if Rs >= Rt branch to label bge   $t1,$t2,done
Branch If Less Than or Equal ble   Rs,Rt,label if Rs <= Rt branch to label ble   $t1,$t2,three
Branch If Equal To Zero beqz  Rs,label if Rs == 0 branch to label beqz  $t1,after
Branch If Not Equal To Zero bnez  Rs,label if Rs != 0 branch to label bnez  $t1,second
Branch If Greater Than Zero bgtz  Rs,label if Rs > 0 branch to label bgtz  $t1,high
Branch If Less Than Zero bltz  Rs,label if Rs < 0 branch to label bltz  $t1,low
Branch If Greater Than or Equal To Zero bgez  Rs,label if Rs >= 0 branch to label bgez  $t1,somehi
Branch If Less Than or Equal To Zero blez  Rs,label if Rs <= 0 branch to label blez  $t1,somelo
Branch b     label branch to label b     loop

Conditional Statements

We use the branch statements shown above to construct conditional statements in MIPS. Here are some examples.

Pseudocode MIPS
if $t1 > $t2
   .
   . (1)
   .
endif
.
. (2)
.
         bgt   $t1,$t2,one
         b     cont
one:
         .
         . (1)
         .
cont:
         .
         . (2)
         .
if $t1 > $t2
   .
   . (1)
   .
endif
.
. (2)
.
         ble   $t1,$t2,cont
         .
         . (1)
         .
cont:
         .
         . (2)
         .
if $t1 == $t2
   .
   . (1)
   .
else
   .
   . (2)
   .
endif
.
. (3)
.
         beq   $t1,$t2,equal
         .
         . (2)
         .
         b     after
equal:
         .
         . (1)
         .
after:
         .
         . (3)
         .
if $t1 == $t2
   .
   . (1)
   .
else
   .
   . (2)
   .
endif
.
. (3)
.
         bne   $t1,$t2,noteq
         .
         . (1)
         .
         b     after
noteq:
         .
         . (2)
         .
after:
         .
         . (3)
         .
if $t1 > $t2 || $t3 <= $t4
   .
   . (1)
   .
else
   .
   . (2)
   .
endif
.
. (3)
.
         bgt   $t1,$t2,true
         ble   $t3,$t4,true
         .
         . (2)
         .
         b     endif
true:
         .
         . (1)
         .
endif:
         .
         . (3)
         .
if $t1 != $t2
   .
   . (1)
   .
else if $t3 > $t4
   .
   . (2)
   .
else
   .
   . (3)
   .
endif
.
. (4)
.
         bne   $t1,$t2,one
         bgt   $t3,$t4,two
         .
         . (3)
         .
         b     after
one:
         .
         . (1)
         .
         b     after
two:
         .
         . (2)
         .
after:
         .
         . (4)
         .


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2009