0% found this document useful (0 votes)
48 views

CC2032 COAL Lab # 08

The document provides information about branch statements in MIPS assembly language including unconditional and conditional branches. It explains different branch instructions like beq, bne, bgez, and others. Code examples are given to illustrate usage of each branch instruction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

CC2032 COAL Lab # 08

The document provides information about branch statements in MIPS assembly language including unconditional and conditional branches. It explains different branch instructions like beq, bne, bgez, and others. Code examples are given to illustrate usage of each branch instruction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

University of Management and Technology

Department of Artificial Intelligence


CC2032L Computer Organization and Assembly Language Lab
Spring 2024
Participant ID: Participant Name:

Date: Total Marks: Obtained Marks:

Lab # 08: Branch Statements in MIPS


1. Learning Objectives
 Provide a clear and concise explanation of branch statements in MIPS32 assembly
language
 Illustrate the usage of each branch statement with well-structured code
2. Branch Statements
Branch statements determine the next instruction to be executed based on the evaluation of a
condition. This is similar to decision-making structures in high-level programming languages
like if-else statements, switch-case statements, and loops.
Branch statements in MIPS32 (Microprocessor without Interlocked Pipeline Stages) assembly
language are used to control the flow of execution in a program. Here are the primary branch
instructions used in MIPS32:
1.1. Unconditional Branch:
 j target: Jump to the instruction at the specified address (label).

1.2. Conditional Branches:


 beq rs, rt, label: Branch to label if the contents of registers rs and rt are equal.

 bne rs, rt, label: Branch to label if the contents of registers rs and rt are not equal.

 bgez rs, label: Branch to label if the contents of register rs are greater than or equal to zero.

 bgtz rs, label: Branch to label if the contents of register rs are greater than zero.

 blez rs, label: Branch to label if the contents of register rs are less than or equal to zero.

 bltz rs, label: Branch to label if the contents of register rs are less than zero.

3. Explanation of Branches with Code Examples


1.3. Unconditional Branch
1.3.1. j label (Jump)
This instruction always jumps to the instruction labeled label.
Example Code:
loop: # Label for the start of the loop
# Code executed in each iteration of the loop
j loop # Unconditionally jump back to the beginning of the loop

1.4. Conditional Branches


1.4.1. beq $rs, $rt, label (Branch on Equal)
This instruction jumps to label if the values in registers $rs and $rt are equal.

addi $t0, $s1, 1 # Add 1 to $s1 and store the result in $t0
beq $t0, $s2, equal_block # Branch if $t0 equals $s2
# Code executed if $t0 is not equal to $s2
j end

equal_block: # Label for code if $t0 equals $s2


# Code executed if $t0 is equal to $s2

end: # Label for the end of the code

1.4.2. bne $rs, $rt, label (Branch on Not Equal):


This instruction jumps to label if the values in registers $rs and $rt are not equal.

addi $t0, $s1, 1 # Add 1 to $s1 and store the result in $t0
bne $t0, $s2, not_equal_block # Branch if $t0 is not equal to $s2
# Code executed if $t0 is equal to $s2
j end

not_equal_block: # Label for code if $t0 is not equal to $s2


# Code executed if $t0 is not equal to $s2

end: # Label for the end of the code

1.4.3. bgez rs, label (Branch on Greater Than or Equal to Zero):


This instruction jumps to the instruction labeled label if the value in register rs is greater than or
equal to zero.
addi $t0, $s1, -5 # Add -5 to $s1 and store the result in $t0
bgez $t0, non_negative # Branch if $t0 is greater than or equal to zero
# Code executed if $t0 is negative

non_negative: # Label for code if $t0 is non-negative (zero or positive)


# Code executed if $t0 is greater than or equal to zero

1.4.4. bgtz rs, label (Branch on Greater Than Zero):


This instruction jumps to the label label only if the value in register rs is strictly greater than zero
(positive).

addi $t0, $s1, 3 # Add 3 to $s1 and store the result in $t0
bgtz $t0, positive # Branch if $t0 is greater than zero
# Code executed if $t0 is zero or negative

positive: # Label for code if $t0 is positive


# Code executed if $t0 is greater than zero

1.4.5. blez rs, label (Branch on Less Than or Equal to Zero):


This instruction jumps to the label label if the value in register rs is less than or equal to zero
(zero or negative).

addi $t0, $s1, 0 # Add 0 to $s1 and store the result in $t0
(essentially copying $s1)
blez $t0, non_positive # Branch if $t0 is less than or equal to zero
# Code executed if $t0 is positive

non_positive: # Label for code if $t0 is non-positive (zero or negative)


# Code executed if $t0 is less than or equal to zero

1.4.6. bltz rs, label (Branch on Less Than Zero):


This instruction jumps to the label label only if the value in register rs is strictly less than zero
(negative).

addi $t0, $s1, -8 # Add -8 to $s1 and store the result in $t0
bltz $t0, negative # Branch if $t0 is less than zero
# Code executed if $t0 is zero or positive

negative: # Label for code if $t0 is negative


# Code executed if $t0 is less than zero

4. Task # 1: Assemble and Run the following program in MARS and


observe the output
.data
value1: .word 10
value2: .word 20

.text
main:
lw $t0, value1 # Load value1 into register $t0
lw $t1, value2 # Load value2 into register $t1
beq $t0, $t1, equal # Branch to 'equal' if $t0 == $t1

# Code if not equal


li $v0, 10 # Load 10 into $v0 (exit syscall)
syscall # Exit

equal:
# Code if equal
li $v0, 4 # Load 4 into $v0 (print string syscall)
la $a0, msg # Load address of msg into $a0
syscall # Print the string
li $v0, 10 # Load 10 into $v0 (exit syscall)
syscall # Exit

.data
msg: .asciiz "Values are equal\n"

Q1. Have you received any output message in the I/O console. If not, why have you not received
any output message?

5. Task # 2: Add a message in the above program to show if the value are
not equal. For example, “The given values are not equal”. Write the
piece of code below.
6. Task # 3: Write a MIPS32 assembly program that loads a value and
checks if the value is greater than or equal to zero. If the value is non-
negative, the program should branch to a label where it prints the
message "Register is non-negative". If the value is negative, the
program should terminate without printing the message.

7. Task # 4: Write a MIPS32 assembly program that prints "Jumped to


target" by unconditionally jumping to a label called target.

You might also like