ECE 374: Computer Organization
Conditional Operations
Branch to a labeled instruction if a condition is true, otherwise, continue sequentially.
An example of a branch instruction is beq rs, rt, L1
which means if rs
and rt
are equal, then
skip to the label L1
.
You can also unconditionally jump using j <label>
.
If-Statement Example
C Code:
if (i == j)
f = g + h;
else
f = g - h;
MIPS ASM:
# $s0 â i
# $s1 â j
# $s2 â f
# $s3 â g
# $s4 â h
...
bne $s0, $s1, Else # Check that i and j are equal
add $s2, $s3, $s4
j End # Skip Block B
Else:
sub $s2, $s3, $s4
End:
...
Loop-Statement Example
C Code:
while (save[i] == k) i += 1;
MIPS ASM:
Loop:
ssl $t1, $s3, 2 # i multiplied by 4 (because integers are 4 bytes)
add $t1, $t1, $s6 # Add computed i*4 to the base address of save
lw $t0, 0($t1) # Load value at computed address
bne $t0, $s5, Exit # Execute the conditional statement
addi $s3, $s3, 1 # Increment i
j Loop # Continue the loop
Exit: