Directives (sometimes referred to as pseudo-ops) are special instructions in assembly language that provide guidance to the assembler (the tool that translates assembly code into machine code) rather than to the CPU. Unlike regular assembly instructions (such as MOV, ADD, or JMP), which are executed by the CPU, directives are used to control the assembly process or provide information about how the code should be organized, how data is stored, or how memory is allocated.
Directives don’t generate machine code directly but help the assembler understand how to interpret the program. They can define memory spaces, set constants, allocate space, specify data sections, and much more.
Directives in assembly can be categorized into different types based on their function. Here are the most common types of directives:
These directives are used to define and initialize data in the program. Data is typically stored in memory, and these directives tell the assembler how to allocate and initialize space for data.
DB (Define Byte)DB value1, value2, ..., valueNExample:
message DB 'Hello, World!', 0 ; Define a string with a null terminator
This example defines a byte array message containing the string "Hello, World!" followed by a null terminator (0).
DW (Define Word)DW value1, value2, ..., valueNExample:
dataWords DW 100, 200, 300 ; Define three 16-bit words
This defines three 16-bit words with values 100, 200, and 300.
DD (Define Doubleword)DD value1, value2, ..., valueNExample:
longNumbers DD 1000, 2000, 3000 ; Define three 32-bit values
This defines three 32-bit values with the specified integers.
DQ (Define Quadword)DQ value1, value2, ..., valueNExample:
bigNumbers DQ 1000000000, 2000000000
This defines two 64-bit values.
DT (Define Ten Bytes)DT value1, value2, ..., valueNExample:
realData DT 3.14159, 2.71828
This defines a ten-byte area for floating-point data.
These directives are used to reserve space in memory without initializing it. The data space is not filled with any value but simply allocated.
RESB (Reserve Byte)RESB number_of_bytesExample:
buffer RESB 128 ; Reserves 128 bytes for a buffer
This reserves 128 bytes of space for the buffer without initializing the data.
RESW (Reserve Word)RESW number_of_wordsExample:
stack RESW 64 ; Reserves 64 words (128 bytes) for the stack
This reserves 128 bytes for the stack, where each word is 2 bytes.
RESD (Reserve Doubleword)RESD number_of_doublewordsExample:
array REVD 10 ; Reserves 10 doublewords (40 bytes) for an array
This reserves 40 bytes for an array, where each doubleword is 4 bytes.
These directives control the flow and execution of the program. They can be used to define program properties, constants, or control how the program is compiled and linked.
EQU (Equate)EQU directive.name EQU valueExample:
MAX_SIZE EQU 1024 ; Define a constant MAX_SIZE with value 1024
This defines a constant named MAX_SIZE with the value 1024.
GLOBALGLOBAL symbol_nameExample:
GLOBAL _start ; Declare the symbol _start to be accessible outside the module
This makes the label _start accessible from other modules or external files during linking.
EXTERNEXTERN symbol_nameExample:
EXTERN printf ; Declare that 'printf' will be defined elsewhere
This tells the assembler that printf is an external function that will be linked during the linking stage.
These directives define sections or segments in the program. A section is a portion of memory designated for specific purposes, such as data or code.
.data.dataExample:
.data
message DB 'Hello, World!', 0
This tells the assembler that the following data is initialized and should be placed in the .data section.
.text.textExample:
.text
MOV AX, 1 ; Example of program logic
This marks the following code as executable and places it in the .text section.
.bss.bss section is used for variables that are declared but not yet initialized..bssExample:
.bss
counter RESB 4 ; Reserve 4 bytes for an integer counter (uninitialized)
This reserves 4 bytes for the variable counter without initializing it.
Assemblers can include macros, which are essentially blocks of code that can be reused multiple times within the program. Macros are expanded by the assembler during the assembly process.
MACROMACRO
; macro code
ENDM
Example:
PRINT_MACRO MACRO msg
MOV AH, 09h
MOV DX, msg
INT 21h
ENDM
This defines a macro PRINT_MACRO, which prints a string using DOS interrupt 21h. The msg parameter will be replaced by the actual string when the macro is used.
ALIGNALIGN boundaryExample:
ALIGN 4 ; Align the next data on a 4-byte boundary
This ensures that the next data starts at an address that is divisible by 4.
INCLUDEOpen this section to load past papers