Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can different source code generate the same executable/binary file?

Tags:

compilation

Can different source code generate the same executable/binary file?

Is this possible?


2 Answers

Yes. Compilers can make a lot of optimizations, and different source code can map to the same object code. Here are a couple trivial examples. Note that these are language-dependent.

  • You can express an integer in decimal, hex, octal, or binary--the result in the object code will be the same.
  • In many languages, the variable names do not appear in the executable, and you can change the names of variables without affecting the executable.
like image 144
Nosredna Avatar answered May 05 '26 10:05

Nosredna


Yes. For example

int nabs1(int a){ 
  return -abs(a); 
}

int nabs2(int a){ 
  return(a<0) ? a : -a;
}

gcc -O6 generates the same code:

 _nabs1:
pushl   %ebp
movl    %esp, %ebp
movl    8(%ebp), %edx
popl    %ebp
movl    %edx, %eax
sarl    $31, %eax
xorl    %eax, %edx
subl    %edx, %eax
ret
.p2align 4,,15
.globl _nabs2
.def    _nabs2; .scl    2;  .type   32; .endef
_nabs2:
pushl   %ebp
movl    %esp, %ebp
movl    8(%ebp), %edx
popl    %ebp
movl    %edx, %eax
sarl    $31, %eax
xorl    %eax, %edx
subl    %edx, %eax
ret
like image 45
maykeye Avatar answered May 05 '26 10:05

maykeye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!