MIPS Loops: For, While, Do while

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:

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

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.mips loops for while dowhile
mips loops for while dowhile

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:

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.

Nauman Rehmat
 

Nauman Rehmat is the owner of Assembly Language Tuts.He is interested in Programming and SEO. He is also pursuing a degree in Computer Software Engineering from Comsats.