11
Here,
EBP is saved on the stack in the beginning of the function and restored in the end. The
code relies on
EBP being unchanged after the call to AnotherFunction. EAX is also used,
but doesn't have to be saved.
It is more efficient to use registers for transferring parameters to a function and for receiving
the return value than to store these values on the stack. Some calling conventions use
certain registers for parameter transfer, but the rules for which registers to use are compiler-
specific in 16-bit and 32-bit systems. In 64-bit systems, the use of registers for parameter
transfer is standardized. All systems use registers for return values if the returned object fits
into the registers that are assigned for this purpose. See the next chapter for details.
Segment registers
You only have to care about segment registers in 16-bit mode. DS has to be saved and
restored if you change it.
ES can be changed freely. In DOS programs, ES can have any
value. In 16-bit Windows,
ES can only have values that are valid segment descriptors. It is
not allowed to use
ES for other purposes.
In 32-bit and 64-bit mode, it is not allowed to change any segment register, not even
temporarily. CS, DS, ES and SS all point to the flat segment group. FS is used for a thread
environment block in Windows and for thread specific data in Linux.
GS is used for a
processor control region in 64-bit Windows. It is unused but reserved in 32-bit Windows. It is
probably unused in 32-bit Linux.
Arithmetic flags
The rules for the arithmetic flags (zero flag, carry flag, etc.) are the same as for scratch
registers. These flags need not be saved. Some programming languages (not C++) use the
carry flag for Boolean returns.
Direction flag
The rules for the direction flag is the same in all systems. The direction flag is cleared by
default. If the direction flag is set, then it must be cleared again before any call or return.
Some compilers and subroutine libraries rely on the direction flag always being clear
(Microsoft, Watcom, Digital Mars) while other systems use the double-safe strategy of
always leaving the direction flag cleared, but not relying on receiving it cleared (Borland,
Gnu).
There is a slight possibility that some programmers may have ignored the rule for the
direction flag. Therefore, it may be wise to use the double-safe strategy and clear the
direction flag before using it if the code will be linked together with modules from unreliable
sources.
Interrupt flag
It is not allowed to turn off the interrupt flag in programs running in multi-user systems
because this would make it possible to steal unlimited amounts of CPU time from other
processes. It may be possible to turn off the interrupt flag in console mode programs
running under Windows 98 and earlier operating systems without network. But since
programs written for old operating systems are likely to be run under newer operating
systems, it is reasonable to say that it is never possible to turn off the interrupt flag in
application programs.
Floating point registers
The floating point registers ST(0)-ST(7) need not be saved. The register stack must be
emptied before any call or return, except for registers used for return values. The 64-bit
Microsoft compiler does not use
ST(0)-ST(7).