What does the dollar sign ($) mean in x86 assembly when calculating string lengths like “$ - label”? [duplicate]

故事扮演 提交于 2019-11-26 09:35:30

问题


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



来源:https://stackoverflow.com/questions/10361231/what-does-the-dollar-sign-mean-in-x86-assembly-when-calculating-string-lengt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!