0% found this document useful (0 votes)
1 views16 pages

Conditional Ops

The document discusses conditional operations in computer architecture, specifically in MIPS assembly language, detailing instructions for branching based on equality and inequality, as well as loop structures. It explains the use of comparison instructions, jump tables, and procedures, including how to manage parameters and return values. Additionally, it covers stack operations and optimization of register usage in procedure calls.

Uploaded by

yoosefelbooz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views16 pages

Conditional Ops

The document discusses conditional operations in computer architecture, specifically in MIPS assembly language, detailing instructions for branching based on equality and inequality, as well as loop structures. It explains the use of comparison instructions, jump tables, and procedures, including how to manage parameters and return values. Additionally, it covers stack operations and optimization of register usage in procedure calls.

Uploaded by

yoosefelbooz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

COMPUTER

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.

Branch if Equal (beq) Example


beq $t0, $t1, L1
beq register1, register2, L1: Branch to the label L1 if the values in register1 # If $t0 == $t1, jump to label L1
and register2 are equal.

Branch if Not Equal (bne) Example


bne $t0, $t1, L1
bne register1, register2, L1: Branch to the label L1 if the values in register1 # If $t0 != $t1, jump to label L1
and register2 are not equal.

These instructions enable conditional branching in assembly language.

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

bne $s3, $s4, Else # Branch to Else if i != j


add $s0, $s1, $s2 # f = g + h
j Exit # Jump to Exit (end of if statement)
Else: sub $s0, $s1, $s2 # f = g - h
Exit: # End of if-then-else

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.

Loop (Loop:) Example


Loop: # Label for the loop
A sequence of instructions that is executed repeatedly based on a condition, # Loop code
with the iteration count being controlled by a counter or a comparison
between registers.
Exit: Ends the loop and proceeds to the rest of the code.

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

Loop: sll $t1,$s3,2 # $t1 = i * 4


add $t1,$t1,$s6 # $t1 = address of save[i]
lw $t0,0($t1) # $t0 = save[i]
bne $t0,$s5, Exit # Go to Exit if save[i] != k
addi $s3,$s3,1 # i = i + 1
j Loop # Go to Loop
Exit: # End of Loop
Comparison Instructions
In MIPS, comparisons are crucial for making decisions in loops and conditionals.
The most common comparison tests are equality and inequality,
but comparisons like less than are also frequently used in loops and conditions.

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.

Set on Less Than


Unsigned (sltu or sltiu)
Example
sltu $t0, $s3, $s4
This instruction compares two registers unsigned (without considering negative # Sets $t0 to 1 if $s3 < $s4,
numbers) and sets a third register to 1 if the first register is less than the second, otherwise $t0 = 0 (Unsigned)
or 0 otherwise.
Branch Instructions
Why Not Branch on Less Than?
MIPS avoids the complexity of including a branch on less than instruction, as it could either increase the
clock cycle time or introduce additional cycles per instruction. It uses simpler instructions like slt or slti,
which are faster and more efficient.

Why Not Set on Greater Than?


Although there is no direct "Set on Greater Than" sgt instruction in MIPS,
you can simulate this behavior by swapping the operands in the slt instruction.

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.

lw $t0, jump_table($s0) # Load jump address for case from table


jr $t0 # Jump to the address stored in $t0
Page 94
Example 3
Suppose register $s0 has the binary number: 1111 1111 1111 1111 1111 1111 1111 1111two
and that register $s1 has the binary number: 0000 0000 0000 0000 0000 0000 0000 0001two
What are the values of registers $t0 and $t1 aft er these two instructions?
slt $t0, $s0, $s1 # Signed comparison
sltu $t1, $s0, $s1 # Unsigned comparison

Answer

slt t0 --> s0 < s1 t0 --> -1 < 1 t0 = 1

sltu t1 --> s0 < s1 t1 --> 4,294,967,295 < 1 t1 = 0


Page 95
Example 4
Use this shortcut to reduce an index-out-of-bounds check: jump to IndexOutOfBounds
if $s1 ≥ $t2 or if $s1 is negative.

Answer

Conditional_Ops

sltu $t0,$s1,$t2 # $t0=0 if $s1>=length or $s1<0


beq $t0,$zero,IndexOutOfBounds #if bad, goto Error
Procedures in MIPS
A procedure (or function) is a stored subroutine that performs a specific task based on parameters
passed to it. Procedures help in structuring programs, improving readability, reusability, and abstraction.
They allow programmers to break down complex tasks into manageable parts.

