Download Yasm User Manual

Transcript
2.4. IMMEDIATE OPERANDS
2.3.2 RIP Relative Addressing
In 64-bit mode, a new form of effective addressing is available to make it easier to write position-independent
code. Any memory reference may be made RIP relative (RIP is the instruction pointer register, which contains
the address of the location immediately following the current instruction).
In NASM syntax, there are two ways to specify RIP-relative addressing:
mov dword [ r i p +10] , 1
stores the value 1 ten bytes after the end of the instruction. 10 can also be a symbolic constant, and will
be treated the same way. On the other hand,
mov dword [ symb wrt r i p ] , 1
stores the value 1 into the address of symbol symb. This is distinctly different than the behavior of:
mov dword [ symb+r i p ] , 1
which takes the address of the end of the instruction, adds the address of symb to it, then stores the value
1 there. If symb is a variable, this will not store the value 1 into the symb variable!
Yasm also supports the following syntax for RIP-relative addressing. The REL keyword makes it produce
RIP-relative addresses, while the ABS keyword makes it produce non-RIP-relative addresses:
mov [ r e l sym ] , rax
mov [ abs sym ] , rax
; RIP - r e l a t i v e
; not RIP - r e l a t i v e
The behavior of mov [sym], rax depends on a mode set by the DEFAULT directive (see Раздел 4.2), as
follows. The default mode at Yasm start-up is always ABS, and in REL mode, use of registers, a FS or GS
segment override, or an explicit ABS override will result in a non-RIP-relative effective address.
default rel
mov
mov
mov
mov
mov
mov
[ sym ] , rbx
[ abs sym ] , rbx
[ rbx +1] , rbx
[ f s : sym ] , rbx
[ ds : sym ] , rbx
[ r e l sym ] , rbx
;
;
;
;
;
;
RIP - r e l a t i v e
not RIP - r e l a t i v e ( e x p l i c i t o v e r r i d e )
not RIP - r e l a t i v e ( r e g i s t e r u s e )
not RIP - r e l a t i v e ( f s o r g s u s e )
RIP - r e l a t i v e ( segment , but not f s o r g s )
RIP - r e l a t i v e ( redundant o v e r r i d e )
d e f a u l t abs
mov
mov
mov
mov
mov
mov
[ sym ] , rbx
[ abs sym ] , rbx
[ rbx +1] , rbx
[ f s : sym ] , rbx
[ ds : sym ] , rbx
[ r e l sym ] , rbx
;
;
;
;
;
;
not RIP - r e l a t i v e
not RIP - r e l a t i v e
not RIP - r e l a t i v e
not RIP - r e l a t i v e
not RIP - r e l a t i v e
RIP - r e l a t i v e ( e x p l i c i t o v e r r i d e )
2.4 Immediate Operands
Immediate operands in NASM may be 8 bits, 16 bits, 32 bits, and even 64 bits in size. The immediate size
can be directly specified through the use of the BYTE, WORD, or DWORD keywords, respectively.
64 bit immediate operands are limited to direct 64-bit register move instructions in BITS 64 mode. For
all other instructions in 64-bit mode, immediate values remain 32 bits; their value is sign-extended into the
upper 32 bits of the target register prior to being used. The exception is the mov instruction, which can take
a 64-bit immediate when the destination is a 64-bit register.
All unsized immediate values in BITS 64 in Yasm default to 32-bit size for consistency. In order to get a
64-bit immediate with a label, specify the size explicitly with the QWORD keyword. For ease of use, Yasm
will also try to recognize 64-bit values and change the size to 64 bits automatically for these cases.
Examples in NASM syntax:
21