The "Go" tools
The GoAsm manual
by Jeremy Gordon -
This file is intended for those interested in 32 bit assembler programming, in particular for Windows.
The "registers" are small areas of memory contained within the processor itself. The processor is designed to manipulate the data in the registers and transfer data into and out of them at great speed. As processors have become more powerful so have the number and size of the registers they contain, but all processors from the 386 upwards have eight "general" 32 bit registers for general programming use. These have been given names by Intel based on their traditional uses, shortened for all purposes to EAX, EBX, ECX, EDX, EDI, ESI, EBP, and ESP.
There is also an EIP register which holds the current instruction being executed at any one time. You cannot read from or write to the EIP register so it is not available for general programming use. However its value will be visible to you when you are single-step debugging.
EBP (base pointer) and ESP (stack pointer) have certain restrictions in use which I shall come to later.
Each of the registers can be accessed using 32 bits. That is
to say if you use a 32 bit assembler instruction, then all 32 bits of
the register will be changed, read or written to (depending on the
instruction). Each of the registers can also be accessed using only
the first 16 bits if this is allowed by the instruction concerned. This is
achieved by using the register names AX, BX, CX, DX, DI, SI, BP and SP.
The following illustrations show this:-
The first 16 bits of the EAX register (that is bits 0 to 15) can be accessed as the AX register. The first 8 bits of the EAX register (that is bits 0 to 7) can be accessed as the AL register. The second 8 bits of the EAX register (that is bits 8 to 15) can be accessed as the AH register. EBX, ECX, and EDX work in the same way.
The first 16 bits of the EDI register can be accessed as the DI register. ESI, EBP and ESP work in the same way, but note there is no further sub division of 16 bit register in the case of these particular registers.
To make their programs run as fast as possible assembler programmers
use the registers as much as they can. For example, if a routine needs
to access a particular 32 bit number frequently, the routine will run
much faster if the number is kept in a register rather than in memory.
Keeping numbers on the stack (using the PUSH and POP instructions) is
also very quick but not as quick as using the registers.
In 32 bit programming there is no advantage in using the 16 bit registers instead of the 32 bit ones. The main reason they have been kept is to ensure downwards compatibility with 16-bit code. However there are some instructions which only use the 16 bit registers.
In 32 bit programming if you use the 16 bit registers you will usually add one more byte to the instruction size and your programs will probably run more slowly because the processor is designed to expect 32 bit transactions.
The 8 bit registers are useful in 32 bit assembler programming if they can be used to hold data which otherwise would have to be held in memory. For example you might use the two registers AL and AH if the numbers are small enough. Remember however that you could not then use EAX. This is because AL and AH are part of EAX.
There are also eight 80 bit registers which are used for floating point and for executing the 64 bit MMX instructions.
Later processors also have eight 128 bit XMM registers which can make full use of the SSE and SSE2 instructions.
Traditional use of registersSome instructions use particular registers to perform certain tasks; some instructions are faster if certain registers are used; in early processors not all the registers could do everything as they do now. These three facts, together with established traditional use of registers by assembler programmers over the years, have established expectations about how the registers should be used. If you stick to these it will help with the readability of your code.
Copyright © Jeremy Gordon 2002-2003