assemblyx86-64micro-optimizationcode-size

Shortest Intel x86-64 opcode for rax=1?


What would be the shortest Intel x86-64 opcode for setting rax to 1?

I tried xor rax,rax and inc al (in NASM syntax); which gives the 5-byte opcode 48 31 c0 fe c0. Would it be possible to achieve the same result in 4 bytes?

You can modify or read any other registers, but cannot assume that a specific value would be on any one of them from previous instructions.


Solution

  • Since there is a byte immediate encoding for push and a one-byte pop for registers, this can be done in three bytes: 6a 01 58, or push $1 / pop %rax.