In MASM (Microsoft Macro Assembler), data is defined in the data segment of a program. The data segment is where you declare and initialize variables, constants, and other data types that your program will use during execution. This section is separate from the code segment, which contains the instructions that the CPU will execute.
In MASM, you use assembler directives to define and initialize data. These directives help the assembler understand how to reserve space in memory for different types of data (such as integers, characters, strings, etc.).
.data
.data directive defines the beginning of the data segment. This is where you will declare all your variables, constants, and strings that are used by your program.Example:
.data
message DB 'Hello, World!', 0 ; Declare a null-terminated string
num1 DW 10 ; Declare a word-sized variable with the value 10
num2 DD 20 ; Declare a double word-sized variable with the value 20
.data?
.data? directive is used to define uninitialized data. These variables will reserve memory space but won't initialize them with any values. This is useful for variables that you will set later in the code.Example:
.data?
buffer DB 256 ; Declare an uninitialized buffer of 256 bytes
DB (Define Byte):
DB reserves space for one byte of data. You can also initialize the byte with a value, such as an integer or character.Syntax:
DB value1, value2, ...
Example:
message DB 'Hello', 0 ; Reserve memory for a string and null-terminate it
DW (Define Word):
DW reserves space for two bytes (a word). It is commonly used to store 16-bit values.Syntax:
DW value1, value2, ...
Example:
num1 DW 100 ; Reserve space for a 16-bit number and initialize it with 100
DD (Define Double Word):
DD reserves space for four bytes (a double word). It is used to store 32-bit values.Syntax:
DD value1, value2, ...
Example:
num2 DD 2000 ; Reserve space for a 32-bit number and initialize it with 2000
DQ (Define Quad Word):
DQ reserves space for eight bytes (a quad word). It is used to store 64-bit values (supported on 64-bit systems).Syntax:
DQ value1, value2, ...
Example:
num3 DQ 5000000000 ; Reserve space for a 64-bit number and initialize it
DT (Define Ten Bytes):
DT reserves space for ten bytes. This is often used for larger data structures or when you need to store floating-point numbers in some formats.Syntax:
DT value1, value2, ...
Example:
bigData DT 12345678901234567890 ; Reserve space for ten bytes (larger numbers)
RESB, RESW, RESD (Reserve Byte, Word, Double Word):
Syntax:
RESB n ; Reserve n bytes
RESW n ; Reserve n words (n*2 bytes)
RESD n ; Reserve n double words (n*4 bytes)
Example:
buffer RESB 256 ; Reserve space for 256 bytes (uninitialized buffer)
In MASM, strings are usually represented as arrays of characters. Since each character takes 1 byte, you can use the DB directive to define strings.
Example of a string:
message DB 'Hello, World!', 0 ; Declare a null-terminated string
0 at the end is a null terminator (character 0), indicating the end of the string. This is a common practice in many programming languages and is needed for functions that handle strings, like printing them to the screen.Example of defining multiple strings:
greeting DB 'Hello', 0
farewell DB 'Goodbye', 0
In MASM, arrays are simply sequences of data items, such as bytes, words, or double words, defined using the appropriate data type directive. Arrays are particularly useful when you need to store multiple values of the same type.
Example of an array of bytes:
numbers DB 10, 20, 30, 40, 50 ; Declare an array of 5 bytes
Example of an array of words:
scores DW 100, 90, 85, 95 ; Declare an array of 4 words
Example of an array of double words:
data DD 1000, 2000, 3000 ; Declare an array of 3 double words (32-bit)
You can also define constants (values that do not change during the program execution) in MASM using the EQU directive. Constants are often used to make your code more readable and easier to maintain.
Syntax:
name EQU value
Example:
PI EQU 3.14159 ; Define a constant for Pi
MAXVAL EQU 100 ; Define a constant for maximum value
You can then use the constant PI and MAXVAL throughout your program as if they were regular values.
Here's a simple example of how data is defined and used in a MASM program:
.model small ; Define small memory model
.stack 100h ; Define stack size
.data ; Data segment begins
message DB 'Hello, MASM!', 0 ; Declare a string with a null terminator
num1 DW 25 ; Declare a word (16-bit) variable and initialize it
num2 DD 1000 ; Declare a double word (32-bit) variable and initialize it
.code ; Code segment begins
start: ; Label for the start of the program
MOV AX, @data ; Load address of data segment
MOV DS, AX ; Move address into DS register
; Code to print the string message
MOV AH, 09h ; DOS function: display string
LEA DX, message ; Load the address of 'message' into DX
INT 21h ; Call interrupt to print message
; Code to perform a simple calculation (num1 + num2)
MOV AX, num1 ; Load num1 into AX register
ADD AX, num2 ; Add num2 to AX
MOV BX, AX ; Move the result to BX register
; Program termination
MOV AH, 4Ch ; DOS function: exit program
INT 21h ; Call interrupt to terminate program
end start ; End of the program
message and variables num1 and num2 are declared.num1 and num2.In MASM assembler, defining data is crucial for storing variables, constants, and strings used by your program. The .data directive defines the data segment, where you can use various directives like DB, DW, DD, and EQU to define different types of data. Understanding how to define and manage data in MASM is a fundamental part of assembly language programming, as it helps structure your program and enables efficient memory management.
Open this section to load past papers