0

I am practicing the various data accessing modes in assembly. Here is what I have so far:

# Practicing the various Data Accessing methods

.globl main
main:

    # Immediate mode
    mov $7,     %rbx

    # Register addressing mode
    mov %rbx,   %rax

    # Direct addressing mode?

    ret

I understand the concept of moving the value at a memory address into a register, such as:

mov 0x0102030405060708,     %eax

But my issue is, how is someone supposed to know where the (virtual) memory addresses are before the program compiles? Or if, for example, that memory address is even accessible (or that I might get a seg fault).

So, what might be a practical example that works for using Direct addressing mode?

David542
  • 104,438
  • 178
  • 489
  • 842
  • Use labels/symbols to let the linker fill in the right absolute or RIP-relative memory address, like `mov my_global_var(%rip), %eax`. You can see the details by disassembling. – Peter Cordes Aug 22 '20 at 05:34
  • @PeterCordes which of those answers do you think would be a duplicate where I could find information on how to better learn direct addressing? – David542 Aug 22 '20 at 05:58
  • [What is the function of a "data label" in an x86 assembler?](https://stackoverflow.com/q/44742872) is probably the best bet; it explains how a label can turn into an absolute memory address after assembling + linking (i.e. it uses the `[disp32]` addressing mode, [which you can call "direct" if you want](https://stackoverflow.com/questions/46257018/do-terms-like-direct-indirect-addressing-mode-actual-exists-in-the-intel-x86-man)). Look at any 32-bit mode compiler output that uses a static/global variable. (x86-64 code will always use PC-relative in such cases, which is basically equivalent.) – Peter Cordes Aug 22 '20 at 06:00
  • @PeterCordes oh, your answer here is fantastic: https://stackoverflow.com/questions/46257018/do-terms-like-direct-indirect-addressing-mode-actual-exists-in-the-intel-x86-man. I think that would be the main duplicate. – David542 Aug 22 '20 at 06:01
  • 1
    Glad you found it helpful, but I didn't think it was a duplicate. It didn't have any examples (which is why I linked some of the current dupes), and it didn't explain how the linker fills in relocation info in the final machine code in executables for absolute direct addressing modes, according to where it put variables. IMO that's the real answer to how you'd ever use direct addressing in real code in a linked program running under an OS. – Peter Cordes Aug 22 '20 at 06:04
  • 1
    [What is the purpose of the assembler and symbol table? What is at a symbol's address?](https://stackoverflow.com/q/57984652) looks good, too, using MIPS (which uses region-absolute encoding for its `j` jump instruction) – Peter Cordes Aug 22 '20 at 06:06

0 Answers0