18-2 Vol. 3
MIXING 16-BIT AND 32-BIT CODE
18.1 DEFINING 16-BIT AND 32-BIT PROGRAM MODULES
The following IA-32 architecture mechanisms are used to distinguish between and
support 16-bit and 32-bit segments and operations:
• The D (default operand and address size) flag in code-segment descriptors.
• The B (default stack size) flag in stack-segment descriptors.
• 16-bit and 32-bit call gates, interrupt gates, and trap gates.
• Operand-size and address-size instruction prefixes.
• 16-bit and 32-bit general-purpose registers.
The D flag in a code-segment descriptor determines the default operand-size and
address-size for the instructions of a code segment. (In real-address mode and
virtual-8086 mode, which do not use segment descriptors, the default is 16 bits.) A
code segment with its D flag set is a 32-bit segment; a code segment with its D flag
clear is a 16-bit segment.
The B flag in the stack-segment descriptor specifies the size of stack pointer (the
32-bit ESP register or the 16-bit SP register) used by the processor for implicit stack
references. The B flag for all data descriptors also controls upper address range for
expand down segments.
When transferring program control to another code segment through a call gate,
interrupt gate, or trap gate, the operand size used during the transfer is determined
by the type of gate used (16-bit or 32-bit), (not by the D-flag or prefix of the transfer
instruction). The gate type determines how return information is saved on the stack
(or stacks).
For most efficient and trouble-free operation of the processor, 32-bit programs or
tasks should have the D flag in the code-segment descriptor and the B flag in the
stack-segment descriptor set, and 16-bit programs or tasks should have these flags
clear. Program control transfers from 16-bit segments to 32-bit segments (and vice
versa) are handled most efficiently through call, interrupt, or trap gates.
Instruction prefixes can be used to override the default operand size and address size
of a code segment. These prefixes can be used in real-address mode as well as in
protected mode and virtual-8086 mode. An operand-size or address-size prefix only
changes the size for the duration of the instruction.
18.2 MIXING 16-BIT AND 32-BIT OPERATIONS WITHIN A
CODE SEGMENT
The following two instruction prefixes allow mixing of 32-bit and 16-bit operations
within one segment:
• The operand-size prefix (66H)
• The address-size prefix (67H)