Assembly Language - Constants
Constants are values determined at compile time and cannot be changed during program execution. Using constants in assembly makes code more readable and maintainable.
What are constants in assembly?
In NASM, constants arepseudo-instructionsdefined symbols. The assembler replaces all constant references with actual values at compile time.
Unlike variables, constants do not occupy data-section memory; theyat compile timeare replaced, equivalent to `#define` in C.#define。
EQU - Equate Constant
EQUIt is the most common way to define constants; once defined, it cannot be redefined:
Example
; Numeric constants
MAX_SIZE equ 256 ; Maximum buffer size
DEFAULT_PORT equ 8080 ; Default port number
TRUE equ 1 ; True
FALSE equ 0 ; False
; Character constants
LF equ 0xA ; Newline
CR equ 0xD ; Carriage return
NULL equ 0 ; Null character
; Expression constants
ARRAY_SIZE equ 100 * 4 ; Number of bytes for 100 doublewords
BUFFER_SIZE equ MAX_SIZE * 2 ; Referencing another constant
TOTAL equ 10 + 20 + 30 ; Calculation result
section .data
; Using constants
buffer db MAX_SIZE dup(0) ; Define a buffer of MAX_SIZE bytes
section .text
global _start
_start:
mov eax, MAX_SIZE ; eax = 256
mov ebx, BUFFER_SIZE ; ebx = 512
mov ecx, ARRAY_SIZE ; ecx = 400
mov eax, 1
mov ebx, 0
int 0x80
EQUConstants defined with EQU cannot be modified in the program and cannot be redefined. If you need constants that can be redefined (like macro variables), use%define。
%define - Macro-defined Constant
%defineIt is more flexible than EQU, supporting redefinition and scope control:
Example
%define VERSION 1 ; Define version number
%define APP_NAME 'example' ; Define application name
; Constants defined with %define
mov eax, VERSION ; eax = 1
; Can be redefined (EQU does not allow this)
%define VERSION 2 ; Version upgrade
mov eax, VERSION ; Now eax = 2
; %undef undefines
%undef VERSION ; VERSION is no longer valid
; mov eax, VERSION ; Error: VERSION is undefined
; %define can define multi-line macros (with parentheses)
%define sum(a, b) (a + b)
mov eax, sum(10, 20) ; mov eax, (10+20) -> eax = 30
| Features | EQU | %define |
|---|---|---|
| Can be redefined | no | Yes |
| Can be undefined | no | Yes (%undef) |
| Supports parameters | no | Yes (macro functions) |
| Use cases | True constants (immutable values) | Variable configuration, macro substitution |
%assign - Assignable Constant
%assignUsed to define numeric constants that can be modified multiple times at compile time:
Example
%assign counter 0 ; Initially 0
section .data
; Used with %rep repeated blocks
; Generate table_0, table_1, ..., table_9
%rep 10
db counter ; Use current counter value
%assign counter counter + 1
%endrep
Practical Application of Constants in Programs
Example
; Comprehensively use constants to improve code readability
; System call number constants
SYS_EXIT equ 1
SYS_FORK equ 2
SYS_READ equ 3
SYS_WRITE equ 4
SYS_OPEN equ 5
SYS_CLOSE equ 6
; File descriptor constants
STDIN equ 0
STDOUT equ 1
STDERR equ 2
; Program constants
MAX_INPUT equ 256
EXIT_SUCCESS equ 0
EXIT_FAILURE equ 1
section .data
msg db 'Hello, EXAMPLE!', 0xA
msg_len equ $ - msg
section .bss
buffer resb MAX_INPUT ; Use a constant to define the buffer size
section .text
global _start
_start:
; Using constant names instead of numbers makes the code intent clear at a glance
mov eax, SYS_WRITE ; Clear: this is a write operation
mov ebx, STDOUT ; Clear: output to screen
mov ecx, msg
mov edx, msg_len
int 0x80
mov eax, SYS_EXIT ; Clear: exit the program
mov ebx, EXIT_SUCCESS ; Clear: normal exit
int 0x80
Using constant names instead of "magic numbers" is a good habit. Three months later,
mov eax, SYS_WRITEComparemov eax, 4it's easier to understand.
String Constants
NASM supports several ways to define string constants:
Example
section .data
; Direct definition (length can be expressed with EQU)
s1 db 'hello', 0
s1_len equ $ - s1
; With escape characters
s2 db 'Line1', 0xA, 'Line2', 0 ; 0xA is the newline character
; Use backticks to support C-style escaping
s3 db `hello\nworld\n`, 0 ; \n is automatically replaced with 0xA
; Multi-line string
s4 db 'first line', 0xA
db 'second line', 0xA
db 'third line', 0
Other extensionsNASM's backtick strings support
\n(newline),\t(tab),\\(backslash),\'(single quote) and other standard C escape sequences.