I want to know what is the difference between this code and this code in assembly language:
a db 2 dup (1fh)
b dw 1fh,1fh
and I think that the
The 2nd line is equivalent to db 1fh, 0, 1fh, 0
, because each arg to DW is a word-sized integer. (And x86 is little-endian)
The 1st line is equivalent to db 1fh, 1fh
.
To do that with DW, use dw 1f1fh
. For a very short constant, that's maybe clearer. For anything more than 2 repeats of 2 bytes, using the dup
syntax is probably clearer for other humans to see that it's the same thing multiple times, without having to look carefully to check for differences.
If you're using an assembler which implicitly associates a size with a symbol, it matters whether you use dw
or db
, so if you want the symbol to be declared as a byte
"variable", you need a db 1fh,1fh
or a db 2 dup (1fh)
.
Think of dup
as an operator where the left side is the count and the right side is the thing being repeated. Like Python's 'abc' * 3
being equivalent to "abcabcabc"
, or Perl's 'abc' x 4
, except with the order of the operands reversed.
IDK why MASM is designed this way.
NASM syntax is times 2 db 0x1f
. The times
repeats a whole pseudo-instruction, or a whole instruction like times 3 imul eax, ecx
.