what is the difference beetwen dw and db with dup in assembly

岁酱吖の 提交于 2020-01-30 12:29:28

问题


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 second code is better and I don't know why they make number first.


回答1:


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.



来源:https://stackoverflow.com/questions/52398258/what-is-the-difference-beetwen-dw-and-db-with-dup-in-assembly

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