How to Create an Infinite Loop in x86 Assembly Language

x86 Assembly programming language


In this article, we show how to create an infinite loop in x86 assembly language.

An infinite loop is a program that runs over and over without ever terminating.

In many situations, an infinite loop is necessary such as when you want a program to run forever, such as a embedded microcontroller. This could be for any device such as a temperature sensor that you always want to read the ambient temperature. This is a system that you want to run and output the temperature continuously without stopping.

So in order to create an infinite loop, there are probably many approaches you can take, but one of the easiest approaches you can do is assign the program you want to loop infinitely to a label and then at the end of the program add a jmp statement that goes back to the label. This runs the program over and over and over, infinitely.

Below we have a program which prints a character to the emulator screen.

Because this program is in an infinite loop, it prints this character over and over again, infinitely.



So we create a label, called printchar

This label can be called anything. It is used in order to be able to go back to the start of the label at the end, which we do with a jmp statement.

We then have the statement, mov ah, 0eh

This statement gives instructions to the BIOS so that it can print out a single character.

We then have the statement, mov al, 'A'

This the character we want to output to the emulator screen.

We then have the statement, int 10h

This gives instructions to the BIOS to print the character to the emulator screen.

Next is the most important line, in regard to creating an infinite loop.

This is the line, jmp printchar

This allows us to jump back to the printchar label, which allows us to go back to the beginning of the program (and run it again).

This repeats over and over again, hence, an infinite loop.

We can see 'A' being printed repeatedly in the emulator screenshot below.



Infinite loop in x86 assembly





So this is how to create an infinite loop in x86 assembly language.

Related Resources



HTML Comment Box is loading comments...