Can an 80386 run modern x86 executables?

116 Views Asked by At

If I compile a 32 bit executable to run on my modern i5, can I run it on an 80386? Assume I'm using the same operating system for both CPUs.

1

There are 1 best solutions below

0
Peter Cordes On

No, most modern compilers default to assuming PPro new instructions like cmov and fcomi. Also 486 instructions like bswap. Depending on your compiler config, it might even default to using SSE2 instructions in 32-bit mode. (Pentium 4 / Pentium-M / AMD K8 from the early 2000s.)

If you want to override that default, e.g. for GCC or clang, you can use -m32 -march=i386
For example:

int foo(int a, int b)
{
    return (a > 3) ? 4 : b;
}

GCC -O3 -m32 (with a standard / default config on Godolt) compiles to

foo(int, int):
        cmp     DWORD PTR [esp+4], 3
        mov     eax, 4
        cmovle  eax, DWORD PTR [esp+8]
        ret

vs. with -march=i386 -O3 -Wall -m32

foo(int, int):
        mov     eax, DWORD PTR [esp+8]
        cmp     DWORD PTR [esp+4], 3
        jg      .L4
        ret
.L4:
        mov     eax, 4
        ret