Question: CCCN 221 Computer Architecture Lab 5 MIPS Flow Control Statement Purpose: The purpose of this Lab. is to familiarize student with Arithmetic, Logical and control

CCCN 221 Computer Architecture

Lab 5 MIPS Flow Control

Statement Purpose: The purpose of this Lab. is to familiarize student with Arithmetic, Logical and control instructions and how to deal with looping. This lab explains how to control the sequence of instructions execution, depending on the value of a registers using the branching instructions and how to repeat some instructions for many times.

Activity Outcomes: Student will learn how to write MIPS programs that make decisions, execute different parts and repeat some instructions depending on registers values. Student will solve different problem that will help them to choose appropriate Logical and branching statement for a given task. There are some exercises, through which they will understand the concept learn in this chapter.

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 following table

Program To Demonstrate Branch-if Equal(beq) and Branch if Not- Equal(bne). .data message: .asciiz "The Numbers are Equal" message2: .asciiz "The Numbers are Not Equal" .text main: addi $t1,$zero ,6 addi $t2,$zero ,15 beq $t1, $t2, numbersEqual bne $t1, $t2, numbersdifferent li $v0,10 syscall

numbersEqual: li $v0,4 la $a0,message syscall j End

numbersdifferent: li $v0,4 la $a0,message2 syscall

End: li $v0,10 syscall

Pseudo Instructions: Pseudo instructions are instructions introduced by an assembler as if they were real instructions. We have seen an example of a pseudo instruction before, which is the li instruction. Pseudo instructions are useful as they facilitate programming in assembly language. For example, the MIPS processor does not have the following useful conditional branch comparison instructions: blt, bltu branch if less than (signed/unsigned) ble, bleu branch if less or equal (signed/unsigned) bgt, bgtu branch if greater than (signed/unsigned) bge, bgeu branch if greater or equal (signed/unsigned)

The reason for not implementing these instructions as part of the MIPS instruction set is that they can be easily implemented based on a set of two instructions.

For example, the instruction blt $s0, $s1, label can be implemented using the following sequence of two instructions: slt $at, $s0, $s1 bne $at, $zero, label

Similarly, the instruction ble $s2, $s3, label can be implemented using the following sequence of two instructions: slt $at, $s3, $s2 beq $at, $zero, label

(2). Program To Demonstrate check if number is less than another number.

.data message1: .asciiz "First number is less than second number " message2: .asciiz "Second number is less than first number "

.text main: addi $t0,$zero ,9 addi $t1,$zero ,1 slt $s0,$t0,$t1 bne $s0, $zero, printMessage

li $v0,4 la $a0,message2 syscall j End

printMessage: li $v0,4 la $a0,message1 syscall

End: li $v0,10 syscall

(3). Program To Demonstrate check if number is greater than another number.

.data message: .asciiz " greater than " mg1:.asciiz "Enter first number: " mg2:.asciiz "Enter second number: "

.text main: li $v0,4 la $a0,mg1 syscall

li $v0,5 syscall move $s0,$v0 #first number in s0

li $v0,4 la $a0,mg2 syscall

li $v0,5 syscall move $s1,$v0 #second number in s1

bgt $s0, $s1,DisplayMsg li $v0,1 move $a0,$s1 #print second number syscall

li $v0,4 la $a0,message syscall

li $v0,1 move $a0,$s0 #print first number syscall

j End

DisplayMsg: li $v0,1 move $a0,$s0 #print first number syscall

li $v0,4 la $a0,message syscall

li $v0,1 move $a0,$s1 #print second number syscall

End:

#End of main li $v0,10 syscall

Exercise 1. Write a program to find the largest of 3 numbers. 2. Write a program to find the smallest of 3 numbers.

Ex1- Solution: .data message: .asciiz " the greatest number " mg1:.asciiz "Enter first number: " mg2:.asciiz "Enter second number: " mg3:.asciiz "Enter third number: "

.text main: li $v0,4 la $a0,mg1 syscall

li $v0,5 syscall move $s0,$v0 #first number in s0

li $v0,4 la $a0,mg2 syscall

li $v0,5 syscall move $s1,$v0 #second number in s1

li $v0,4 la $a0,mg3 syscall

li $v0,5 syscall move $s2,$v0 #third number in s2

bgt $s0, $s1,L1 bgt $s1, $s2, NumberTwo j NumberThree

L1: bgt $s0,$s2, NumberOne j NumberThree

NumberOne: # number one is the greatest li $v0,1 move $a0, $s0 syscall j End

NumberTwo: # number two is the greatest li $v0,1 move $a0, $s1 syscall j End

NumberThree: li $v0,1 # number three is the greatest move $a0, $s2 syscall j End

End:

li $v0,4 la $a0,message syscall

#End of main li $v0,10 syscall

*PLEASE SOLVE THE EXERCISE BY MIPS ASSEMBLY

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!