How is relative RIP addressing compared to mov reg, imm64?

It is known that x86-64 instructions do not support 64-bit immediate values ​​(except mov). Therefore, when transferring code from 32 to 64 bits, this instruction:

    cmp rax, addr32

cannot be replaced by the following:

    cmp rax, addr64

Under these conditions, I consider two alternatives: (a) using a zero register to load a constant, or (b) using relative addressing. These two approaches are as follows:

    mov r11, addr64 ; scratch register
    cmp rax, r11

ptr64: dq addr64

...
     cmp rax, [rel ptr64]    ; encoded as cmp rax, [rip+offset]

, ( ). () , () ( i-). , , () 10% , (). - ?


true:  dq 0xFFFF0000FFFF0000
false: dq 0xAAAABBBBAAAABBBB

main:
    or rax, 1  ; rax is odd and constant "true" is even
    mov rcx, 0x1
    shl rcx, 30
branch:
    mov r11, 0xFFFF0000FFFF0000 ; not present in (b)
    cmp rax, r11                ; vs cmp rax, [rel true]
    je next
    add rax, 2
    loop branch

next:
    mov rax, 0
    ret
+1
1

, , () ~ 10% , ()

, , AMD Bulldozer Ryzen, loop. loop , , , . 7 , 5 .

mov r64, imm64 - - 2 Intel uop. (. - Sandybridge Agar Fog microarch pdf) , imm64 m64 x86-64?, .

, , 1 . , AMD ( 1-/1 2 loop), mov 10%. , 3 4 uops 2 , , loop 2 .

Intel, loop - 7 , 5 , / 4 , . loop , . ( Skylake LSD , ). , mov r64,imm64 uop uop .


, , (2 - , cmp). , , . .


:

, RIP-relative lea 64- .
lea rax, [rel addr64].
, . (BTW, default rel. [abs fs:0], .

, , , (), , 32 . ( 2GiB, , ). . 32- x86-64 Linux?, gcc ; -pie . , , Linux-, 64- . , , lea .

32 , cmp r64, imm32 cmp r32, imm32 PIC.

64- , mov r64, imm64 . cmp , mov . x86-64 , ( ) .

+3

All Articles