Assembly programming - "variables" defined using (what looks like) labels, or variable names?

StackOverflow https://stackoverflow.com/questions/17913264

  •  04-06-2022
  •  | 
  •  

سؤال

I have seen "variables" in assembly "defined" using two syntaxes, the first looks like a label, and the second looks like a variable name. Can the two be used interchangeably, or is there a specific reason for each ones use?

For example:

msg db "Hello World",0x0a

looks kind of like a variable name in C / C++, whereas

msg: db "Hello World",0x0a

looks kind of like a label in either asm or C / C++.

  1. Is there a reason for using one or the other syntax?

  2. What is the difference between them?

  3. If I msg: db "Text",0x0a, can I then mov rax, [msg] or do I need to mov rax, [msg:]?

هل كانت مفيدة؟

المحلول

From the NASM documentation: The colon after a label is also optional. I guess this means that both pieces of code create a label called msg.

  1. Code style, readability & consistency come to mind as a reason for using one over the other. But seeing as the semantics are the same, I would imagine that's the only reason.

  2. Given x: db 0 and x db 0, it would seem that both create a label called x.

  3. I would say you would do mov rax, [msg]. This is because in NASM, when you create a label, the semicolon at the end is not included in the name but rather is a hint to the assembler that the end of the label's name has been reached.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top