有没有代表 GNU GAS 程序集中当前地址的符号? [英] Is there a symbol that represents the current address in GNU GAS assembly?

查看:17
本文介绍了有没有代表 GNU GAS 程序集中当前地址的符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道是否有任何特殊的 GAS 语法可以实现与 NASM 示例中相同的功能:

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.

推荐答案

gas 和 NASM 之间有一个有用的比较:http://www.ibm.com/developerworks/linux/library/l-gas-nasm/index.html

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:

清单 2 还引入了位置计数器的概念(第 6 行).NASM 提供了一个特殊的变量($ 和 $$ 变量)来操作位置计数器.在 GAS 中,没有方法可以操作位置计数器,必须使用标签来计算下一个存储位置(数据、指令等).例如,要计算字符串的长度,您可以在 NASM 中使用以下习语:

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

$ 给出了位置计数器的当前值,从这个位置计数器中减去标签的值(所有变量名都是标签)给出了标签声明和当前位置之间存在的字节数.equ 指令用于将变量 STR_SIZE 的值设置为它后面的表达式.GAS 中类似的成语如下所示:

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

结束标签(pstr_end)给出下一个位置地址,减去起始标签地址给出大小.还要注意使用 .set 将变量 STR_SIZE 的值初始化为逗号后面的表达式.也可以使用相应的 .equ.NASM 中 GAS 的 set 指令别无选择.

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.

这篇关于有没有代表 GNU GAS 程序集中当前地址的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