Abstract
- MIPS executes instructions in a sequential manner. It follows the instructions one by one in order. If we want to have loops, we need the ability to change the order of executing instructions
Labels are not instructions
Labels usually point to the target of branching or jumping. They aren’t stored in the generated machine codes!
Important
Any form of loop can be written in Assembly language with the help of Conditional Branch and Unconditional Jump.
For
for
, we convert it to awhile
loop first. Forwhile
loop, we can rewrite it withif
statements andgoto
.For
if
statements, we can rewrite it with conditional branch. Forgoto
, we can rewrite it with unconditional jump.
Conditional Branch
bne $t0, $t1, label
branches tolabel
when$t0 != $t1
. This is similar toif ($t0 != $t1) goto label
beq $t0, $t1, label
branches tolabel
when$t0 == $t1
. This is similar toif ($t0 == $t1) goto label
Translating from C to MIPS
To optimise the number of MIPS instructions, consider inverting the condition. For example, use
bne
(branch if not equal) forif ($t0 == $t1) goto label
andbeq
(branch if equal) forif ($t0 != $t1) goto label
.
New address
When branch is taken, Program Counter PC = (PC + 4) + (Immediate Value * 4).
Unconditional Jump
j label
branches tolabel
. This is similar togoto label
MIPS Inequality
slt $t0, $s1, $s2
is same as$t0 = $s1 < $s2 ? 1 : 0
Important
blt $s1, $s2, L
is a Pseudo Instruction which gets translated toslt $t0, $s1, $s2
, followed bybne $t0, $zero, L
by the Assembler.