background image

Vol. 1 9-9

PROGRAMMING WITH INTEL® MMX™ TECHNOLOGY

Example 9-1 illustrates how to use the CPUID instruction to detect the MMX technology. This example does not 
represent the entire CPUID sequence, but shows the portion used for detection of MMX technology.

Example 9-1.  Partial Routine for Detecting MMX Technology with the CPUID Instruction

...

; identify existence of CPUID instruction

...

; identify Intel processor

mov

EAX, 1

; request for feature flags

CPUID

; 0FH, 0A2H CPUID instruction

test

EDX, 00800000H

; Is IA MMX technology bit (Bit 23 of EDX) set?

jnz

; MMX_Technology_Found

9.6.2 

Transitions Between x87 FPU and MMX Code

Applications can contain both x87 FPU floating-point and MMX instructions. However, because the MMX registers 
are aliased to the x87 FPU register stack, care must be taken when making transitions between x87 FPU instruc-
tions and MMX instructions to prevent incoherent or unexpected results.
When an MMX instruction (other than the EMMS instruction) is executed, the processor changes the x87 FPU state 
as follows:

The TOS (top of stack) value of the x87 FPU status word is set to 0.

The entire x87 FPU tag word is set to the valid state (00B in all tag fields). 

When an MMX instruction writes to an MMX register, it writes ones (11B) to the exponent part of the corre-
sponding floating-point register (bits 64 through 79).

The net result of these actions is that any x87 FPU state prior to the execution of the MMX instruction is essentially 
lost.
When an x87 FPU instruction is executed, the processor assumes that the current state of the x87 FPU register 
stack and control registers is valid and executes the instruction without any preparatory modifications to the x87 
FPU state.
If the application contains both x87 FPU floating-point and MMX instructions, the following guidelines are recom-
mended:

When transitioning between x87 FPU and MMX code, save the state of any x87 FPU data or control registers 
that need to be preserved for future use. The FSAVE and FXSAVE instructions save the entire x87 FPU state.

When transitioning between MMX and x87 FPU code, do the following:
— Save any data in the MMX registers that needs to be preserved for future use. FSAVE and FXSAVE also save 

the state of MMX registers.

— Execute the EMMS instruction to clear the MMX state from the x87 data and control registers.

The following sections describe the use of the EMMS instruction and give additional guidelines for mixing x87 FPU 
and MMX code.

9.6.3 

Using the EMMS Instruction

As described in Section 9.6.2, “Transitions Between x87 FPU and MMX Code,” when an MMX instruction executes, 
the x87 FPU tag word is marked valid (00B). In this state, the execution of subsequent x87 FPU instructions may 
produce unexpected x87 FPU floating-point exceptions and/or incorrect results because the x87 FPU register stack 
appears to contain valid data. The EMMS instruction is provided to prevent this problem by marking the x87 FPU 
tag word as empty.
The EMMS instruction should be used in each of the following cases: 

When an application using the x87 FPU instructions calls an MMX technology library/DLL (use the EMMS 
instruction at the end of the MMX code).