MIPS loops are similar to loops in other programming languages like c++ or java. In this section, we will explain loops in MIPS along with examples. Before explaining types of loops we will have a short discussion on loops, what is the purpose of loops and basic logic behind loops.
MIPS Loops Basic Structure
Basically, loops are used for repetition of some instruction. we have different loops for different types of situation. In some situation, we know in advance the total number of repetitions or sometimes we don’t know the number of repetitions.
Loops in MIPS assembly have the same logic as loops in any high-level language, but the syntax is different. In assembly language, we deal with registers directly, so it makes it a little bit difficult to learn.
In MIPS we can use three types of loops for, while and do-while.
For Loops in MIPS
For loop is used in a condition when you know the number of iterations in advance. In for loop you have to initialize a counter register with the total iterations you want the loop to execute.
GENERAL SYNTAX:
For(condition){
set of statements
iteration++
}
Below is a simple program to illustrate the functionality of for loop in MIPS assembly.
PROGRAM:
.data msg1: .asciiz"A program in MIPS to test for loop. " .text li $t0,10 la $a0,msg1 for_loop: BLT $t0,1,Exit li $v0,4 syscall Sub $t0,$t0,1 j for_loop Exit: li $v0,10 syscall
EXPLANATION:
In the above program of for loop, we have initialized a string message, and we want to print that message ten times, for that we took a counter register and initialized it with value 10, we used a BLT(branch less than) condition. Unless the counter value is not less than one the loop will continuously be printing the message. You can also use BGT, BLTG with slight differences.
While Loop in MIPS
While loop is used in a condition when we don’t know the number of iterations. It may be a case in which a user enters values repeatedly, and the loop doesn’t end unless the user enters a particular value. Basic stucture of a while loop is given below:
while(condition){
set of statements
}
Below we will discuss an example program of the while loop in which will help you understand it completely!
PROGRAM
.data msg1: .asciiz "Enter a number: " .text li $v0,4 la $a0,msg1 while: syscall li $v0,5 syscall bgt $v0,10,print li $v0,10 syscall j while print: li $v0,1 move $a0,$v0 syscall j while
EXPLANATION:
To test while loop, we asked the user to enter a number, the user will be asked to enter the number again and again until he enters a number less than 10.
When the user enters a value, it is checked through an instruction whether it is greater or smaller than 10. If the value is greater than ten then the control is transferred to the print label. If the value is smaller than ten, then the program terminates.
Do-While Loop in MIPS
Basic structure of do-while loop is given below:
do{
set of statements
}
while(condition){
set of statements
}
Basic logic behind a do-while loop is that in do-while loop, a set of statements are executed at least one time if the given condition is true.
Below is the example program in MIPS to understand do-while loop.
PROGRAM:
.data msg1: .asciiz "This is the example of a do-while loop in MIPS assembly. " msg2: .asciiz "Enter a number: " .text li $v0,4 la $a0,msg2 syscall li $v0,5 do: syscall li $v0,4 la $a0,msg1 syscall while: bgt $v0,1,exit j do exit: li $v0,10 syscall
EXPLANATION:
The above program will print the statement in .data segment atleast one time whether condition in while labe is true or false.
If the condition in while label is true then program will contine to print the statement unles the condition become false.