tags:

views:

79

answers:

3

Is this possible?

+1  A: 

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.

James Black
+6  A: 

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.
Nosredna
+4  A: 

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
maykeye
good example, maykeye
Nosredna