79

我是一个组装初学者,我不知道所有的 db、dw、dd 是什么意思。我试图编写这个执行 1+1 的小脚本,将其存储在一个变量中,然后显示结果。到目前为止,这是我的代码:

.386
.model flat, stdcall 
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
.data
num db ? ; set variable . Here is where I don't know what data type to use.
.code
start:
mov eax, 1               ; add 1 to eax register
mov ebx, 1               ; add 1 to ebx register
add eax, ebx             ; add registers eax and ebx
push eax                 ; push eax into the stack
pop num                  ; pop eax into the variable num (when I tried it, it gave me an error, i think  thats because of the data type)
invoke StdOut, addr num  ; display num on the console.
invoke ExitProcess       ; exit
end start

我需要了解 db、dw、dd 的含义以及它们如何影响变量设置和组合以及诸如此类的事情。

在此先感谢,

4

2 回答 2

104

快速复审,

  • DB - 定义字节。8 位
  • DW - 定义字。在典型的 x86 32 位系统上通常为 2 个字节
  • DD - 定义双字。在典型的 x86 32 位系统上通常为 4 个字节

来自x86 汇编教程

pop 指令将 4 字节数据元素从硬件支持的堆栈顶部移到指定的操作数(即寄存器或内存位置)中。它首先将位于内存位置 [SP] 的 4 个字节移动到指定的寄存器或内存位置,然后将 SP 加 4。

你的 num 是 1 个字节。尝试将其声明为DD4 个字节并与pop语义匹配。

于 2012-04-16T04:39:58.370 回答
36

完整名单是:

DB、DW、DD、DQ、DT、DDQ 和 DO(用于在输出文件中声明初始化数据。)

见:http ://www.tortall.net/projects/yasm/manual/html/nasm-pseudop.html

它们可以通过多种方式调用:(注意:对于 Visual-Studio - 使用“h”而不是“0x”语法 - 例如:不是 0x55 而是 55h):

    db      0x55                ; just the byte 0x55
    db      0x55,0x56,0x57      ; three bytes in succession
    db      'a',0x55            ; character constants are OK
    db      'hello',13,10,'$'   ; so are string constants
    dw      0x1234              ; 0x34 0x12
    dw      'A'                 ; 0x41 0x00 (it's just a number)
    dw      'AB'                ; 0x41 0x42 (character constant)
    dw      'ABC'               ; 0x41 0x42 0x43 0x00 (string)
    dd      0x12345678          ; 0x78 0x56 0x34 0x12
    dq      0x1122334455667788  ; 0x88 0x77 0x66 0x55 0x44 0x33 0x22 0x11
    ddq     0x112233445566778899aabbccddeeff00
    ; 0x00 0xff 0xee 0xdd 0xcc 0xbb 0xaa 0x99
    ; 0x88 0x77 0x66 0x55 0x44 0x33 0x22 0x11
    do      0x112233445566778899aabbccddeeff00 ; same as previous
    dd      1.234567e20         ; floating-point constant
    dq      1.234567e20         ; double-precision float
    dt      1.234567e20         ; extended-precision float

DT 不接受数字常量作为操作数,DDQ 也不接受浮点常量作为操作数。任何大于 DD 的大小都不接受字符串作为操作数。

于 2014-03-21T09:27:47.950 回答