SLIDE 1
Thursday, 24 September (delayed)
Exam date re-set for Thursday, 8 October Conditional instructions in MIPS Loops
SLIDE 2 In C, Lots of Relational Operators:
“>”, “<”, “>=”, “<=”, “==”, “!=” We don’t need them all. For instance, “<” and “>”, “<=” and “>=” are symmetric: a < b is the same as b > a. Also, a <= b is the
- pposite of (the negation of) a > b, and a >=
b is the negation of a < b. Similarly, “==” and “!=” are opposites.
SLIDE 3
Relational Operators in MIPS
In MIPS, we have just “less than”, “equals”, and “not equals”. All the rest can be constructed from these. The “beq” instruction takes two registers and an instruction address: beq $t0,$t1,label Meaning: “If $t0 == $t1, go to label”
SLIDE 4
Relational Operators in MIPS
The “bne” instruction takes two registers and an instruction address: bne $t0,$t1,label Meaning: “If $t0 != $t1, go to label”
SLIDE 5
Example
C: if (i == j) { j = j-1; } Note how we used the “not equal” branch (bne) to skip around the “j=j-1” code in MIPS. MIPS: # Assume i = $t0, j = $t1 bne $t0,$t1,skip subi $t1,$t1,1 skip: ...
SLIDE 6
Unconditional Branching
MIPS has a way to go to any labeled statement unconditionally: the “j” instruction (for “J”ump): j label where “label” is a label of some other instruction.
SLIDE 7
Unconditional Branching--Example
C: if (a != b) { a++; } else { b++; } We used the “j” command to skip around the “else” part. MIPS: # Assume a = $s0, b = $s1 beq $s0, $s1, else addi $s0,$s0,1 j done else: addi $s1,$s1,1 done: ...
SLIDE 8
What About “<”?
“Less than” operations are slightly more complicated to implement than equality (for “==” all we need to do is compare bit patterns; for “<” we need to worry about signed numbers). As a result, the implementers of MIPS did not create “branch” instructions for “<”, “<=”, etc. (although these might exist as “pseudoinstructions” in some implementations). Instead, they split this into a two-step process: first, set a register with the results of the comparison; second, check the contents of the register and do a “bne” or “beq” branch.
SLIDE 9
The “slt” Instruction
The slt instruction stands for “set if less-than”. It takes three register arguments: slt $x, $y, $z and it does the following: if $y < $z then $x = 1 else $x = 0 TERMINOLOGY NOTE: in electronics and circuit design, “set” means “set to 1”, “reset” means “set to 0”. So “set if less-than” means “set to 1 if less-than”
SLIDE 10
The “slt” Instruction
We usually use slt in combination with a beq or bne. For instance, let’s translate the following if-statement into MIPS (I’ve used register names as variables to help you understand): if (t1 <= t2) t3++; else t4++; slt $t0,$t2,$t1 bne $t0,$zero,else addi $t3,$t3,1 j done else: addi $t4,$t4,1 done: ...
SLIDE 11
For More Examples...
The class shared repository has more examples. In folder “sep24” you will find: conditional.asm loop.asm fib.asm