The MASM Forum

General => The Campus => Topic started by: vamman on December 02, 2013, 01:48:28 PM

Title: use relative addressing mode in ml64/masm
Post by: vamman on December 02, 2013, 01:48:28 PM
I have the following error in MS VS 2010 using MASM.

Error   1   error A2006: undefined symbol : rip

ASM code:
lea rdx, [rip]

Is there something I need to enable in VS? According to this MSDN link this should be possible? http://msdn.microsoft.com/en-us/library/windows/hardware/ff561499%28v=vs.85%29.aspx

What's the work around?
Title: Re: use relative addressing mode in ml64/masm
Post by: dedndave on December 02, 2013, 02:03:09 PM
you can try this
        call    label0
label0: pop     rdx
        add     rdx,4


the instruction pointer is not normally used as a general register
but, the CALL instruction PUSH's the address of the following instruction on the stack (return address)
you can then POP it off the stack
notice that EDX will have the address of label0
POP EDX is a one byte opcode
ADD RDX,4 is 3 bytes (i believe)
Title: Re: use relative addressing mode in ml64/masm
Post by: qWord on December 02, 2013, 03:04:03 PM
AFAIK MASM doesn't allow direct usage of RIP. However, RIP-relative addressing is the default modus for x64 and memory operators are corresponding encoded (RIP+displacement). You can see that by duplicating an instruction that access memory:
.const
    foo DWORD ?
.code
    mov eax,foo  ; 8b055f200000 <- size = 6 byte
    mov eax,foo  ; 8b0559200000 <- 59+6=5f (.const section lies above .code) 

Title: Re: use relative addressing mode in ml64/masm
Post by: dedndave on December 02, 2013, 03:22:03 PM
duh.....    :redface:

now that i look at it, you don't need to use CALL
        mov     edx,label0        ;for code labels, the "offset" operator is implied
label0: