What does the dollar sign ($) mean in x86 assembly

2019-01-07 13:23发布

问题:

This question already has an answer here:

  • How does $ work in NASM, exactly? 1 answer

For example, if we were writing a simple hello world type program, the .data section might contain something like:

section .data

msg     db      'Enter something: '
len     equ     $ - msg

What does the $ in this example represent, and why does $ - msg equal the length of the string?

回答1:

It means the address of "here". In here "here" is the byte after the end of the msg string. Any assembler documentation will describe this. Read the documentation.



回答2:

In this case, the $ means the current address according to the assembler. $ - msg is the current address of the assembler minus the address of msg, which would be the length of the string.



回答3:

NASM documentation

http://www.nasm.us/doc/nasmdoc3.html#section-3.5

NASM supports two special tokens in expressions, allowing calculations to involve the current assembly position: the $ and $$ tokens. $ evaluates to the assembly position at the beginning of the line containing the expression; so you can code an infinite loop using JMP $.

http://www.nasm.us/doc/nasmdoc3.html#section-3.2.4

EQU defines a symbol to a given constant value: when EQU is used, the source line must contain a label. The action of EQU is to define the given label name to the value of its (only) operand. This definition is absolute, and cannot change later. So, for example,

message         db      'hello, world' 
msglen          equ     $-message

defines msglen to be the constant 12



回答4:

$ is used to refer to the current address and $$ is used to refer to the address of the start of current section in assembly.

example:

section .text
  Mov A,0x0000
  Mov B,0x0000
  Mov C,0x0000

for 3rd line $ refers to the address of the line itself while $$ refers to the address of the 1st line (where our section started). This convention works for me in nasm.

source:nasm.us