x86 Assembler using MASM32 Tutorial 6 - While Loops



Watch the video or follow the tutorial.



In this, tutorial 6 we will have a little look at While Loops. We will create a program that undertakes a particular action based upon a condition that is tested at the start of each action or loop. If the condition tests false, in this case, the loop continues, if it tests true then the code moves on beyond the while loop. So, first of all open Visual MASM, if you haven't installed Visual MASM yet you can learn how to do it here. Once installed open it and learn create the default code template needed for all my tutorials here. This default code will be the starting point for most of our tutorials including this one. So, once all this is done you should be looking at the code window as seen below:


Scroll down until you see the .data section and add the following code:

strTitle DB "While Loop", 0
strStart DB "Before Loop", 0



Then scroll down until you see the .code section and underneath the label start: and add the following code:

; Signals the start of the code and that we haven't entered the While Loop yet

Push MB_OK
Push Offset strTitle
Push Offset strStart
Push 0
Call MessageBox

Mov Eax, 0 ; Reset value in Eax to zero

Push MB_OK
Push Offset strTitle
Push ustr$(Eax)
Push 0
Call MessageBox

Mov Eax, 0 ; Reset value in Eax to zero

.WHILE Eax != 10000000

     Inc Eax

.ENDW

; After the While Loop, signals the end of the code
Push MB_OK
Push Offset strTitle
Push ustr$(Eax)
Push 0
Call MessageBox

; Exit App
Push 0
Call ExitProcess

To give a little explanation of how the code works exactly, initially it displays a MessageBox telling us that it has started and has not yet entered the While Loop and then resets the value stored in the Eax register to zero. Then it displays a second MessageBox showing us the current value in Eax which should be zero and will be the starting point for the While Loop. The next step is to reset the value in Eax to zero again since the last MessageBox call will have changed it and then we enter the While Loop itself. The While loop tests the condition: "Is the value in Eax 10,000,000 if it is not then it runs the loop during which the value in Eax is incremented by 1 and the loop returns to the beginning and the condition is tested again. While the value in Eax is less than 10,000,000 the loop runs and once the value reaches 10,000,000 the loop ends and the code moves onto the next statement.

There is another MessageBox call which which displays the current value in Eax which will be 10,000,000 and once you press OK the app reaches the ExitProcess function and the program ends.

And that is the basic While Loop in x86 Assembler using MASM32. In the next tutorial we will have a look at structs and how they are created and stored in memory, so until then, enjoy.

Below is the entire code from today's tutorial.

; *************************************************************************
; 32-bit Windows Program
; *************************************************************************

.686                                      ; Enable 80686+ instruction set
.model flat, stdcall                ; Flat, 32-bit memory model (not used in 64-bit)
option casemap: none         ; Case sensitive syntax

; *************************************************************************
; MASM32 proto types for Win32 functions and structures
; *************************************************************************
include c:\masm32\include\windows.inc
include c:\masm32\include\user32.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\masm32rt.inc     ; for using ustr$() and such like

; *************************************************************************
; MASM32 object libraries
; *************************************************************************
includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib

; *************************************************************************
; Our data section.
; *************************************************************************
.data

strTitle DB "While Loop", 0
strStart DB "Before Loop", 0

; *************************************************************************
; Our unintialised data section.
; *************************************************************************
.data?



; *************************************************************************
; Our constant section.
; *************************************************************************
.const



; *************************************************************************
; Macros
; *************************************************************************



; *************************************************************************
; Our executable assembly code starts here in the .code section
; *************************************************************************
.code

start:

; Signals the start of the code and that we haven't entered the While Loop yet
Push MB_OK
Push Offset strTitle
Push Offset strStart
Push 0
Call MessageBox

Mov Eax, 0 ; Reset value in Eax to zero

Push MB_OK
Push Offset strTitle
Push ustr$(Eax)
Push 0
Call MessageBox

Mov Eax, 0 ; Reset value in Eax to zero

.WHILE Eax != 10000000

     Inc Eax

.ENDW

; After the While Loop, signals the end of the code
Push MB_OK
Push Offset strTitle
Push ustr$(Eax)
Push 0
Call MessageBox

; Exit App
Push 0
Call ExitProcess

end start


Link to a text file with complete source code and more comments - here.