Search code examples
assemblycompiler-constructionlinkercpu-architecture

Assembly code on different O.S.s but same processor is same or different?


When a C/C++ code is compiled, it produces assembly code. If I have Windows and Linux dual boot (i.e. exact same processor) and I write a program in C++ on both Windows and Linux then

the assembly code after compiling(before linking) will be same for different?

I mean what makes difference between executables is linker, write? But I am not talking about executables... I am talking about assembly code before linking.

If both assembly codes are different then why and how are they different?

Libraries are pre-compiled so that may be the difference I think(If not please correct me). But what if I don't use libraries?


Solution

  • Different OSes use different executable file formats.

    They also offer their functionality through different APIs (fopen() is going to eventually call CreateFileA() using int 0x2E or sysenter on 32-bit Windows and open() using int 0x80 on 32-bit Linux) that function internally in different ways. So there will be different code inside your program's fopen() for Windows and Linux.

    Different compilers may use different default calling conventions or the same compiler can use different calling conventions for different target OSes. Different optimization and debugging options also affect code generation even on the same compiler.

    Finally, different compilers can also generate vastly different code even when everything else is the same even if you don't use any libraries (which would be a pointless thing to do in general). That's simply because they don't use exactly the same algorithms and aren't written by the same people and there's nobody to mandate that all compilers behave identically.

    What's the point of your question, anyway?