YouTip LogoYouTip

Assembly Constants

```html\\n

Assembly Language - Constants

\\n

Constants are values determined at compile time that cannot be changed during program execution. Using constants in assembly makes code more readable and easier to maintain.

\\n
\\n

What are Constants in Assembly

\\n

In NASM, constants are symbols defined through pseudo-instructions. The assembler replaces all constant references with their actual values at compile time.

\\n

Unlike variables, constants do not occupy memory in the data segment; they are replaced at compile time, equivalent to #define in the C language.

\\n
\\n

EQU - Equal Constants

\\n

EQU is the most common way to define constants. Once defined, they cannot be redefined:

\\n

Example

\\n
; EQU Constant Definition Example\\n\\n; Numeric Value Constants\\n\\n MAX_SIZE equ 256; Maximum Buffer Size\\n\\nDEFA ULT_PORT equ 8080; Default Port Number\\n\\n TRUE equ 1; TrueValue\\n\\n FALSE equ 0; FalseValue\\n\\n; Character Constants\\n\\n LF equ 0xA; Newline character\\n\\n CR equ 0xD; Carriage return\\n\\n NULL equ 0; Null character\\n\\n; Expression Constants\\n\\n ARRAY_SIZE equ 100*4; 100 Number of Bytes in a Double Word\\n\\n BUFFER_SIZE equ MAX_SIZE *2; Reference Another Constant\\n\\n TOTAL equ 10+20+30; Calculation result\\n\\nsection.data\\n\\n; UseConstants\\n\\n buffer db MAX_SIZE dup(0); Define Buffer of MAX_SIZE Bytes\\n\\nsection.text\\n\\nglobal _start\\n\\n_start:\\n\\nmov eax, MAX_SIZE ; eax = 256\\n\\nmov ebx, BUFFER_SIZE ; ebx = 512\\n\\nmov ecx, ARRAY_SIZE ; ecx = 400\\n\\nmov eax,1\\n\\nmov ebx,0\\n\\nint 0x80
\\n
Constants defined with EQU cannot be modified in the program, nor can they be redefined. If you need redefinable constants (similar to macro variables), use %define.
\\n
\\n

%define - Macro Definition Constants

\\n

%define is more flexible than EQU, supporting redefinition and scope control:

\\n

Example

\\n
; %define Constant Definition and Redefinition Example\\n\\n%define VERSION 1; Define Version Number\\n\\n%define APP_NAME 'tutorial'; Define Application Name\\n\\n; Use %define Defined Constants\\n\\nmov eax, VERSION ; eax = 1\\n\\n; Can be redefined (EQU does not allow this)\\n\\n%define VERSION 2; Version Upgrade\\n\\nmov eax, VERSION ; Now eax = 2\\n\\n; %undef Undefine\\n\\n%undef VERSION ; VERSION No Longer Valid\\n\\n; mov eax, VERSION  ; Error: VERSION Not Defined\\n\\n; %define Multi-line Definition (Macros with Parentheses)\\n\\n%define sum(a, b)(a + b)\\n\\nmov eax, sum(10,20); mov eax, (10+20) -> eax = 30
\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n
FeatureEQU%define
Can be redefinedNoYes
Can be undefinedNoYes (%undef)
Supports parametersNoYes (macro functions)
Use casesTrue constants (unchanging values)Variable configurations, macro substitution
\\n
\\n

%assign - Assignable Constants

\\n

%assign is used to define numeric constants that can be modified multiple times at compile time:

\\n

Example

\\n
; %assign Compile-Time Variable Constants\\n\\n%assign counter 0; Initially 0\\n\\nsection.data\\n\\n; Use with %rep Repeat Block\\n\\n; Generate table_0, table_1, ..., table_9\\n\\n%rep 10\\n\\ndb counter ; UseCurrent counter Value\\n\\n%assign counter counter +1\\n\\n%endrep
\\n
\\n

Practical Application of Constants in Programs

\\n

Example

\\n
; File Path: constants_practice.asm\\n\\n; Comprehensive Use of Constants to Improve Code Readability\\n\\n; System Call Number Constants\\n\\n SYS_EXIT equ 1\\n\\n SYS_FORK equ 2\\n\\n SYS_READ equ 3\\n\\n SYS_WRITE equ 4\\n\\n SYS_OPEN equ 5\\n\\n SYS_CLOSE equ 6\\n\\n; File Descriptor Constants\\n\\n STDIN equ 0\\n\\n STDOUT equ 1\\n\\n STDERR equ 2\\n\\n; Program Constants\\n\\n MAX_INPUT equ 256\\n\\n EXIT_SUCCESS equ 0\\n\\n EXIT_FAILURE equ 1\\n\\nsection.data\\n\\n msg db'Hello, TUTORIAL!',0xA\\n\\n msg_len equ$- msg\\n\\nsection.bss\\n\\n buffer resb MAX_INPUT
\\n```
← Assembly LogicAssembly Addressing β†’