1

I am trying to load a pointer into the %rdi register. The memory address I want is stored 10 bytes past the memory location of $rsp.

How would I write the assembly code for this? This is what I have so far

movq 10(%rsp),%rdi
ret
Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
RR84
  • 111
  • 5
  • 1
    For historical reasons, while IA-32 x86, IA-64 has nothing to do with x64/AMD64/x86-64 or however you want to call it (just don't call it IA-64 because that's Itanium) – harold Nov 06 '16 at 20:33
  • Unless is *is* ia64.. http://stackoverflow.com/a/11893420/3437608 – Cullub Nov 06 '16 at 20:37
  • 2
    @cullub: I bet you 10000$ that this is not an IA-64 question, because x86-64 has `%rsp` and `%rdi` registers, while IA-64 doesn't. – Peter Cordes Nov 06 '16 at 20:38
  • @Peter it looks like the OP just edited, so I guess you're right... – Cullub Nov 06 '16 at 20:39
  • Yes my bad, it's x86-64 instruction set – RR84 Nov 06 '16 at 20:42
  • Can you explain what you really want. The instruction in the post is a LOAD instruction, in loads the *data* from the memory location %rsp+10 into register %rdi. Is that what you need? – dimm Nov 06 '16 at 20:44
  • Avoid the word "store" when you're not talking about writing to memory. You just want RDI = RSP+10, right? BTW, I may have been hasty in closing as a duplicate. The target question is asking about exactly the instruction you need, with the same offset from the stack pointer, but neither the Q nor A exactly explain why it solves your problem. – Peter Cordes Nov 06 '16 at 20:45
  • See also http://stackoverflow.com/questions/21614972/how-does-the-lea-instruction-interact-with-esp/21617964#21617964 – Peter Cordes Nov 06 '16 at 20:46
  • I have a memory address stored in a location starting 10 bytes after the memory location of %rsp. I want to move this memory address to the %rdi register. – RR84 Nov 06 '16 at 21:01
  • 1
    Are you sure you mean 10, and not `0x10`? Otherwise IDK why you're asking a question, because the code in your question does load 8 bytes from RSP+10 into RDI. I thought it was unusual that you wanted an unaligned address, but I figured you wanted to use RDI as a buffer for storing characters or something. It would be even more unusual to load 8 bytes from an unaligned address, so I think you probably do mean `0x10` (i.e. 16) – Peter Cordes Nov 06 '16 at 21:22

1 Answers1

1

Try leaq instruction, it is often used to compute address offset, or a simple multiply–add computation like this:

leaq 4(%rsi,%rdi,2), %rdx    # rdx = 4 + rsi + (rdi << 1)

So what you need would be

leaq 0xa(%rsp),%rdi # assume you need an offset of decimal number 10
ret
D-Y
  • 76
  • 2