Moving 64-bit constant to memory in x86 Assembly

2019-06-24 07:05发布

问题:

I'm working with Intel x64 assembly, NASM compiler, trying to move the "0x4000000000000000" constant to memory, which in the ieee 754 standard double should be equal to 2.0.

The code I'm using is:

%define two 0x4000000000000000 

section .text

foo:

push rbp
mov rbp, rsp

mov QWORD [rdi], two

pop rbp
ret

Compiling this throws

warning: signed dword immediate exceeds bounds.

When i print the value in C++ it shows "0" instead of "2".

I've already found a way of getting the right value, which is:

mov r9, 0x4000000000000000
mov [rdi], r9

But i would like to know if there is a way of achieving this without the use of a register.

by the way, im compiling the code with this script:

#!/bin/bash
nasm -f elf64 -g -F dwarf vvp_asm.asm -o vvp_asm.o
g++ -c -m64 -std=c++11 main.cpp -o main.o
g++ -o main -m64 vvp_asm.o main.o

回答1:

There is no instruction

    mov r/m64, imm64

You could use

    mov dword [rdi], 0
    mov dword [rdi+4], 0x40000000

or

    and qword [rdi], 0
    mov byte [rdi+7], 0x40

which is only 8 bytes (if that matters).