Conditional Ops
Conditional Ops
ARCHITECTURE
Conditional Ops
Making Decisions
A computer distinguishes itself from a simple calculator by its ability to make decisions
based on input data and computed values.
Conditional Branching
An instruction that compares two values and transfers control to a new
address in the program depending on the result of the comparison.
Page 90
Example 1
In the following code segment, f, g, h, i, and j are variables. If the five variables f through j correspond
to the five registers $s0 through $s4, what is the compiled MIPS code for this C if statement?
if (i == j) f = g + h; else f = g - h;
Answer
Conditional_Ops
i = j i ≠ j
i == j?
f = g + h Else: f = g - h
Exit:
Loops
Loops are essential for iterating a computation. Like decision-making instructions,
loops in assembly language also rely on conditional branches and jump instructions.
Basic Blocks
A basic block is a sequence of instructions in a program that does not
contain any branches, except possibly at the end, and does not have
branch targets or labels except at the beginning. These blocks help
optimize and organize the program during the compilation process.
Breaking a program into basic blocks is one of the early steps in compilation.
These blocks help optimize and organize the program during the compilation process.
Page 92
Example 2
Here is a traditional loop in C:
while (save[i] == k)
i += 1;
Assume that i and k correspond to registers $s3 and $s5 and the base of the array save is in $s6.
What is the MIPS assembly code corresponding to this C segment?
Answer
Conditional_Ops
Set on Less
Than (slt or slti)
Example
slt $t0, $s3, $s4
This instruction compares two registers and sets a third register to 1 # Sets $t0 to 1 if $s3 < $s4,
if the first register is less than the second, or 0 otherwise. otherwise $t0 = 0
slti: This is an immediate version of the slt instruction,
allowing for comparisons with constants.
Jump Tables
A jump table is an array of addresses that point to different parts of the program.
The program uses the value of the switch expression to index into the jump table
and then jumps to the corresponding instruction.
The MIPS instruction to support this is the jump register jr,
which allows for an unconditional jump to an address stored in a register.
Answer
Answer
Conditional_Ops
Transfer control to
02 the procedure.
Allocate storage resources
for the procedure. 03
$a $v $ra
Argument registers ($a0 - $a3) Value registers ($v0 & $v1) Return address register
These are used to pass up to four These are used to return results This is where the address of the
parameters to a procedure. from a procedure. instruction immediately following
(reg’s 4 – 7) (reg’s 2 & 3) the procedure call is stored.
(reg 31)
Jump-and-Link (jal)
This instruction is used to call a procedure in MIPS.
It performs two actions simultaneously: Example
1. Jumps to the address of the procedure. jal ProcedureAddress # Jumps to
2. Saves the address of the instruction immediately ProcedureAddress and saves return
following the jal in the return address register ($ra). address in $ra
This return address allows the procedure to return control to the calling point in the program.
Procedure Calling (Cont.)
Jump Register (jr) Example
jr $ra # Returns to the address
It is used to jump to the address stored in a register,
stored in the return
usually the return address register ($ra). Once the procedure has finished
address register ($ra)
1 Third element
Page 98
Example 5
int leaf_example (int g, int h, int i, int j) {
int f;
f = (g + h) - (i + j);
return f;
}
What is the compiled MIPS assembly code?
Answer Conditional_Ops
leaf_example:
# Save registers used by the procedure
addi $sp, $sp, -12 # Adjust stack to make room for 3 items (12 bytes)
sw $t1, 8($sp) # Save register $t1 for later use
sw $t0, 4($sp) # Save register $t0 for later use
sw $s0, 0($sp) # Save register $s0 for later use
Answer Conditional_Ops
leaf_example:
# Save register $s0 (since it's a saved register, it must be preserved)
addi $sp, $sp, -4 # Adjust stack to make room for 1 item (4 bytes)
sw $s0, 0($sp) # Save register $s0