Is there a symbol that represents the current address in GNU GAS assembly?

orustammanapov picture orustammanapov · Jan 24, 2012 · Viewed 7.2k times · Source

I am curious to know is there any special GAS syntax to achieve the same like in NASM example:

SECTION .data       

    msg:    db "Hello World",10,0  ; the 0-terminated string.
    len:    equ $-msg              ; "$" means current address.

Especially I'm interested in the symbol $ representing the current address.

Answer

user811773 picture user811773 · Jul 7, 2012

Excerpt from info as (GNU Binutils 2.21.90), or online in the GAS manual: https://sourceware.org/binutils/docs/as/Dot.html

5.4 The Special Dot Symbol

The special symbol . refers to the current address that as is assembling into. Thus, the expression melvin: .long . defines melvin to contain its own address.

Assigning a value to . is treated the same as a .org directive. Thus, the expression .=.+4 is the same as saying .space 4.

msg:    .ascii "Hello World!\n"       # not zero-terminated, use .asciz for that
msglen = . - msg                      # A .equ directive would be equivalent

is GAS version of the same idiom used in NASM (len equ $ - symbol) idiomatic way to get the assembler to calculate the length of something for you.