How cpu architecture affects compiling programs?

Can i compile a program on an intel cpu and run it on an AMD processor?
Why can't simple console programs that have been compiled on windows be run on linux; after all they are mere executables.

If anyone could clear all this out for me in detail or refer me to a good site, i would be grateful.

Thanks,
Arun Jusrut.
If your code is understood by the CPU, the CPU can run it. You could be a little clearer on what you mean by AMD and Intel, but I'll assume here that you mean Intel 386 compatible instructions/modes by both manufactures. If that's the case, yes; as long as you don't use processor specific instructions.

They are mere executables making function calls in runtime libraries. Different environments have different libraries or even executable format.
Intel and AMD CPUs are different implementations of the same architecture: x86 (assuming we're talking about the 32-bit versions). It's not impossible, however, to compile a program that will only run on certain CPUs. Different CPUs support different instruction sets. For example, Core 2 supports SSSE3 and Pentium 4 doesn't. If a program was to unconditionally take advantage of it, that program would fail to run on a Pentium 4.

Windows uses the PE executable format, while Linux uses the ELF executable format. This can be compared to having the same text in RTF and HTML. The text is the same, but it's written in two mutually incompatible formats.
Once we get that out of the way, there are other problems. At the lowest level, every program needs to make calls to the operating system to, for example, allocate memory, free memory, open and lock files, etc. Linux's API and the WinAPI are of course incompatible.

It's possible to write a program that will run on any CPU of a given architecture regardless of operating system, but that would be an operating system in itself and would have to be loaded by the BIOS.
But how can windows install on both intel and AMD CPUs while linux has got different versions for each architecture.
E.g,
i take a copy of windows and install on i386, then same copy of windows i install on AMD64, it would work

But linux, e.g, DEBIAN, has 2 versions for each architecture(though both x86). for example debian lenny is available for download for a number of archs, including i386 and AMD64. I suppose if i try to install the i386 version on an AMD it would not work, unlike the windows example above.

Hope i was clear enough on what my confusion is.

Thanks again for the replies :).
But how can windows install on both intel and AMD CPUs while linux has got different versions for each architecture.
E.g,
i take a copy of windows and install on i386, then same copy of windows i install on AMD64, it would work
You can run an i386 (more often called x86) Linux on a 64-bit processor (be that AMD64 or EM64T) because those processors are also able to run in 32-bit mode. What you can't do is run an x86-64 OS on an x86 processor. 64-bit processors are backwards compatible with 32-bit programs, but 32-bit processors are not forward compatible with 64-bit programs (otherwise they would be 64-bit processors).
Notice that this rule applies for all OSs: Windows, Linux, BSD, etc. Windows Vista, for example, also has x86 and x86-64 versions. There's also a Windows XP-64 which isn't very popular.

It's often, though not always, preferable to install the version closest to the CPU. Advantages of 64-bit mode include: more addressable memory (up to 16 EiB), bigger integers. Possible disadvantages include: buggy device drivers, possible binary incompatibility with existing 32-bit software.
Last edited on
Thanks.
Topic archived. No new replies allowed.