Question:
How is a .exe file generated by a C compiler machine independent?
?
2009-06-30 23:44:59 UTC
A confusing doubt... please help
Can someone help me out on this... this is more of a C compiler thing

Whenever we write a C program and compile it , it gets converted into machine language 0's and 1's

A .exe file is generated.

The things is this .exe file can be run on ' any ' machine, now how is this possible?

Dont these machine language format have to be loaded into the instruction register of the CPU(microprocessor), to be executed

what if different CPU's(microprocessors) have different architectures and different instruction sets, how can the same .exe file be used in them all.

How is the compiler generated machine language, generic

I hope you guys understand what i am trying to tell. Based on my limited understanding I tried making it as clear as i possible can :)
Six answers:
zoot661
2009-07-02 00:20:37 UTC
Machine code is targeted for a family of processors that share the same instruction set and register set.



In addition, an executable file is targeted at a single operating system that provides the 'system calls' (e.g. file open / close) with the correct calling sequences for the program (this is achieved by linking against the correct libraries.



Processors come in families, so, for example, you have a range of x86 compatible processors (mainly from Intel and AMD) which will run the same executable or the power PC processors from Motorola.



These processor has a core instruction set supported by the whole family and then extended instructions supported by only some of the processors.



If a compiler want to generate code that is compatible with all processors in the family, it either has to restrict itself to the core instruction set, maybe reducing performance on the enhanced processors or use the illegal instruction exception to handle the non-core instructions where they are not supported, reducing performance on the standard processors.



A compiler can compile to an intermediate language, rather than machine code and then run in an environment like '.net' that executes the intermediate language. This makes the target code processor independent.



Programs can also be run under emulation, so that the emulator program reads the machine code, translates it to an set of machine code instructions supported by the target processor and then runs the program.



Coupled with a suitable operating system support layer (e.g. WABI Windows Application Binary Interface) that provides the correct set of system calls, then a program compiled for an x86 processor running Windows can run on a machine with a PowerPc processor running Linux
Shadow Wolf
2009-07-03 23:02:08 UTC
Actually, If your compiler creates an EXE file, the only systems it will run on may be a Windows system. No other systems would know how to handle the EXE file without some sort of emulator program. Then there are still other problems with which version of Windows or even DOS you are using. An EXE file is not really a working program in the sense that you can simply point an x86 processor at it and run it. There is a lot more that happens behind the scenes. Not only is an EXE file machine dependent, it is also operating system dependent. To make matters worse, an EXE file is portable within the x86 hardware structure in that it can be relocated and is not really executable until it is relocated.



The old DOS COM files were actual machine program files. Even then, the file contained information needed for the operating system to run it. So it would still be very operating system dependent as well as machine dependent. The COM programs were not as relocatable as EXE files but could still be moved around by changing pointers.



The reason C is portable across multiple types of hardware is the language itself was designed to be portable. It was designed so that it would be fairly easy to create a C compiler to bootstrap an new system or machine.



To compile C programs for another machine, you would need a cross compiler. It would produce a program compatible with the other machine and it would likely run once it was transfered over. The other alternative would be to compile the C program using a native compiler on the system for which it was intended. Because of the portable nature of C, the resulting program would then be native to that machine.



The thing with C is if you have 5 machines with different microprocessors, you only need to copy the source code file and compile it for each machine. The result would be 5 different machines that could run the same program and each would appear to be identical in function even though the actual machine code and hardware would be different for each of the machines.



The most common modern language that is closest to being portable or generic across multiple types of hardware is Java. Java compilers create an intermediate code that is similar to machine code but all Java interpreters know how to deal with the Java code. The intermediate code is sort of a Java machine code which allows it to be universal. The underlying Java support programs still need to be compiled for each machine that runs Java. So the basic problem with Java is it isn't really native to any one machine. It is an interpreted language even after it is compiled. You'll find Javascript embedded in many websites and it generally works for any machine if it is Java capable.



I probably made things as clear as mud. The problem is there is a lot more happening than most people know about or even care about. An executable file of any type may have a lot more information in it than just the machine code for the program.



Shadow Wolf
O'Sullivan
2009-07-01 00:00:54 UTC
When you compile a C program, it generates object (OBJ) files which are then linked to produce the executable (EXE) file. The program can run on any computer provided that the computer has a processor that supports the full instruction set used in the program and provided that the computer has an operating system that supplies the APIs the program uses.



The generated machine code is generic only in the sense that most modern Intel and AMD processors support broadly the same instruction set, the only differences being the internal design of the processor.
sheldonlinker
2009-06-30 23:55:58 UTC
Most C compilers generate code for just one type of processor. In some cases, the processor may be backwards compatible. For instance, code written for an Intel 80386 will likely run on an Intel Pentium.



In some rare cases, such as the Mac, the compiler will turn out multiple code modules, so that there will be a code block marked for use on Pentium and one for PowerPC, for instance.
2009-06-30 23:57:19 UTC
The only machines that the machine code will run on is those which have the same architecture. This would be i386 processors, such as intel, AMD, and VIA. x86_64/amd64 processors can also run i386 code.



also these binaries will onyl run on windows. You have to recompile or distrobute source for people to run on Linux or other UNIX/POSIX systems.



This binary wont run on any of the many other architectures, such as ARM or MIPS.
shec0002
2009-07-01 11:44:55 UTC
The mono compiler can create .exe files that can run on any machine with .net


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...