Key Points on Programming in Assembler
Test Instructions/Loops
There are a number of instructions that can be used to test for particular conditions. These perform the same operations as an arithmetic operation but don't change the values in the registers; they just affect the flags.
The cmp instruction effectively subtracts the source from the destination, but doesn't save the resultant value. For instance:
CmpFunction proc mov eax, 100 cmp eax, 100 ; jump if equals je Equals ; not equal mov eax, 2 jmp EndIf Equals: mov eax, 1 EndIf: ret CmpFunction endp
The test instruction performs an and operation on the source and destination operands and sets the flags accordingly without saving the result.
The loop instruction decrements ecx by one, and jumps to a specified location if the result is not zero.
LoopFunction proc xor eax, eax mov ecx, 10 LoopStart: inc eax loop LoopStart ret LoopFunction endp
MASM Macros
There are a huge number of macros available in MASM that are designed to make life easier for the Assembler developer. However, I'm only going to cover a few.
The first is the '.if' statement. It provides the ability to compare two operands (using standard C++ operators like =, >=, <=, and so forth:
IfProc proc
mov eax, 100
mov ecx, 200
.if eax == ecx
; do something
.else
; do something else
.endif
ret
IfProc endp
The second is the .repeat - .until loop. There are various forms of this. .untilcxz decrements ecx by one and continues the loop if the result is not zero. .until zero? continues the loop until the zero flag is set.
LoopProc proc
xor eax, eax
mov ecx, 100
.repeat
inc eax
.untilcxz
ret
LoopProc endp
When performing loops in loops, to free the use of the eax, ebx, and edx registers, the outer loop's ecx value can be pushed and then popped on exiting the inner loop. For example:
LoopInLoopProc proc
xor eax, eax
mov ecx, 100
.repeat
push ecx
mov ecx, 100
.repeat
inc eax
.untilcxz
pop ecx
.untilcxz
ret
LoopInLoopProc endp
Calling Functions from Inside Assembler
You call functions inside of Assembler code by using invoke followed by the name of the function and its parameter list seperated by commas. For example:
Function1 proc dwValue:DWORD add eax, 100 ret Function1 endp MainFunction proc mov eax, 100 invoke Function1, eax ; eax now = 200, i.e. eax += 100 ret MainFunction endp
Note that there is a comma between the function name and the first parameter.
Local Memory
MASM allows you to allocate memory local to functions and label it appropriately. This could potentially be considered as local variables, but if you examine the underlying machine language, you'll see that in fact it's just another shorthand form for accessing memory.
You define memory at the start of the function. If you examine the disassembly, you'll see that what actually happens is that a block of static memory is allocated before the first instruction in the function. The memory has a size that is determined by the basic types in MASM; in other words, BYTE, WORD, or DWORD.
ExampleLocalMemory proc LOCAL dwValue:DWORD ; allocates 4 bytes and labels it 'dwValue' LOCAL wValue:WORD ; allocates 2 bytes and labels it 'wValue' LOCAL bValue:BYTE ; allocates 1 byte and labels it 'bValue' xor eax, eax mov dwValue, eax mov wValue, ax mov bValue, al ret ExampleLocalMemory endp
Optimization
When attempting to write efficient code,it must be considered that not every instruction takes the same time to complete. For instance, mul and div operations are relatively slow compared to the bit-shift operations of shr and shl. A full list of the times of each operation is available in the MASM help files.
When writing efficient code, another consideration is number of instructions involved inside of loops. The fewer the number of instructions, the faster the code will be.
When writing code, memory access is slower than access to registers, so always try to use registers in preference to local function memory.
Also, the efficiency of a jmp depends on the number of bytes to be jumped. This instruction takes offsets of either 8, 16, or 32 bits in size and an 8-bit jump is considerably more efficient than a 32-bit jump. This obviously affects loops: Loops whose instructions size is less than 128 bytes are more efficient than loops containing large blocks of code.
The primary concern is the algorithm itself. The fastest algorithms are always the simplest because they always contain the fewest number of instructions necessary. It is always better to reconsider the algorithm that you are using for a particular task, and if you can trade some accuracy or flexibility in favour of a large improvement in the speed, do so.
There are many, many other considerations when it comes to optimising assembler. Again, the MASM help files are an invaluable source for fine-tuning your code.
If you want to read more about optimising Assembler then I recommend you read Agner Fog's manual at www.agner.org. This will give you an insight into how the processor works, and advise on how to truly optimise your assembler code.
Conclusion
I hope that this set of tutorials has been interesting and informative. It is by no means complete because it is only intended as an introduction. For more information, consult the tutorials and help files that come with MASM.
But, I hope that I have demonstrated the fact that Assembler isn't difficult to write and you should be able to add considerable speed to your applications and perform tasks that you never thought possible in real time.



Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.