21.3 Prologs in Protected Mode

Although exported functions in protected-mode, single-instance applications need to set the DS register, these functions do not require the exported prolog described in the previous section. Instead, they can use code similar to that generated by the _loadds keyword of the Microsoft C Optimizing Compiler (CL) to set the DS register.

The code generated by _loadds copies the data segment selector to the DS register whenever the function is called. Because a selector does not change value when the corresponding segment is moved, there is no need to set the AX register to the appropriate data segment address before calling the function (or to mark the stack frame). The function can, therefore, be called directly rather than through a procedure-instance address. The _loadds code has the following form:

push    bp
mov     bp,sp
push    ds
mov     ax, CONSTANT
mov     ds, ax

Functions that use the _loadds code can be used as callback functions. Because no prolog code is required, the functions do not need to be exported when used in an application. Functions in DLLs can also use the _loadds code. However, the functions must be exported to ensure that other applications can link dynamically to them.

In multiple-instance applications, the Windows prolog is needed only for far functions called by Windows. For these functions, procedure-instance addresses are required. The _loadds code cannot be used in multiple-instance applications. Instead, applications should copy the SS register to the DS register.