1

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?

1
  • 2
    System calls will also be different. If it was a trivial application that didn't do any IO, you could probably run the same binary on both architectures, but as soon as you get beyond the trivial case it'll fail. Feb 24, 2013 at 10:13

2 Answers 2

4

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?

4
  • I asked this question since I always find only basic things when searching about tool-chains and not the "everything" and eventually I end up with many confusions! If you know any resources about "everything" about c++ tool chains please comment it! Thanks!!
    – Sam
    Feb 24, 2013 at 10:52
  • Everything? I bet even the compiler developers don't know everything about their compiler. Take bugs, for example. :) Study compiler/tool documentation for details. Feb 24, 2013 at 11:00
  • By everything I meant everything that a good programmer should know! :)
    – Sam
    Feb 24, 2013 at 11:02
  • 1
    Begin with the programming language. Know the language standard. Know algorithms. Work on real commercial projects with other people to know the wide spectrum of important things, including less technical ones (e.g. communication other soft skills). Know how to test and debug. Take it from there. Feb 24, 2013 at 11:09
1

The assembly code will be the same, as long as you don't have to use a system call - which means that you cannot read input, nor perform output, not even end the program - nothing.

So, except for a totally useless program that cannot do input and output and that you will have to kill manually you can't create a binary that goes through the same code on several operating systems, doing the same stuff everywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.