CS274: Computer Architecture - MIPS Conditionals
Activity Goals
The goals of this activity are:- To implement
ifandelsestatements using MIPS assembly.
The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: An if/else statement in MIPS
Questions
- Translate the above code into a C, Java, or Python program. What does it do?
- What would happen if the
j exitline was omitted? - How would you modify this program to print the secret number using a syscall?
- Translate the
beqinstruction to machine language. - Translate the
jinstruction to machine language. - What is the farthest you can move the program counter with a branch instruction? How about a jump instruction?
- Suppose you wanted to jump to a location that caused the upper four bits of the program counter to be modified. Using a combination of branch and jump instructions, perform this jump.
Model 2: Implementing MIPS Conditionals
Questions
- Translate the above C code into a MIPS assembly program
Model 3: Key Formulas and Concepts Recap
A one-page summary of conditionals in MIPS.
Translation patterns for every comparison (note the trick: to run the body when a condition holds, branch away on its opposite!):
| C condition | MIPS to skip the body when false | Why |
|---|---|---|
if (a == b) |
bne $t0, $t1, skip |
Branch away on the opposite: not-equal |
if (a != b) |
beq $t0, $t1, skip |
Branch away on equal |
if (a < b) |
slt $at, $t0, $t1 then beq $at, $zero, skip |
$at = 1 if a < b; skip when it is 0 (i.e. a >= b) |
if (a >= b) |
slt $at, $t0, $t1 then bne $at, $zero, skip |
Skip when a < b is true |
if (a > b) |
slt $at, $t1, $t0 then beq $at, $zero, skip |
a > b is the same as b < a: swap the operands |
if (a < 100) |
slti $at, $t0, 100 then beq $at, $zero, skip |
Comparing against a constant uses slti |
The if/else skeleton (compare with the C on the left):
C MIPS
--------------------- ----------------------------------
if (a == b) { bne $t0, $t1, else_part
/* then code */ # ... then code ...
} else { j endif
/* else code */ else_part:
} # ... else code ...
/* after */ endif:
# ... after ...
Concrete micro-example: with $t0 = 7 and $t1 = 7, bne is not taken (7 == 7), the then-code runs, and j endif hops over the else-code. With $t0 = 3, bne is taken and only the else-code runs. Forgetting the j endif would run both bodies!
Glossary:
| Term | One-line definition |
|---|---|
| Branch | A conditional change of the PC (beq, bne), reaching PC-relative targets within +/- 32K words. |
| Jump | An unconditional change of the PC (j), reaching anywhere in the current 256 MB region. |
| Label | A named address used as a branch or jump target; the assembler computes the offset for you. |
| Fall through | What happens when a branch is not taken: execution continues with the next instruction. |
slt / slti |
Set-if-less-than: writes 1 or 0 to a register, turning a comparison into data a branch can test. |
$at |
The assembler temporary register (1), reserved for pseudoinstruction expansions like blt. |
| Pseudoinstruction | blt $t0, $t1, L is really slt $at, $t0, $t1 + bne $at, $zero, L. |
| Branch inversion | The compiler's habit of branching on the opposite condition to skip the body. |