Conditions in MIPS

 


What about MIPS?      [beq, bne, j, slt]

Branch if equal (beq), Branch if not equal (bne):

Syntax for beq and bne:

        beq reg1, reg2, label   # if reg1 == reg2, branch to label
        bne reg1, reg2, label   # if reg1 != reg2, branch to label

Example 2:

	C					FORTRAN
	-------					-------
	temp = a;				TEMP = A
	if ( a != b )				IF (A .EQ. B) GOTO ENDIF
	   temp = (a+b)/2;			TEMP = (A+B)/2
	temp += c;			ENDIF:	TEMP = TEMP + C
Assume that $s0 = a, $s1 = b, $s2 = c, $t0 = temp.
            move $t0, $s0           # temp = a
            beq $s0, $s1, ENDIF     # if ( a == b ) goto endif
            add $t0, $t0, $s1       # temp += b, i.e., temp = a + b
            srl $t0, $t0, 1         # temp /= 2 (shift right logical)
     ENDIF: add $t0, $t0, $s2       # temp += c

There is also a j instruction (jump no matter what):

	j label                 # jump to label
NOTE: labels must be unique.

Set-on-less-than (slt):

    	slt destReg, srcReg1, srcReg2   # set destReg to 1 if srcReg1 < srcReg2
        bne destReg, $zero, label   # branch if srcReg1 < srcReg2 (destReg is 1)
        beq destReg, $zero, label   # branch if srcReg1 >= srcReg2

Exercise:

Assume that $s0 = a, $s1 = b, $s2 = c, $s3 = diff, $s7 = max. Write the MIPS assembly code for the following C code fragment.
        /* compute diff = c - max(a, b) */
	max = a;
	if ( a < b )
	   max = b;
	diff = c - max;

 


Alyce Brady, Kalamazoo College