10.0 MASM
If you are not using MASM, you can skip this chapter and try to convert all the examples, or read it, and try to convince yourself using MASM. Of course, this is your choice. But MASM really makes assembly language easier.
10.1 Conditions and Circulating Structure
Masm has some pseudo high-order syntax to easily create conditions and loop structures:
. Indif.repeat, .until.while, .ndw, .break.continue
IF
If you have experience in using programming languages (you should have), you may have seen some structures like IF / ELSE:
.IF EAX == 1; EAX is equal to 1.Elseif Eax = 3; EAX is equal to 3.ELSE; EAX is neither 1 nor 3.enDIF
This structure is very useful. You don't need to stir with a pair of jumps, as long as one .IF statement (not forgetting .IF and .ELSE). Nested IF is permissible:
.IF EAX == 1. IF ECX! = 2; EAX = 1 and ECX is not 2.enDif.Endif
But you can be more concise:
(EAX == 1 && ECX! = 2); EAX = 1 and ECX is not 2.enDIF
These are the operators you can use:
==
equal
! =
not equal to
>
more than the
<
Be less than
> =
greater or equal to
<=
Less than or equal
&
Bit test
!
Logical
&&&&
Logic
||
Logic or
Carry?
Carry bit set
OVERFLOW?
Overflow bit set
Parity?
Parity Bit Set
SIGN?
Sign bit set
ZERO?
Zero Bit Set
Repeat
This statement executes an instruction to know the condition is true:
.Repeat; code here .until Eax == 1
This code repeatedly executes code between REPEATs and UnTIL, knows EAX = 1.
While
While is the reversal of the REPEAT statement. It performs code blocks when the conditions are true:
.While eax == 1; code here .Endw
You can use the .break statement to jump out of the loop
.While edx == 1inc eax.if eax == 7.break.endif.Endw
If EAX == 7, the While loop will stop
The Continue instruction allows the REPEAT or WHILE to skip the code block below and re-execute the loop.
10.2 Invoke
This is better than TASM and NASM's biggest advantages. Invoke simplifies the process and call of Call.
General format:
Push Parameter3push Parameter2push Parameter1call Procedure
INVOKE format:
Invoke Procedure, Parameter1, Parameter2, Parameter3
The compilation code is the same, but the Invoke format is simpler and more reliable. Use Invoke to a process, you have to define prototype this:
Proto Stdcall Testproc: DWORD,: DWORD,: DWORD
Declare the process called TestProc to do three DWORD sizes. Now if you do this ...
Invoke Testproc, 1, 2, 3, 4
... MASM will give you a TestProc process require three parameters instead of four errors. MASM will also do a type check. It checks if the parameter is the correct type (ie, size) in an Invoke statement, you can use Addr instead of Offset. This will make the address are correct when assembled.
The process is defined:
Testproc Proto Stdcall: DWORD,: DWORD,: DWORD
.code
TestProc Proc Pram1: DWORD, PARAM2: DWORD, PARAM3: DWORD
RettestProc ENDP
This creates a process called TestProc with three parameters. Prototype is used to call the process.
Testproc Proto Stdcall: DWORD,: DWORD,: DWORD
.code
TestProc Proc Pram1: DWORD, PARAM2: DWORD, PARAM3: DWORD
Mov ECX, Param1mov Edx, Param2Mov Eax, Param3Add Edx, Eaxmul Eax, ECX
RettestProc ENDP
Now, the process has made calculations, (param1, param2, param3) = param1 * (param2 param3). Result (return value) is stored in EAX, local variables are defined:
TestProc Proc Param1: DWORD, PARAM2: DWORD, PARAM3: DWORDLOCAL VAR1: DWORDLOCAL VAR2: BYTE
Mov ECX, Param1mov Var2, Clmov Edx, Param2mov Eax, Param3mov Var1, EaxAdd Edx, EXMUL EAX, ECXMOV EBX, VAR1.IF BL == VAR2XOR EAX, EAX.Endif
RettestProc ENDP
You can't use these variables outside of the process. They are stored in the stack and move out when the process returns.
10.3 macro
Don't explain the macro now. Maybe in the later tutorial, but now they are not important to us.