OK, as far as I understand there are many different types of ISA for different CPUs, such as x86, MIPS, etc. When the compiler compile the source code (C++/JAVA) in different ISA environments, the assembly code generated will be different as well. And then, I wonder whether the binary machine code generated will be different as well or just the same???? My wild guess is that the binary code generated will be the same, otherwise how can different CPU run the same piece of exe file on different computers?? I wonder whether my hypothesis is correct or not. Thanks for clarification!
The binary code generated for different CPUs is different. One example C program:
int a;
int b(int c)
{
return a+c;
}
Compiled with the GCC compiler for 32-bit x86:
8b 44 24 04 // mov 0x4(%esp), %eax
03 05 xx xx xx xx // add a, %eax
c3 // ret
The same program compiled with the GCC compiler for MIPS:
3c 1c xx xx 27 9c xx xx // la gp, GOT - actually 2 instructions!
8f 82 xx xx 00 00 00 00 // la v0, a - actually 2 instructions!
8c 42 00 00 // lw v0,0(v0)
03 e0 00 08 // jr ra
00 44 10 21 // addu v0, v0, a0
Of course the .exe file for x86 will not work on MIPS and vice versa!
An exception to this are Java and .NET exectuables:
These types executables contain a special type of code which can neither be executed by x86 nor by MIPS, ARM or PPC. However this code is designed in a way that it can easiely be translated into machine-dependent code.
When you execute a .NET .exe file on Windows the first time the CPU-independent code in the .exe file will be translated into x86-only (or ARM-only or whatever) code and the "translated" variant will be stored in a temporary directory. The file from the temporary directory will actually be executed.
Java works similar but a bit more complicated...