0

I've noticed that all of the following three ways 'work' to access a variable in asm:

  1. name it literally:

    .globl main
    number1:  .byte 7
    number2:  .byte 14
    main:
        mov number1,     %ebx
        mov number2,     %ecx
        mov $0,     %eax
    multiply_step:
        add %ebx,   %eax
        dec %ecx
        jnz multiply_step
        ret
    
  2. name it with a $ prepended:

    number1:  .byte 7         # use number1 = 7 or .equ to make this block work
    number2:  .byte 14
    main:
        mov $number1,     %ebx   # editor's note: this gets the address
        mov $number2,     %ecx   # not the value.
    
  3. name it relative to rip:

    number1:  .byte 7
    number2:  .byte 14
    main:
        mov number1(%rip),     %ebx
        mov number2(%rip),     %ecx
    

All three give me the same product, 98, when you multiply them like in this question.

What's the difference between these three ways to reference a variable, and is one of the ways preferred over the others?

David542
  • 104,438
  • 178
  • 489
  • 842
  • None of these are calls, they're all just loads. If you mean "naming" or "referencing", as in "what do I call you?", then say "three ways of referencing a variable". Or e.g. "reference it relative to RIP". – Peter Cordes Aug 10 '20 at 04:13
  • Way #2 gives you their addresses. Also, none of these will give you `98` in a register or anywhere else, and there's no `ret` on main so these aren't complete programs, and you aren't setting `main`'s return value (EAX) anyway. The first and last give you `0x????0e07` in EBX, and `0x??????07` in ECX, where `??` comes from whatever bytes follow these 2 single bytes. (You're doing 4 byte loads). – Peter Cordes Aug 10 '20 at 04:15
  • 3. is preferred when you want the value: [Why is the address of static variables relative to the Instruction Pointer?](https://stackoverflow.com/q/40329260) / [Why are global variables in x86-64 accessed relative to the instruction pointer?](https://stackoverflow.com/q/56262889). 2. is preferred ([in a Linux non-PIE executable](https://stackoverflow.com/questions/43367427/32-bit-absolute-addresses-no-longer-allowed-in-x86-64-linux) only) when you want the address, otherwise `lea foo(%rip), %rcx`. Look at compiler output for functions that return the value or address of a global var. – Peter Cordes Aug 10 '20 at 04:20
  • @PeterCordes I updated the code -- I left out the `globl main...ret` part to just focos on referencing the vars. – David542 Aug 10 '20 at 18:37

0 Answers0