-2

Me and my friend used GCC to convert the same .c file to .s for our assignment. But we both got different assembly language code.

Me OS :- Windows 10 using GCC 4.7.2

Friend OS:- Ubuntu using GCC 4.7.2

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 2
    Probably passing different compiler options, e.g. optimization level. – Igor Tandetnik Jul 24 '16 at 16:49
  • 1
    The default options can differ. You can check them via `g++ -dumpspecs >the_default_specs.txt`. – Cheers and hth. - Alf Jul 24 '16 at 16:52
  • Not necessarily in the options you gave since the defaults for optimization may be different. Also, there are some things that it inlines differently depending on how the GCC was built. – MAP Jul 24 '16 at 16:53
  • 3
    Different platforms == different codegen. Same compiler on different platforms == different implementation details == different codegen. (Possibly) different compiler options used during build == different codegen. – Jesper Juhl Jul 24 '16 at 16:55
  • Could you please post both the `asm` programs? – babon Jul 24 '16 at 16:59
  • Why is this tagged as C++? – Jesper Juhl Jul 24 '16 at 17:06
  • 1
    posting the compiled code will help to explain with proper reasons why the code is different – NishanthSpShetty Jul 24 '16 at 17:38
  • 1
    There is no reason to expect them to be the same. Same compiler same machine different build options, different compiler options, etc will produce vastly different code. Start to vary the platform, the compiler version, build options, etc and the output from the same source will continue to vary. Basically there is no reason to expect them to match. Same computer same options, same compiler, same day (sometimes you need the same minute or second if there is a time stamp) same source and you can start to expect consistency. – old_timer Jul 24 '16 at 18:23
  • not seen it in a while but same compiler, same computer, same source, same options, ignoring the time stamp would produce a different result because the compiler (well linker) was padding the binary with whatever was in ram before it started to generate the binary in ram before writing to disk. So you would see variations in uninitialized structures or padding, and rare if ever to get two binaries to match. Not a common practice if at all anymore. – old_timer Jul 24 '16 at 18:28
  • Then there's the whole "Windows" versus "Linux" thing.. – David Hoelzer Jul 24 '16 at 23:28

1 Answers1

4

The program you have written is compiled to run on two different OS. Both has its own set of syscalls and calling conventions. The compiled code will have different way of passing parameter, registers used for passing the values around and selected optimisation level.

NishanthSpShetty
  • 661
  • 5
  • 14