Is there a symbol that represents the current address in GNU GAS assembly?
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 thatas
is assembling into. Thus, the expressionmelvin: .long .
definesmelvin
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
This is a GAS version of the same idiom used in NASM (len equ $ - symbol
) to get the assembler to calculate the length of something for you.
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.