Receive its parameters


to registers. 01

Transfer control to
02 the procedure.
Allocate storage resources
for the procedure. 03

04 Perform the task.


Return the result to
the calling program. 05
Return control to the
06 point of origin.
Procedure Calling
MIPS uses the following conventions for handling procedure calls, utilizing the 32 registers available:

$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)

Place Parameters Transfer Control


01 Pass parameters to the procedure using the
argument registers $a0–$a3.
02 The procedure is called using jal.

Acquire Resources Perform Task


03 The procedure allocates necessary resources
(local variables, temporary data).
04 The procedure executes its task.

Return Value Return Control


05 The result is placed in the return value
registers $v0–$v1.
06 The jr $ra instruction ensures the
program returns to the correct point after
the procedure completes.
More Registers Caller
The program that calls a procedure and
Procedure Call Flow provides the necessary parameters.
The caller program places parameter values into registers $a0–$a3
and then uses the jal instruction to jump to the callee procedure. Callee
The callee performs the task, places the return values in $v0–$v1, The procedure that is called by the caller
and then uses jr $ra the instruction to return control to the caller. and executes a task based on the parameters.

Program Counter (PC) Program Counter (PC)


The Program Counter (PC) holds the address of the current instruction being executed. The register holding the address of the
In MIPS, it is often saved as PC + 4 in the return address register ($ra) during a procedure currently executing instruction.
call via jal. This saved address helps in returning control to the correct point in the caller
after the procedure finishes.
Stack
Register Spilling
A memory structure used to save
Spilling registers means saving the caller’s register values to memory, registers, following the LIFO principle.
and the best data structure for managing this process is a stack
(a last-in, first-out (LIFO) queue).
Stack Pointer ($sp)
The stack pointer ($sp) is used to track the current pushed element to
The register pointing to the most recently
the stack. Registers are saved (pushed) onto the stack and later restored allocated address in a stack, indicating where
(popped) from the stack. the next value will be stored or retrieved.
Stack Operations Push
Adding data to the stack
Downward Growth (decreasing the stack pointer).
The stack grows by moving from higher memory addresses to lower ones.
When you push a value, the stack pointer moves downward to the Pop
next lower memory address. When you pop a value, Removing data from the stack
the stack pointer moves upward to the next higher memory address. (increasing the stack pointer).

The stack pointer $sp is assigned to register 29.

Optimization of Register Usage


Since temporary registers ($t0 - $t9) do not need to be preserved
across procedure calls, the callee does not need to save and restore them. 3 First element
Thus, we can optimize the code by removing the save and restore operations
for ($t0 - $t9), leading to a more efficient procedure.
2 Second element
$t8 & $t9 are assigned to registers 24 & 25.

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

# Procedure body (calculating f = (g + h) - (i + j))


add $t0, $a0, $a1 # $t0 = g + h (stored in $a0 and $a1)
add $t1, $a2, $a3 # $t1 = i + j (stored in $a2 and $a3)
sub $s0, $t0, $t1 # f = (g + h) - (i + j), result in $s0

# Return the value of f in $v0


add $v0, $s0, $zero # Return value of f ($v0 = $s0 + 0)

# Restore the saved registers from the stack


lw $s0, 0($sp) # Restore register $s0 for caller
lw $t0, 4($sp) # Restore register $t0 for caller
lw $t1, 8($sp) # Restore register $t1 for caller

# Adjust the stack pointer to remove the saved registers


addi $sp, $sp, 12 # Adjust stack pointer back to original position (delete 3 items)

# Return to the caller


jr $ra # Jump back to the return address in $ra
Page 98
Example 5 (Optimized)
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 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

# Procedure body (calculating f = (g + h) - (i + j))


add $t0, $a0, $a1 # $t0 = g + h (stored in $a0 and $a1)
add $t1, $a2, $a3 # $t1 = i + j (stored in $a2 and $a3)
sub $s0, $t0, $t1 # f = (g + h) - (i + j), result in $s0

# Return the value of f in $v0


add $v0, $s0, $zero # Return value of f ($v0 = $s0 + 0)

# Restore the saved register $s0 from the stack


lw $s0, 0($sp) # Restore register $s0
addi $sp, $sp, 4 # Adjust stack pointer back to original position

# Return to the caller


jr $ra # Jump back to the return address in $ra

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy