在NASM
汇编程序中,可以使用.
前缀声明本地标签。
我问,因为有些功能让我很困惑。这是一个示例代码:
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
指令一起使用,则地址计算为相对于原点的正常绝对地址。
short
一起使用(如最后一行),那么会发生什么?跳转的偏移是从绝对地址计算的吗?我问这一切是因为我的代码中有本地标签(.LNXYZ
,随机生成),我需要制作地址列表(来自那些标签),这些地址将包含4字节元素,其中包含跳转的绝对地址。这样的事情可能,或者我必须使用普通标签?对它有任何指示吗?
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
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: