University of Management and Technology
Department of INFS
CC222L Computer Organization and Assembly Language Lab
Fall 2025
Lab # 9
Participant ID: Participant Name:
Date: Total Marks: Obtained Marks:
Introduction to MIPS Assembly Language
Objectives
Get familiar with MIPS Jump and Branch instructions
Learn about pseudo instructions in MIPS
Learn how to translate high-level flow control constructs (if-then-else, for loop, while
loop) to MIPS code
MIPS Jump and Branch Instructions
Like all processors, MIPS has instructions for implementing unconditional and conditional jumps.
The MIPS Jump and Branch instructions are shown in Table.
MIPS Jump and Branch Instructions
MIPS uses branch and jump instructions to alter the flow of control. These instructions allow
programs to decide on the next course of action based on conditions or values stored in registers.
Branch Instructions:
beq (branch if equal): Jump to a label if two registers are equal.
beq $t0, $t1, LABEL # High-level: if (a == b) jump to LABEL
bne (branch if not equal): Jump to a label if two registers are not equal.
bne $t0, $t1, LABEL # High-level: if (a != b) jump to LABEL
Example
if (a == b) {
// Execute block of code
result = a + b;
} else {
// Execute alternative block of code
result = a - b;
Mips
.data
newline: .asciiz "\n"
.text
.globl main
main:
# Initialize variables
li $t0, 10 # a = 10
li $t1, 20 # b = 20
# Compare a and b
beq $t0, $t1, Equal # If a == b, jump to Equal
# Else block
sub $t2, $t0, $t1 # result = a - b
j End # Jump to the end
Equal:
add $t2, $t0, $t1 # result = a + b
End:
# Print the result
li $v0, 1 # Load syscall for printing integers
move $a0, $t2 # Move result to $a0
syscall # Print the result
# Print newline
li $v0, 4 # Load syscall for printing strings
la $a0, newline # Load newline string
syscall # Print newline
# Exit program
li $v0, 10 # Load syscall for program exit
syscall
Jump Instruction:
j (jump): Jump unconditionally to a label.
j LABEL # High-level: Go to LABEL unconditionally
Set Instructions:
slt (set less than): Set a register to 1 if one value is less than another, otherwise set it to 0.
slt $t0, $t1, $t2 # High-level: if (b < c) $t0 = 1, else $t0 = 0
slti (set less than immediate): Similar to slt, but compares with an immediate value.
slti $t0, $t1, 10 # High-level: if (b < 10) $t0 = 1, else $t0 = 0
Example:
if (b < c) {
result = 1; // True case
} else {
result = 0; // False case
MIPS
.text # Text section
.globl main # Declare the main function
main:
# Step 1: Initialize b and c
li $t0, 5 # Set b = 5 (Load 5 into register $t0)
li $t1, 10 # Set c = 10 (Load 10 into register $t1)
# Step 2: Compare b and c (b < c)
slt $t2, $t0, $t1 # $t2 = 1 if b < c, else 0
# Step 3: Perform the operation based on the comparison
beq $t2, $zero, false_case # If b >= c, jump to false_case
add $t3, $t0, $t1 # True case: $t3 = b + c
j done # Jump to done to skip false_case
false_case:
add $t3, $t1, $t1 # False case: $t3 = c + c
done:
# Step 4: Output the result
move $a0, $t3 # Move the result to $a0 for printing
li $v0, 1 # Load the system call code for print integer (1)
syscall # Call the syscall to print the result
# Step 5: Exit the program
li $v0, 10 # Load the exit system call code (10)
syscall # Call the syscall to exit the program
Pseudo instructions
simplify assembly programming by combining multiple real instructions into a single, readable
form. Examples include:
bgt (branch if greater than): Translates to slt and bne.
bgt $t0, $t1, LABEL # High-level: if (a > b) jump to LABEL
blt (branch if less than): Translates to slt and bne.
blt $t0, $t1, LABEL # High-level: if (a < b) jump to LABEL
Example:
if (a > b) {
// Execute this block if the condition is true
result = a + b;
} else {
// Execute this block if the condition is false
result = a - b;
}
Mips
Assume that A = 20, B = 10
# Load values into registers
li $t0, 20 # Load A (20) into $t0
li $t1, 10 # Load B (10) into $t1
# Compare A and B (i.e., $t0 > $t1)
bgt $t0, $t1, greater_than # If $t0 > $t1, jump to the 'greater_than' label
# Else block (A <= B) - This will not be executed since A > B
sub $t2, $t0, $t1 # result = A - B (A <= B)
j end_if # Jump to the end of the if-else structure
greater_than:
add $t2, $t0, $t1 # result = A + B (A > B)
end_if:
# Execution continues here after the if-else structure
High level
if (a < b) {
// Do something
} else {
// Do something else
}
Mips
# MIPS program to demonstrate blt (branch if less than) pseudo-instruction
.data
a: .word 5 # Example value of a
b: .word 10 # Example value of b
if_msg: .asciiz "a < b\n"
else_msg: .asciiz "a >= b\n"
.text
.globl main
main:
# Load the values of a and b
lw $t0, a # $t0 = a
lw $t1, b # $t1 = b
# Use blt to check if a < b
blt $t0, $t1, less_than # If a < b, jump to less_than
# Else block (a >= b)
la $a0, else_msg # Load "a >= b" message
li $v0, 4 # Syscall for printing string
syscall
j end_program # Jump to the end of the program
less_than:
# If block (a < b)
la $a0, if_msg # Load "a < b" message
li $v0, 4 # Syscall for printing string
syscall
end_program:
# Exit the program
li $v0, 10 # Syscall for program exit
syscall
Translating High-Level Constructs: If-Else
If-else constructs in high-level languages can be directly mapped to MIPS using conditional
branches and jumps.
Example: High-Level Code
if (a == b) {
x = 1;
} else {
x = 0;}
MIPS Translation
Assume:
a is stored in $t0.
b is stored in $t1.
x is stored in $t2.
beq $t0, $t1, IF_BLOCK # High-level: if (a == b) go to IF_BLOCK
li $t2, 0 # High-level: x = 0 (else case)
j END_IF # High-level: jump to END_IF
IF_BLOCK:
li $t2, 1 # High-level: x = 1 (if case)
END_IF:
# Continue program execution
This approach uses logical comparisons, branches, and jumps to achieve flow control.
High-Level Code for if-else-if-else
if (a > b) {
result = a + b; // Execute if a > b
} else if (a == b) {
result = a * b; // Execute if a == b
} else {
result = a - b; // Execute if a < b
Mips
# MIPS Program for if-elseif-else
# Assume a = 30, b = 20 are stored in $t0 and $t1
# The result will be stored in $t2
.data
newline: .asciiz "\n"
.text
.globl main
main:
# Initialize variables
li $t0, 30 # a = 30
li $t1, 20 # b = 20
# Check if a > b
slt $t3, $t1, $t0 # $t3 = 1 if b < a (i.e., a > b)
bne $t3, $zero, Greater # If $t3 != 0 (a > b), jump to Greater
# Check if a == b
beq $t0, $t1, Equal # If a == b, jump to Equal
# Else block (a < b)
sub $t2, $t0, $t1 # result = a - b
j End # Jump to the end
Greater:
# Then block (a > b)
add $t2, $t0, $t1 # result = a + b
j End # Jump to the end
Equal:
# Else-if block (a == b)
mul $t2, $t0, $t1 # result = a * b
End:
# Print the result
li $v0, 1 # Load syscall for printing integers
move $a0, $t2 # Move result to $a0
syscall # Print the result
# Print newline
li $v0, 4 # Load syscall for printing strings
la $a0, newline # Load newline string
syscall # Print newline
# Exit program
li $v0, 10 # Load syscall for program exit
syscall
Tasks
Understand Basic Branching:
1. Write MIPS code to compare two integers and set a register value based on their
equality. Use the following variables:
a = 5, stored in $t0.
b = 5, stored in $t1.
Result stored in $t2 (1 if equal, 0 otherwise).
Implement If-Else:
2. Convert the following high-level code into MIPS assembly:
if (x > y) {
z = 10;
} else {
z = -10;
Assume:
x is in $t0.
y is in $t1.
z is in $t2.
Check Greater or Equal:
3. Write MIPS code to check if a number a is greater than or equal to a number b. Set
the result in $t3 (1 if true, 0 otherwise). Use the following variables:
a = 8, stored in $t0.
b = 6, stored in $t1.
Check Pass or Fail:
4. Input two integer add them Implement the following logic in MIPS:
if (sum < 33) {
status = "Fail";
} else {
status = "Pass";