Which variable size to use (db, dw, dd) with x86 assembly?

Progrmr picture Progrmr · Apr 16, 2012 · Viewed 193.9k times · Source

I am a beginner to assembly and I don't know what all the db, dw, dd, things mean. I have tried to write this little script that does 1+1, stores it in a variable and then displays the result. Here is my code so far:

.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

I need to understand what the db, dw, dd things mean and how they affect variable setting and combining and that sort of thing.

Thanks in advance, Progrmr

Answer

Pavan Manjunath picture Pavan Manjunath · Apr 16, 2012

Quick review,

  • DB - Define Byte. 8 bits
  • DW - Define Word. Generally 2 bytes on a typical x86 32-bit system
  • DD - Define double word. Generally 4 bytes on a typical x86 32-bit system

From x86 assembly tutorial,

The pop instruction removes the 4-byte data element from the top of the hardware-supported stack into the specified operand (i.e. register or memory location). It first moves the 4 bytes located at memory location [SP] into the specified register or memory location, and then increments SP by 4.

Your num is 1 byte. Try declaring it with DD so that it becomes 4 bytes and matches with pop semantics.