3

NASM汇编程序中,可以使用.前缀声明本地标签。

  1. 那么,本地标签(以及所有汇编程序中的标签)的地址是什么?它是相对的还是绝对的,还是取决于用途?

我问是因为有些功能让我感到困惑。这是一个示例代码:

ORG 0x400000 ;origin of address for labels

start:       ;address here should be 0x400000
.....        ;some code here

     .loop   ;local label
     .....   ;some code here
     jmp short .loop ;<------- address is not taken as absolute
     jmp short start

如果我使用一些普通标签(如start)进行引用并将其与lea指令一起使用,则地址将计算为相对于原点的普通绝对地址。

  1. 但是,如果我使用标签并将其与short(如最后一行)一起使用,会发生什么?跳转的偏移量是从绝对地址计算的吗?

我问这一切是因为我的代码中有本地标签(.LNXYZ随机生成),并且我需要制作地址列表(来自该标签),其中包含包含跳转的绝对地址的 4 字节元素。这样的事情可能吗,还是我必须使用普通标签?有什么指令吗?

4

2 回答 2

6

来自NASM 用户手册

3.9 本地标签

NASM 对以句点开头的符号进行特殊处理。以单个句点开头的标签被视为本地标签,这意味着它与之前的非本地标签相关联。因此,例如:

label1  ; some code 

.loop 
    ; some more code 

    jne     .loop 
    ret 

label2  ; some code 

.loop 
    ; some more code 

    jne     .loop 
    ret 

在上面的代码片段中,每条 JNE 指令都跳转到紧接在它之前的行,因为 .loop 的两个定义是分开的,因为每个定义都与前一个非本地标签相关联。

这种本地标签处理形式是从旧的 Amiga 汇编器 DevPac 中借用的;然而,NASM 更进一步,允许从代码的其他部分访问本地标签。这是通过根据之前的非本地标签定义一个本地标签来实现的:上面.loop的第一个定义实际上是定义了一个名为label1.loop的符号,第二个定义了一个名为label2.loop的符号。所以,如果你真的需要,你可以写

label3  ; some more code 
        ; and some more 

        jmp label1.loop
于 2014-05-29T15:34:05.360 回答
2

NASM 中本地标签的地址与标签不是本地标签的地址完全相同。

唯一改变的是标签的名称被附加到前一个非本地标签上。

最小的例子:

outside_label:

    ; This should be not done in practice,
    ; but shows how it works under the hood.
    jmp outside_label.inside_label
    ; This is not reached.
.inside_label:

    ; This is what you should do in practice.
    ; Labels also get appended when used as arguments.
    jmp .inside_label2
    ; This is not reached.
.inside_label2:
于 2015-11-10T18:11:55.583 回答