Shortest x86-64 opcode for rax = 1?

What will be the shortest x86-64 opcode to set raxto 1?

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

You can change or read any other registers, but you cannot assume that any particular value would be on any of them from the previous instructions.

+4
source share
2 answers

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

+5
source

Under any known preconditions, there are more efficient (in terms of speed) tricks than the 3-byte push8 / pop rax solution.

There mov eax, 1are many advantages to speed , because it does not have any input dependencies, and this is just one instruction. An unscheduled performance can begin with it (and everything that depends on it), without waiting for other things. (See Agner Fog Handbook and the tag wiki).

, , 32- , REX OP. ( , xor rax,rax Silvermont. xor- 32- , eax r10d, rax r10.)


,

lea   eax, [rcx+1]    ; 3 bytes: opcode + ModRM + disp8

disp8 -128 +127.


eax, and eax, 1 3 .


32- inc eax , inc/dec opupodes REX AMD64. , xor eax,eax/inc eax 4 x86-64, 3 32- . , 1 mov eax,1 , LEA AND , , push/pop.

+2

Source: https://habr.com/ru/post/1616727/


All Articles