background image

Vol. 3B 21-1

CHAPTER 21

MIXING 16-BIT AND 32-BIT CODE

Program modules written to run on IA-32 processors can be either 16-bit modules or 32-bit modules. Table 21-1 
shows the characteristic of 16-bit and 32-bit modules.

The IA-32 processors function most efficiently when executing 32-bit program modules. They can, however, also 
execute 16-bit program modules, in any of the following ways:

In real-address mode.

In virtual-8086 mode.

System management mode (SMM).

As a protected-mode task, when the code, data, and stack segments for the task are all configured as a 16-bit 
segments.

By integrating 16-bit and 32-bit segments into a single protected-mode task.

By integrating 16-bit operations into 32-bit code segments.

Real-address mode, virtual-8086 mode, and SMM are native 16-bit modes. A legacy program assembled and/or 
compiled to run on an Intel 8086 or Intel 286 processor should run in real-address mode or virtual-8086 mode 
without modification. Sixteen-bit program modules can also be written to run in real-address mode for handling 
system initialization or to run in SMM for handling system management functions. See Chapter 20, “8086 Emula-
tion,”
 for detailed information on real-address mode and virtual-8086 mode; see Chapter 34, “System Manage-
ment Mode,” for in
formation on SMM.
This chapter describes how to integrate 16-bit program modules with 32-bit program modules when operating in 
protected mode and how to mix 16-bit and 32-bit code within 32-bit code segments.

21.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.

Table 21-1.  Characteristics of 16-Bit and 32-Bit Program Modules

Characteristic

16-Bit Program Modules

32-Bit Program Modules

Segment Size

0 to 64 KBytes

0 to 4 GBytes

Operand Sizes

8 bits and 16 bits

8 bits and 32 bits

Pointer Offset Size (Address Size)

16 bits

32 bits

Stack Pointer Size

16 Bits

32 Bits

Control Transfers Allowed to Code Segments 

of This Size

16 Bits

32 Bits