2012-04-16 28 views
45

我是彙編的初學者,我不知道db,dw,dd是什麼意思。 我試圖寫這個1 + 1的小腳本,將它存儲在一個變量中,然後顯示結果。這是我到目前爲止的代碼:x86彙編 - 使用哪種可變大小(db,dw,dd)

.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 

我需要了解什麼數據庫,DW,DD事是什麼意思以及它們如何影響變量設置和組合以及諸如此類的事情。

由於提前, Progrmr

+2

db字節8位dw字16位dd雙字32位。請注意,對於其他平臺上的x86,一個字是32位,半字是16位。其他一個字節是9位,等等你用db創建的是一個字節集合。就像無符號char [] = ...在C中。 – 2012-04-16 04:40:44

+0

'push eax' /'pop [num]'是荒謬的。只需'mov [num],eax'。或者'mov dword [num],1 + 1'讓彙編程序在彙編時爲你做1 + 1,而不是運行時,併發出'mov m32,imm32'指令編碼。 (由於沒有寄存器操作數可以從中推斷大小,因此需要'dword'大小)。或'mov eax,1' /'add eax,1'。 – 2016-02-25 10:07:47

+0

我從upvotes的數量中認爲,這是Google在實際手冊中出現的RTFM問題之一。**請參閱[x86標記wiki](http://stackoverflow.com/tags/x86/info)**以獲取參考資料和教程的鏈接。答案中沒有提到的一個微妙之處在於,MASM使用標籤後聲明的空間大小來暗示指向它的指令的操作數大小。 NASM語法沒有任何奇怪的猜測 - 你說的是什麼意思:你可以告訴一個指令如何組裝,而不用考慮其他的源代碼。 – 2016-02-25 10:16:13

回答

59

快速回顧,

  • DB - 定義字節。 8位
  • DW - 定義Word。通常,典型的x86 32位系統上的2個字節
  • DD - 定義雙字。一般在4個字節的典型x86 32位系統

x86 assembly tutorial上,

POP指令從 頂部移除的4字節數據元素的硬件支持堆棧進指定的操作數(即寄存器 或內存位置)。它首先移動位於存儲器 位置[SP]到指定的寄存器或存儲器位置的4個字節,然後通過 4.

遞增SP你num是1個字節。嘗試使用DD聲明它,使其變爲4個字節並與pop語義相匹配。

+2

謝謝。這非常有幫助! – Progrmr 2012-04-29 10:35:49

20

的完整列表:

DB,DW,DD,DQ,DT,DDQ,做

見(用於在輸出文件中聲明初始化的數據。):http://www.tortall.net/projects/yasm/manual/html/nasm-pseudop.html

他們可以在很寬範圍的方式調用:(注:視覺工作室 - 而不是使用「0X」語法「H」 - 如:不是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不接受數字c onstants作爲操作數,而DDQ不接受float常量作爲操作數。任何大於DD的大小都不接受字符串作爲操作數。

+0

嗯,像@LeoMingo在他的評論中指出的(發佈爲答案),這個例子是越野車!這些字符串應該是大寫字母以表示這些ASCII值。小寫ascii的十六進制值高出0x20。 – 2016-02-25 10:13:31

+0

斑點!我修好了它。 – cnd 2017-01-22 22:55:10