Assembly Language - Constants
A constant is a value determined at compile time that cannot be changed during program execution. Using constants in assembly makes code easier to read and maintain.
What is a constant in assembly
In NASM, a constant is a symbol defined via a directive; the assembler replaces all constant references with actual values at compile time.
Unlike variables, constants do not occupy memory in the data segment; they are replaced at compile time, equivalent to the C language #define.
EQU - equated constant
EQU is the most common way to define a constant; once defined it cannot be redefined:
Example
; EQU constant definition example
; Numeric constant
MAX_SIZE
equ
256
; Maximum buffer size
DEFA
ULT_PORT
equ
8080
; Default port number
TRUE
equ
1
; True value
FALSE
equ
0
; False value
; Character constant
LF
equ
0xA
; Newline character
CR
equ
0xD
; Carriage return character
NULL
equ
0
; Null character
; Expression constant
ARRAY_SIZE
equ
100
4
; Number of bytes in 100 double words
BUFFER_SIZE
equ
MAX_SIZE
2
; Reference another constant
TOTAL
equ
10
20
30
; Computation result
section
.data
; Use the constant
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
EQUA defined constant cannot be modified in the program, nor redefined. If you need a constant that can be redefined (like a macro variable), use%define.
%define - macro-defined constant
%define More flexible than EQU, supporting redefinition and scope control:
Example
; %define constant definition and redefinition example
%define
VERSION
1
; Define the version number
%define
APP_NAME
'runoob'
; Define the application name
; Use the constant defined by %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 to undefine
%undef
VERSION
; VERSION is no longer valid
; mov eax, VERSION ; Error: VERSION is not defined
; %define can define multiple lines (a macro with parentheses)
%define
sum
(
a
,
b
)
(
a
b
)
mov
eax
,
sum
(
10
,
20
)
; mov eax, (10+20) -> eax = 30
| Feature | EQU | %define |
|---|---|---|
| Can be redefined | No | Is |
| Can be undefined | No | Yes (%undef) |
| Supports parameters | No | Yes (macro function) |
| Use Cases | A true constant (immutable value) | Mutable configuration, macro substitution |
%assign - assignable constant
%assign Used to define numeric constants that can be modified multiple times at compile time:
Example
; %assign compile-time mutable constant
%assign
counter
0
; Initially 0
section
.data
; Used together with the %rep repeat block
; Generate table_0, table_1, ..., table_9
%
rep
10
db
counter
; Use the current counter value
%assign
counter counter
1
%endrep
Practical use of constants in a program
Example
; File path: constants_practice.asm
; Use constants comprehensively 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, RUNOOB!'
,
0xA
msg_len
equ
$
msg
section
.bss
buffer
resb
MAX_INPUT
; Use a constant to define the buffer size
section
.text
global
_start
_start
:
; Use constant names instead of numbers so the code intent is clear at a glance
mov
eax
,
SYS_WRITE
; Clear: this is a write operation
mov
ebx
,
STDOUT
; Clear: output to the 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_WRITEcompared tomov eax, 4it is easier to understand.
String Constants
NASM supports several ways to define string constants:
Example
; Ways to define string constants
section
.data
; Direct definition (EQU can be used for the length)
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 escapes
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
NASM backtick strings support
\n(newline),\t(tab),\\(backslash),\'(single quote), and other standard C escape sequences.