13.2 Determining Whether a CPU Supports the MMX Instruction Set


13.2 Determining Whether a CPU Supports the MMX Instruction Set

While it's almost a given that any modern CPU your software will run on will support the MMX extended instruction set, there may be times when you want to write software that will run on a machine even in the absence of MMX instructions. There are two ways to handle this problem: Either provide two versions of the program, one with MMX support and one without (and let the user choose which program they wish to run), or have the program dynamically determine whether a processor supports the MMX instruction set and skip the MMX instructions if they are not available.

The first situation, providing two different programs, is the easiest solution from a software development point of view. You don't actually create two source files, of course; what you do is use conditional compilation statements (i.e., #if..#else..#endif) to selectively compile MMX or standard instructions depending on the presence of an identifier or value of a boolean constant in your program. See the chapter about macros for more details.

Another solution is to dynamically determine the CPU type at runtime and use program logic to skip over the MMX instructions and execute equivalent standard code if the CPU doesn't support the MMX instruction set. If you're expecting the software to run on an Intel Pentium or later CPU, you can use the cpuid instruction to determine whether the processor supports the MMX instruction set. If MMX instructions are available, the cpuid instruction will return bit 23 as a one in the feature flags return result.

The following code illustrates how to use the cpuid instruction. This example does not demonstrate the entire cpuid sequence, but shows the portion used for detection of MMX technology:

 // For a perfectly general routine, you should determine if this // is a Pentium or later processor. We'll assume at least a Pentium // for now, since most OSes expect a Pentium or better processor.           mov( 1, eax );           // Request for CPUID feature flags.           CPUID();                 // Get the feature flags into EDX.           test( $80_0000, edx );   // Is bit 23 set?           jnz HasMMX; 

This code assumes at least the presence of a Pentium Processor. If your code needs to run on a 486 or 386 processor, you will have to detect that the system is using one of these processors. There is tons of code on the Internet that detects different processors, but most of it will not run under 32-bit OSes because the code typically uses protected (non–user-mode) instructions. Some operating systems provide a system call or environment variable that will specify the CPU. We'll not go into the details here because 99% of the users out there that are running modern operating systems have a CPU that supports the MMX instruction set or, at least, the cpuid instruction.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net