Search code examples
assemblygnugnu-assemblermemory-address

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


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.


Solution

  • There is a useful comparison between gas and NASM here: http://www.ibm.com/developerworks/linux/library/l-gas-nasm/index.html

    See in particular this part, which I think addresses your question:


    Listing 2 also introduces the concept of a location counter (line 6). NASM provides a special variable (the $ and $$ variables) to manipulate the location counter. In GAS, there is no method to manipulate the location counter and you have to use labels to calculate the next storage location (data, instruction, etc.). For example, to calculate the length of a string, you would use the following idiom in NASM:

    prompt_str db 'Enter your name: '
    STR_SIZE equ $ - prompt_str     ; $ is the location counter
    

    The $ gives the current value of the location counter, and subtracting the value of the label (all variable names are labels) from this location counter gives the number of bytes present between the declaration of the label and the current location. The equ directive is used to set the value of the variable STR_SIZE to the expression following it. A similar idiom in GAS looks like this:

    prompt_str:
         .ascii "Enter Your Name: "
    
    pstr_end:
         .set STR_SIZE, pstr_end - prompt_str
    

    The end label (pstr_end) gives the next location address, and subtracting the starting label address gives the size. Also note the use of .set to initialize the value of the variable STR_SIZE to the expression following the comma. A corresponding .equ can also be used. There is no alternative to GAS's set directive in NASM.