x86 machine code, 3 bytes
C3 D8 F7
The above bytes of code define a function that is a no-op: it simply returns control to the caller. That function is followed by two garbage bytes that will not be executed, since they come after a return—they are in "no man's land". In assembler mnemonics:
ret ; C3
fdiv st(0), st(7) ; D8 F7
Okay, so now some troll comes by and reverses the order of the bytes:
F7 D8 C3
These bytes now define a function that takes an integer argument in the EAX
register, negates it, and returns control to the caller. In assembler mnemonics:
neg eax ; F7 D8
ret ; C3
So…that was simple. :-)
Note that we can make the "negation" instruction be anything we want, since it is never executed in the "forward" orientation and only executed in the "reversed" orientation. Therefore, we can follow the same pattern to do arbitrarily more complicated stuff. For example, here we take an integer argument in a different register (say, EDI
, to follow the System V calling convention commonly used on *nix systems), negate it, and return it in the conventional EAX
register:
C3 ret
D8 F7 fdiv st(0), st(7) ; \ garbage bytes that
F8 clc ; | never get executed,
89 .byte 0x89 ; / so nobody cares
↓ ↓
89 F8 mov eax, edi
F7 D8 neg eax
C3 ret