Question

Is there anyway to specify what processor we are targeting with the code assembled using NASM ?

For example, lets say I want to target only 8086, and hence using this instruction should be invalid:

mov eax, cr0

since eax/cr0 are not there in 8086.

Or is it that NASM works for the "x86 family", and then it is on the programmer to make sure the code is written for the correct processor and is run on the ones where the instructions used are supported ?

Also, what if an instruction is supported in one processor but not in other .I guess as long as we talk about the same processor family this wouldn't happen, but what if such a situation does come up at some point ? Does NASM have functionality to support that ?

I have done some searching about all this, but couldn't find anything that answers this directly. Sorry if these questions sound stupid/rudimentary.

Était-ce utile?

La solution

You can use the CPU directive. From the fine manual:

The CPU directive restricts assembly to those instructions which are
available on the specified CPU.

Options are:

CPU 8086 Assemble only 8086 instruction set
CPU 186 Assemble instructions up to the 80186 instruction set
CPU 286 Assemble instructions up to the 286 instruction set
CPU 386 Assemble instructions up to the 386 instruction set
CPU 486 486 instruction set
CPU 586 Pentium instruction set
CPU PENTIUM Same as 586
CPU 686 P6 instruction set
CPU PPRO Same as 686
CPU P2 Same as 686
CPU P3 Pentium III (Katmai) instruction sets
CPU KATMAI Same as P3
CPU P4 Pentium 4 (Willamette) instruction set
CPU WILLAMETTE Same as P4
CPU PRESCOTT Prescott instruction set
CPU X64 x86-64 (x64/AMD64/Intel 64) instruction set
CPU IA64 IA64 CPU (in x86 mode) instruction set

Autres conseils

You can set the number of bits like this:

[BITS 16]

[BITS 32]

[BITS 64]

Other than that, you just must not use instructions that aren't supported on your target processor. For example, you can use AVX instructions on a Pentium D; if you do, your program will crash.

Edit: Actually, I'm wrong. Check Paul R's answer.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top