Overview of Variable Definitions
In assembly language, variables are allocated storage space in memory through definition directives. NASM provides various definition directives to declare and initialize variables.
Variable Definition Directives
Data Initialization Definition Directives
| Directive | Purpose | Space Allocated |
|---|---|---|
| DB | Define Byte | 1 Byte |
| DW | Define Word | 2 Bytes |
| DD | Define Double Word | 4 Bytes |
| DQ | Define Quad Word | 8 Bytes |
| DT | Define Ten Bytes | 10 Bytes |
Basic Variable Definition Examples
1. Character and String Variables
section .data
; Character variables
single_char DB 'A' ; Single character
newline DB 0xA ; Newline character
tab_char DB 0x9 ; Tab character
null_term DB 0 ; Null character
; String variables
message DB 'Hello, World!', 0xA, 0 ; String + newline + terminator
name_str DB 'Assembly', 0
empty_string DB '' ; Empty string
section .text
global _start
_start:
; Using character variables
mov al, [single_char] ; Load character 'A' into AL register
mov bl, [newline] ; Load newline character into BL register
; Display string
mov eax, 4
mov ebx, 1
mov ecx, message
mov edx, 14 ; String length
int 0x80
mov eax, 1
mov ebx, 0
int 0x80
2. Numeric Variables
section .data
; Integer variables
small_number DB 42 ; 8-bit signed integer
unsigned_byte DB 255 ; 8-bit unsigned integer
word_value DW 12345 ; 16-bit integer
double_word DD 123456789 ; 32-bit integer
quad_word DQ 123456789012 ; 64-bit integer
; Negative variables (using two's complement)
negative_byte DB -42 ; 8-bit negative number
negative_word DW -12345 ; 16-bit negative number
negative_dword DD -1000 ; 32-bit negative number
; Hexadecimal representation
hex_byte DB 0xFF ; Hexadecimal
hex_word DW 0x1234 ; Hexadecimal word
hex_dword DD 0xDEADBEEF ; Hexadecimal double word
section .text
global _start
_start:
; Using numeric variables
mov al, [small_number] ; Load byte
mov bx, [word_value] ; Load word
mov ecx, [double_word] ; Load double word
; Arithmetic operations
mov al, [small_number]
add al, 10 ; 42 + 10 = 52
mov [small_number], al ; Store back to memory
mov eax, 1
mov ebx, 0
int 0x80
3. Floating Point Variables
section .data
; Single precision floating point (32-bit)
float1 DD 3.14159 ; Single precision floating point
float2 DD -2.71828 ; Negative floating point
float3 DD 0.0 ; Zero value
; Double precision floating point (64-bit)
double1 DQ 3.141592653589793
double2 DQ -1.234567890
double3 DQ 0.0
section .text
global _start
_start:
; Note: x87 floating point coprocessor or SSE instructions are used for floating point operations
; This is just to demonstrate variable definitions
mov eax, 1
mov ebx, 0
int 0x80
Uninitialized Data Definitions
Space Reservation Directives
| Directive | Purpose |
|---|---|
| RESB | Reserve Bytes |
| RESW | Reserve Words |
| RESD | Reserve Double Words |
| RESQ | Reserve Quad Words |
| REST | Reserve Ten Bytes |
section .bss
; Buffer definitions
input_buffer RESB 256 ; 256-byte input buffer
output_buffer RESB 512 ; 512-byte output buffer
; Numeric buffers
temp_byte RESB 1 ; 1-byte temporary variable
temp_word RESW 1 ; 1-word temporary variable
temp_dword RESD 1 ; 1 double word temporary variable
temp_qword RESQ 1 ; 1 quad word temporary variable
; Array buffers
byte_array RESB 100 ; 100-byte array
word_array RESW 50 ; 50-word array (100 bytes)
dword_array RESD 25 ; 25 double word array (100 bytes)
; Struct-like buffers
person_name RESB 32 ; 32-byte name
person_age RESB 1 ; 1-byte age
person_id RESD 1 ; 4-byte ID
section .text
global _start
_start:
; Using uninitialized buffers
mov edi, input_buffer ; Point to input buffer
mov ecx, 256 ; Buffer size
mov al, 0 ; Fill value
rep stosb ; Fill buffer with zeros
; Initialize array
mov edi, byte_array
mov ecx, 100
mov al, 0xFF
rep stosb
mov eax, 1
mov ebx, 0
int 0x80
Multiple Variable Definitions and Arrays
1. Multiple Variable Definitions
section .data
; Multiple variables of the same type
var1 DB 10
var2 DB 20
var3 DB 30
; Multiple variables of different types
char1 DB 'X'
num1 DW 1000
big1 DD 1000000
; Continuous definitions
x DB 1, 2, 3, 4, 5 ; 5 continuous bytes
y DW 10, 20, 30 ; 3 continuous words
z DD 100, 200, 300 ; 3 continuous double words
section .text
global _start
_start:
; Accessing continuous variables
mov al, [x] ; al = 1
mov al, [x+1] ; al = 2
mov al, [x+2] ; al = 3
mov bx, [y] ; bx = 10
mov bx, [y+2] ; bx = 20 (each word is 2 bytes)
mov ecx, [z] ; ecx = 100
mov ecx, [z+4] ; ecx = 200 (each double word is 4 bytes)
mov eax, 1
mov ebx, 0
int 0x80
2. Array Definitions
section .data
; One-dimensional arrays
byte_array DB 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
word_array DW 100, 200, 300, 400, 500
dword_array DD 1000, 2000, 3000, 4000, 5000
; String arrays (effectively two-dimensional arrays)
names DB 'Alice', 0
DB 'Bob', 0
DB 'Charlie', 0
DB 'David', 0
DB 'Eve', 0
; Matrix (5x5 byte matrix)
matrix DB 1, 2, 3, 4, 5
DB 6, 7, 8, 9, 10
DB 11, 12, 13, 14, 15
DB 16, 17, 18, 19, 20
DB 21, 22, 23, 24, 25
section .text
global _start
_start:
; Array traversal example
mov esi, byte_array ; Starting address of the array
mov ecx, 10 ; Number of elements
mov eax, 0 ; Sum accumulator
sum_loop:
movzx ebx, byte [esi] ; Zero-extend byte to double word
add eax, ebx ; Accumulate
inc esi ; Move to the next element
loop sum_loop
; Matrix access example
; Access matrix[2][3] (3rd row 4th column)
mov esi, matrix
mov eax, 2 ; Row index
mov ebx, 5 ; Number of columns
mul ebx ; eax = row offset
add eax, 3 ; Column offset
mov bl, [matrix + eax] ; bl = matrix[2][3] = 14
mov eax, 1
mov ebx, 0
int 0x80
Using the TIMES Directive
Repeated Initialization
section .data
; Using TIMES for repeated initialization
zeros TIMES 100 DB 0 ; 100 zeros
ones TIMES 50 DB 1 ; 50 ones
pattern TIMES 10 DB 1, 2, 3 ; Pattern repeat: 1,2,3,1,2,3...
; Asterisk string
stars TIMES 9 DB '*' ; 9 asterisks
spaces TIMES 5 DB ' ' ; 5 spaces
; Numeric repetition
word_zeros TIMES 20 DW 0 ; 20 zero words
dword_array TIMES 5 DD 0x12345678 ; 5 identical double words
section .text
global _start
_start:
; Display 9 asterisks
mov eax, 4
mov ebx, 1
mov ecx, stars
mov edx, 9
int 0x80
; Display newline
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
; Use array created by TIMES
mov esi, pattern
mov ecx, 10
print_pattern:
mov eax, 4
mov ebx, 1
mov ecx, esi
mov edx, 1
int 0x80
inc esi
loop print_pattern
mov eax, 1
mov ebx, 0
int 0x80
section .data
newline DB 0xA
Complex Data Structure Examples
Structures and Records
section .data
; Data organization similar to structures
student:
name DB 'John Doe', 0
TIMES 32-9 DB 0 ; Fill to 32 bytes
age DB 20
grade DB 'A'
id DD 12345
gpa DD 3.75
; Multiple student records
students:
student1:
s1_name DB 'Alice Smith', 0
TIMES 32-12 DB 0
s1_age DB 21
s1_grade DB 'B'
s1_id DD 12346
s1_gpa DD 3.25
student2:
s2_name DB 'Bob Johnson', 0
TIMES 32-12 DB 0
s2_age DB 22
s2_grade DB 'A'
s2_id DD 12347
s2_gpa DD 3.90
section .text
global _start
_start:
; Access structure fields
mov ecx, [student + 32 + 1 + 1] ; Access id field
; Calculate offset: name(32) + age(1) + grade(1) = 34
; Iterate through student records
mov esi, students
mov ecx, 2 ; Number of students
student_loop:
; Display student name
mov eax, 4
mov ebx, 1
mov ecx, esi ; Name address
mov edx, 32 ; Name length
int 0x80
; Move to next student record
add esi, 32 + 1 + 1 + 4 + 4 ; Start of next record
loop student_loop
mov eax, 1
mov ebx, 0
int 0x80
Comprehensive Example: Student Grade Management System
section .data
; Constant definitions
MAX_STUDENTS equ 5
NAME_LENGTH equ 20
; Menu strings
menu_msg DB '=== Student Grade System ===', 0xA
DB '1. Add Student', 0xA
DB '2. List Students', 0xA
DB '3. Exit', 0xA
DB 'Choice: ', 0
menu_len equ $ - menu_msg
; Prompt messages
name_prompt DB 'Enter student name: ', 0
name_len equ $ - name_prompt
grade_prompt DB 'Enter grade (A-F): ', 0
grade_len equ $ - grade_prompt
id_prompt DB 'Enter student ID: ', 0
id_len equ $ - id_prompt
; Error and information messages
error_msg DB 'Invalid input!', 0xA, 0
error_len equ $ - error_msg
full_msg DB 'Database full!', 0xA, 0
full_len equ $ - full_msg
empty_msg DB 'No students in database!', 0xA, 0
empty_len equ $ - empty_msg
newline DB 0xA
section .bss
; Student database
students RESB MAX_STUDENTS * (NAME_LENGTH + 1 + 4)
; Each student: Name(20) + Grade(1) + ID(4) = 25 bytes
; Temporary buffer
input_buffer RESB 32
student_count RESB 1
section .text
global _start
_start:
mov byte [student_count], 0 ; Initialize student count
main_loop:
call display_menu
call read_choice
cmp byte [input_buffer], '1'
je add_student
cmp byte [input_buffer], '2'
je list_students
cmp byte [input_buffer], '3'
je exit_program
call display_error
jmp main_loop
add_student:
; Check if full
mov al, [student_count]
cmp al, MAX_STUDENTS
jge database_full
call get_student_info
inc byte [student_count]
jmp main_loop
database_full:
mov eax, 4
mov ebx, 1
mov ecx, full_msg
mov edx, full_len
int 0x80
jmp main_loop
list_students:
; Check if empty
mov al, [student_count]
cmp al, 0
je database_empty
call display_all_students
jmp main_loop
database_empty:
mov eax, 4
mov ebx, 1
mov ecx, empty_msg
mov edx, empty_len
int 0x80
jmp main_loop
; Subroutine: Display menu
display_menu:
mov eax, 4
mov ebx, 1
mov ecx, menu_msg
mov edx, menu_len
int 0x80
ret
; Subroutine: Read choice
read_choice:
mov eax, 3
mov ebx, 0
mov ecx, input_buffer
mov edx, 2
int 0x80
ret
; Subroutine: Get student information
get_student_info:
; Calculate student storage location
mov al, [student_count]
mov bl, 25 ; Each student 25 bytes
mul bl
mov edi, students
add edi, eax
; Get name
mov eax, 4
mov ebx, 1
mov ecx, name_prompt
mov edx, name_len
int 0x80
mov eax, 3
mov ebx, 0
mov ecx, edi ; Name storage location
mov edx, NAME_LENGTH
int 0x80
; Get grade
add edi, NAME_LENGTH ; Move to grade field
mov eax, 4
mov ebx, 1
mov ecx, grade_prompt
mov edx, grade_len
int 0x80
mov eax, 3
mov ebx, 0
mov ecx, input_buffer
mov edx, 2
int 0x80
mov al, [input_buffer]
mov [edi], al ; Store grade
inc edi ; Move to ID field
; Get ID (simplified)
mov dword [edi], 1000 ; Simplified: auto-assign ID
add dword [edi], [student_count] ; ID = 1000 + index
ret
; Subroutine: Display all students
display_all_students:
mov esi, students
mov ecx, 0
display_loop:
cmp cl, [student_count]
jge display_done
; Display student information
push ecx
push esi
; Display name
mov eax, 4
mov ebx, 1
mov ecx, esi ; Name address
mov edx, NAME_LENGTH
int 0x80
; Display grade
add esi, NAME_LENGTH
mov eax, 4
mov ebx, 1
mov ecx, esi
mov edx, 1
int 0x80
; Display newline
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
pop esi
pop ecx
add esi, 25 ; Next student
inc ecx
jmp display_loop
display_done:
ret
; Subroutine: Display error
display_error:
mov eax, 4
mov ebx, 1
mov ecx, error_msg
mov edx, error_len
int 0x80
ret
exit_program:
mov eax, 1
mov ebx, 0
int 0x80
Best Practices for Variable Definitions
1. Memory Alignment
section .data
; Align data for performance improvement
aligned_data:
align 4
dword_data DD 12345678
align 2
word_data DW 1000
align 1
byte_data DB 42
2. Meaningful Naming
section .data
; Good naming
student_count DB 0
max_buffer_size EQU 1024
file_name DB 'data.txt', 0
; Avoid naming
a DB 0 ; Meaningless
x DW 100 ; Unclear purpose
3. Organizing Related Data
section .data
; Group related data
config:
screen_width DW 800
screen_height DW 600
color_depth DB 32
user:
user_name DB 'username', 0
user_id DD 12345
user_level DB 1
Conclusion
Key Points:
-
Definition Directives:
- DB/DW/DD/DQ/DT – Initialize data
- RESB/RESW/RESD/RESQ/REST – Uninitialized data
Data Types:
- Characters and Strings
- Signed and Unsigned Integers
- Floating Point Numbers
- Arrays and Structures
Special Directives:
- TIMES – Repeated initialization
- EQU – Define constants
Memory Management:
- Proper use of .data and .bss sections
- Consider memory alignment
- Clear variable naming and organization
By properly using variable definition directives, one can create well-structured and maintainable assembly programs that effectively manage memory resources.