Day 4

2.7 Instructions for making Decisions

  • MIPS has two instructions for making decisions

    • Using a value in our program to determine program flow.

  • Branch Equal

    • Let’s us compare two items, and if they are equal move to a defined location.

    • MIPS instruction: beq $s0,$s1,label

    • if ($s0 == $s1)

      • goto Label

  • Branch Not Equal

    • Compare two values and look for inequality.

    • MIPs instruction: bne $s0,$s,label

    • if ($s0 ≠ $s1)

      • goto label

  • Jump

    • unconditional goto label

    • j label X

  • Labels named, defined locations

    • in our programs or memory

  • HL (High Level) Code:

    • if (i == j)

      • (i&j are $s3 & $s4)

        • f = g+h

          • f = $s0, g=$s1, h= $s2

    • Assumptions:

      • f-g in $s0-$s4

      • Want the code to “flow” the same

    • MIPS Code:

      • bne $s3, $s4, else

      • ad $s0,$s1,$s2

      • j EXIT

      • else

      • sub $s0,$s1, $s2

      • EXIT