Reputation:
Can different source code generate the same executable/binary file?
Is this possible?
Upvotes: 3
Views: 511
Reputation: 1316
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
Upvotes: 4
Reputation: 86186
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.
Upvotes: 6
Reputation: 41858
It depends on how you define similar. It would be possible to play with making tiny changes in Java or C# and have it generate to the same bytecode.
For example, in C, I expect that if I use a preprocessing command for a literal string, or use the literal string directly, that the generated code would be the same but the source was different.
Upvotes: 1