In assembly language, data definition statements are used to declare and initialize variables, constants, and data structures that will be used by the program. These statements define the type and size of the data, allocate memory, and may also assign initial values to the data items.
Each assembly language has specific syntax and directives for defining data, but they typically follow similar conventions. The most common data definition directives are:
DB (Define Byte)DW (Define Word)DD (Define Double Word)DQ (Define Quad Word)DT (Define Ten Bytes, used for floating-point numbers)RESB, RESW, RESD, RESQ (Reserved space)These directives are used in the data section (or .data segment) of a program, which is typically used to store variables, constants, strings, and other data that your program will manipulate.
The DB directive is used to define a byte (8 bits) of data. A byte is the smallest unit of storage and can hold values from 0 to 255.
name DB value
section .data
var1 DB 100 ; Declare a byte variable named 'var1' and initialize it with 100
var2 DB 'A' ; Declare a byte variable 'var2' and initialize it with ASCII code of 'A' (65)
var1 holds the value 100 (which is an integer value).var2 holds the value 'A', which is the ASCII value 65.You can also define multiple bytes with a single DB statement:
section .data
byte_array DB 1, 2, 3, 4, 5 ; Define an array of 5 bytes
The DW directive is used to define a word (16 bits, or 2 bytes) of data. This is used when you want to store a value that is 2 bytes in size, such as integers in the range of -32,768 to 32,767.
name DW value
section .data
word1 DW 1234 ; Define a 16-bit word variable with the value 1234
word2 DW 10000 ; Define a word variable with the value 10000
word1 and word2 are both 2-byte variables.You can also define multiple words:
section .data
word_array DW 1, 2, 3, 4 ; Define an array of 4 words
The DD directive is used to define a double word (32 bits, or 4 bytes) of data. Double words are used for larger integer values (up to 4 bytes) and are commonly used in modern 32-bit systems.
name DD value
section .data
dword1 DD 1234567890 ; Define a 32-bit double word variable
dword2 DD -1000 ; Define a 32-bit double word variable with value -1000
dword1 and dword2 are 4-byte variables.You can also define multiple double words:
section .data
dword_array DD 100, 200, 300, 400 ; Define an array of 4 double words
The DQ directive is used to define a quad word (64 bits, or 8 bytes) of data. Quad words are useful for storing larger integer values, typically used in 64-bit systems.
name DQ value
section .data
qword1 DQ 1234567890123456 ; Define a 64-bit quad word variable
qword2 DQ -9876543210 ; Define a 64-bit quad word variable with negative value
qword1 and qword2 are 8-byte variables.The DT directive is used for defining a 10-byte (80-bit) data item. This is primarily used to store floating-point numbers (especially extended precision) in some systems.
name DT value
section .data
pi DT 3.1415926535 ; Define an 80-bit floating-point number
pi is a 10-byte floating-point value representing π.These directives are used when you want to reserve memory space but do not want to initialize it immediately. These directives are especially useful for declaring space for variables whose values will be assigned during runtime.
name RESB number_of_bytes ; Reserve 'number_of_bytes' space (1 byte)
name RESW number_of_words ; Reserve 'number_of_words' space (2 bytes each)
name RESD number_of_dwords ; Reserve 'number_of_dwords' space (4 bytes each)
name RESQ number_of_qwords ; Reserve 'number_of_qwords' space (8 bytes each)
section .bss
buffer RESB 256 ; Reserve 256 bytes (for a buffer or array)
array RESW 10 ; Reserve space for 10 words (20 bytes)
data RESD 5 ; Reserve space for 5 double words (20 bytes)
large_data RESQ 3 ; Reserve space for 3 quad words (24 bytes)
.bss section is used to declare uninitialized variables. The space is reserved but not initialized with any values.In assembly, strings and text constants are typically defined using the DB directive, since each character is stored as a byte (8 bits). You can define strings using single or double quotes. A null-terminated string (with an explicit zero at the end) is often used for string handling in many systems.
name DB 'string', 0 ; Null-terminated string
section .data
greeting DB 'Hello, World!', 0 ; Null-terminated string
message DB 'This is a test.', 0
greeting and message are string constants, terminated by a null byte (0).section .data
var1 DB 100 ; A byte variable initialized to 100
var2 DW 500 ; A word variable initialized to 500
var3 DD 1000 ; A double word variable initialized to 1000
var4 DQ 1234567890 ; A quad word variable initialized to 1234567890
buffer RESB 128 ; Reserve 128 bytes for a buffer
section .text
; Assembly code here that will use these variables
DB, DW, DD, and DQ are used to define data of various sizes (byte, word, double word, quad word)..data section is used to store initialized data..bss section is used to reserve space for variables that will be initialized later.RESB, RESW, RESD, and RESQ are used to reserve memory space without initializing it.These data definition statements are essential for structuring your program's memory and preparing data for processing by your assembly instructions.
Open this section to load past papers