Home x86 Assembly <<< Previous Next >>>

Looping:

A loop in programming is something like this:

Maximum=100

counter=1

START:

print "hello world"

if counter < Maximum then

{ counter= counter+1;

go to START}

END

This algorithm does the following things:

1.) It sets a maximum value of 100

2.) It prints "Hello World" and counts the number of times it has printed it. This counting is done by increasing the value of counter each time the string is printed.

3.) It then checks to see if counter is equal to Maximum if it is not equal, the value of counter is increased again till the condition is met.

4.) The program stops after counter=Maximum

Let us now put this to work in our 'Hello World' assembly program.

We make some changes to it (shown in red)

title Hello World

.dosseg
.model small
.stack 100h

.data
message db 'Hello, World!',13,10,'$'

.code
main proc
mov ax,@data
mov ds,ax

mov cx, 10 ; This sets up a counter in the computer's memory, whose maximum value is 10

loop1: ; This is the entry point of the loop

mov ah,9
mov dx,offset message ; We are printing the 'Hello World String' here
int 21h

loop loop1: ; This tells the computer to go back to loop1 10 times

mov ax,4C00h
int 21h
main endp
end main

Compile the program as you did earlier, and you'd see 'Hello World' printed across the screen 10 times.

Home x86 Assembly <<< Previous Next >>>