Is there a symbol that represents the current addr

2019-02-08 09:11发布

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.

3条回答
我命由我不由天
2楼-- · 2019-02-08 09:39

Sample usages of .

Minimal explicit example:

x: .long .
mov x, %eax
mov $x, %ebx
/* eax == ebx */

Common combo for string lengths:

s: .ascii "abcd"
s_len = . - s
mov $s_len, %eax
/* eax == 4 */

s_len = syntax explained at Is there a difference between equals sign assignment "x = 1" and ".equ x, 1" or ".set x, 1" in GNU Gas assembly?

Infinite loop:

jmp .
查看更多
女痞
3楼-- · 2019-02-08 09:42

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.


查看更多
别忘想泡老子
4楼-- · 2019-02-08 09:47

Excerpt from info as (GNU Binutils 2.21.90):

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'.
查看更多
登录 后发表回答