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++.
Is there a reason for using one or the other syntax?
What is the difference between them?
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
.
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.
Given x: db 0
and x db 0
, it would seem that both create a label called x
.
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.