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 awhileloop first. Forwhileloop, we can rewrite it withifstatements andgoto.For
ifstatements, we can rewrite it with conditional branch. Forgoto, we can rewrite it with unconditional jump.
Conditional Branch
bne $t0, $t1, labelbranches tolabelwhen$t0 != $t1. This is similar toif ($t0 != $t1) goto labelbeq $t0, $t1, labelbranches tolabelwhen$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 labelandbeq(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
j labelj labelbranches tolabel. This is similar togoto label
MIPS Inequality
slt $t0, $s1, $s2is same as$t0 = $s1 < $s2 ? 1 : 0
Important
blt $s1, $s2, Lis a Pseudo Instruction which gets translated toslt $t0, $s1, $s2, followed bybne $t0, $zero, Lby the Assembler.
