title: NASM: copying a pointer from a register to a buffer in .data
body: <p>I am new to asm. I am trying to copy a pointer from a register to a <code>.data</code> variable using NASM, on linux 64-bit.</p>
  
  <p>Concider this program:</p>
  
  <pre><code>    section .data
  ptr: dq 0
  
      section .text
      global _start
  
  _start:
      mov [ptr], rsp
  
      mov rax, 60
      mov rdi, 0
      syscall
  </code></pre>
  
  <p>Here I try to copy the current stack pointer to <code>ptr</code>. <code>ptr</code> is declared as a quadword. Neither <code>nasm</code> nor the linker complains, but when debugging the program with gdb, I can see that both addresses are different:</p>
  
  <pre><code>gdb ./test.s
  +(gdb) break _start
  Breakpoint 1 at 0x4000b0
  +(gdb) run
  Starting program: test
  Breakpoint 1, 0x00000000004000b0 in _start ()
  +(gdb) nexti
  0x00000000004000b8 in _start ()
  +(gdb) info registers
  ...
  rsp            0x7fffffffe460   0x7fffffffe460
  ...
  +(gdb) x ptr
  0xffffffffffffe460: Cannot access memory at address 0xffffffffffffe460
  </code></pre>
  
  <p>From what I understand, <code>mov</code> should copy all 64 bits from <code>rsp</code> to <code>[ptr]</code>, but it seems that the most significant <code>0</code>s are not copied and/or that there is some kind of sign extension, as if only the least significant bits were copied.</p>