I've noticed that all of the following three ways 'work' to access a variable in asm:
- 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
- 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.
- 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?
