x86 Assembler using MASM32 Tutorial 5 - IF Statements



Watch the video or follow the tutorial.



In this, tutorial 5 we will have a little look at the IF statement in x86 assembler. We will create a program that makes decisions based upon a value that we set initially and watch what the code does. 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 "IF Statement", 0
strMessage1 DB "From inside the IF statement", 0
strMessage2 DB "From inside the ELSEIF statement", 0
strMessage3 DB "From inside the ELSE statement", 0


Then scroll down until you see the .code section and underneath the label start: delete the call to ExitProcess and its associated Push and then add the following code:

Mov Eax, 1

_Start_Again :

.IF Eax == 1
     Push MB_OK
     Push Offset strTitle
     Push Offset strMessage1
     Push 0
     Call MessageBox
     Mov Eax, 2
.ELSEIF Eax == 2
     Push MB_OK
     Push Offset strTitle
     Push Offset strMessage2
     Push 0
     Call MessageBox
     Mov Eax, 3
.ELSE
     Push MB_OK
     Push Offset strTitle
     Push Offset strMessage3
     Push 0
     Call MessageBox

     ; Exit app
     Push 0
     Call ExitProcess
.ENDIF

Jmp _Start_Again


Please note that the original call to ExitProcess has been removed and inserted into the .ELSE statement. I will give a quick explanation of how the code works. We set the initial value of Eax to 1 and the code enters the IF statement. It checks the condition of the first part - .IF Eax == 1 and it notices that the value in Eax does indeed equal 1 so it enters this portion of the IF statement. It displays the MessageBox telling us where it is and then sets then the command - Mov Eax, 2 sets the value in the Eax register to 2 and then it drops out of the IF statement entirely. Note: since the first condition did match it does not test the rest of the conditions. After the IF statement it meets the jump command - Jmp _Start_Again and moves back up the code block to the label - _Start_Again :. The code then goes back through the IF statement but this time the Eax register holds the value 2. The test of the first IF condition returns false this time and the code moves onto the next part of the IF statement - .ELSEIF Eax == 2. Since the value in Eax matches the value here the code enters this portion of the IF statement and as before it displays a MessageBox informing us of where it is. Again after we press OK on the MessageBox the value in Eax is changed, this time to 3 and again the code drops out of the If statement and jumps back to the starting position. Upon entering the IF statement again the first two conditions return false and so the code moves onto the third. This statement, simply - .ELSE does not specify a value to check for it simply says "if none of the above conditions are true then..." and so the code enters the third and final section. It displays a MessageBox telling us that it is in the third section and upon pressing OK the code reaches the ExitProcess function call and the program exits.

So, save the project and press F9 and watch it work for yourself

And there you go that is how the above IF statement works. So congratulations you have just taken a step forward on your x86 assembler using MASM32 journey. In the next tutorial we will have a look at the WHILE Loop, so until then, enjoy and below is a copy of the entire code.

; *************************************************************************
; 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 "IF Statement", 0
strMessage1 DB "From inside the IF statement", 0
strMessage2 DB "From inside the ELSEIF statement", 0
strMessage3 DB "From inside the ELSE statement", 0

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



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



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



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

start:

Mov Eax, 1

_Start_Again :

.IF Eax == 1
     Push MB_OK
     Push Offset strTitle
     Push Offset strMessage1
     Push 0
     Call MessageBox
     Mov Eax, 2
.ELSEIF Eax == 2
     Push MB_OK
     Push Offset strTitle
     Push Offset strMessage2
     Push 0
     Call MessageBox
     Mov Eax, 3
.ELSE
     Push MB_OK
     Push Offset strTitle
     Push Offset strMessage3
     Push 0
     Call MessageBox

     ; Exit app
     Push 0
     Call ExitProcess
.ENDIF

Jmp _Start_Again

end start


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