Saving registers

When writing assembler code in 32-bit Windows, there is a convention that governs the use of registers so programmers can interact with the Windows API functions in a predictable and completely reliable way.

However, the registers available with an 80x86 processor are a very limited resource, and they are used by every application (process) running and also by the operating system itself.  Therefore, a reliable method of using registers is very important to the process of writing reliable assembler code.

An 80x86 processor has eight general-purpose integral registers, EAX, EBX, ECX, EDX, ESI, EDI, ESP, and EBP.  Of these, ESP and EBP are almost exclusively used to manage the entry and exit to a procedure, so there are effectively just six general-purpose registers available for application level programming.

Following on, the Windows convention splits the remaining registers so that 3 can be freely modified (EAX, ECX, and EDX) within the Sub/Function/Method/Property that uses them, while the other 3 must be preserved (EBX, ESI, and EDI) by the procedure.  For the sake of this discussion, we'll refer to these two sets as scratch and volatile registers respectively.

In summary, PowerBASIC automatically preserves EBX, ESI, and EDI at the procedure level, but the programmer is responsible for preserving both the scratch and volatile registers within the procedure.

"Preserving the registers" does not necessarily mean that you must push all the registers on the stack, though that is the usual way of ensuring their safety.  Simple routines might not modify any of the registers; in which case, you may not need to take any precautions.  We use may because it's best to avoid making assumptions, especially with assembler programming.  It is better to be safe than sorry.  When in doubt, preserve (save and restore) all of the registers.

 

See Also

The Inline Assembler

Registers

Saving Registers at the procedure level

Saving the FPU registers

Tricks in preserving registers