在标签下使用时与在单行中使用时如何处理 $ 标记?

How is the $ token treated when used under a label vs in a single line?

NASM 手册在第 3.5 节的 $ 令牌上包含以下内容:

$ evaluates to the assembly position at the beginning of the line containing the expression

在 3.1 节的前面有一条关于源代码行的注释:

NASM uses backslash (\) as the line continuation character; if a line ends with backslash, the next line is considered to be a part of the backslash-ended line.

我编写了以下程序集,它按我的意愿分配数据(一个 20 字节的数组,其中包含一个字符串,后跟下划线,直到数组的倒数第二个索引,后跟一个换行符):

section .data
  buf:                    ; define 20-byte buffer
    db 'Hello, world!'    ; declare string constant
    times 19-$+buf db '_' ; declare byte with value '_' after string and up to index 19 in buffer
    db 0xa                ; declare byte with newline character 0xa (10) at end of buffer
  len equ $-buf           ; define len to be size of buf

有效,但我有点困惑为什么在第四行使用$+buf而不是$-buf .我的理解是每一行都是源码行,$指的是行首,buf指的是标有"buf".

的数据的开头

如果我使用 $-buf 代替 $+buf,NASM 吐出:

error: non-constant argument supplied to TIMES

这是怎么回事?

你忽略了原来的 $ 前面有一个 - 的事实。

19-$+buf = buf-$ + 19

还是地址差,不是和


buf refers to the beginning of the data labelled "buf"

是的,但是您的术语很草率。 buf就是你放置buf标签的位置的地址。它只是一个位置,您可以使用它来访问已知偏移量处的字节。没有明确的方法来判断哪些字节 "are labelled buf"。在这种情况下,可能会通过 buf.

访问整个 20 字节缓冲区(包括用 _ 填充的行之后的换行符)