Tips for golfing in x86/x64 machine code I noticed that there's no such question, so here it is:
Do you have general tips for golfing in x86/x64 machine code? If the tip only applies to a certain environment or calling convention, please specify that in your answer.

Please only one tip per answer (see here).
mov-immediate is expensive for constants

This might be obvious, but I'll still put it here. In general it pays off to think about the bit-level representation of a number when you need to initialize a value.

Initializing eax with 0:

b8 00 00 00 00          mov    $0x0,%eax


should be shortened (for performance as well as code-size) to

31 c0                   xor    %eax,%eax


Initializing eax with -1:

b8 ff ff ff ff          mov    $-1,%eax


can be shortened to

31 c0                   xor    %eax,%eax
48                      dec    %eax


or

83 c8 ff                or     $-1,%eax




Or more generally, any 8-bit sign-extended value can be created in 3 bytes with push -12 (2 bytes) / pop %eax (1 byte).  This even works for 64-bit registers with no extra REX prefix; push/pop default operand-size = 64.

6a f3                   pushq  $0xfffffffffffffff3
5d                      pop    %rbp


Or given a known constant in a register, you can create another nearby constant using lea 123(%eax), %ecx (3 bytes).  This is handy if you need a zeroed register and a constant; xor-zero (2 bytes) + lea-disp8 (3 bytes).

31 c0                   xor    %eax,%eax
8d 48 0c                lea    0xc(%eax),%ecx


See also Set all bits in CPU register to 1 efficiently