YouTip LogoYouTip

Assembly Variables

## Assembly Language - Variables Variables are the basic unit for storing data in a program. Defining and using variables in assembly language is closer to the hardware than in high-level languages. You need to directly control the variable's size, type, and memory layout. * * * ## Variable Concepts in Assembly In assembly language, a variable is essentially a named storage location in memory. Unlike high-level languages, assembly variables do not have automatic type checkingβ€”a label is just an alias for a memory address, and you can read from or write to it using any size. NASM provides three segments for storing different types of variables: `.data` (initialized), `.bss` (uninitialized), and `.rodata` (read-only). * * * ## Defining Initialized Variables in .data Segment Use DB, DW, DD, DQ, DT pseudo-instructions to define variables of different sizes: | Pseudo-instruction | Data Size | Meaning | Example | | --- | --- | --- | --- | | DB | 1 byte | Define Byte | `flag db 1` | | DW | 2 bytes | Define Word | `count dw 1000` | | DD | 4 bytes | Define Doubleword | `price dd 9999` | | DQ | 8 bytes | Define Quadword | `big_val dq 0x1234567890ABCDEF` | | DT | 10 bytes | Define Ten Bytes | `ext_val dt 3.14` | ## Example ; File path: variables_data.asm ; Demo various variable definitions in .data segment section.data ; Byte variable status db 1; 1 byte, value is 1 grade db'A'; 1 byte, character 'A' = 0x41 ; Word variable (2 bytes) year dw 2026; 2 bytes, value is 2026 ; Doubleword variable (4 bytes) salary dd 50000; 4 bytes, value is 50000 ; Multi-byte sequence msg db'Hello, tutorial!',0; String ends with 0 (C-style) hex_bytes db 0x55,0xAA,0x00,0xFF; Hex byte sequence ; Repeated values stars db 10 dup('*'); 10 asterisk characters ; Multiple variables with names x dd 10 y dd 20 z dd 30 section.text global _start _start: ; Read variable values into registers mov al,; al = 1 (read 1 byte) mov ax,; ax = 2026 (read 2 bytes) mov eax,; eax = 50000 (read 4 bytes) ; Modify variable values mov byte,0; status = 0 mov dword,100; x = 100 mov eax,1 mov ebx,0 int 0x80 > When accessing variables using ``, make sure the number of bytes read/written matches the variable definition. Use `mov al, ` to read 1 byte, and `mov eax, ` to read 4 bytes. NASM will check if the operand sizes match. * * * ## Reserving Uninitialized Space in .bss Segment Use RESB, RESW, RESD and other pseudo-instructions to reserve space (uninitialized): | Pseudo-instruction | Reserved Size | Example | | --- | --- | --- | | RESB | Byte | `buffer resb 256` | | RESW | Word (2 bytes) | `wbuf resw 100` | | RESD | Doubleword (4 bytes) | `dbuf resd 50` | | RESQ | Quadword (8 bytes) | `qbuf resq 25` | ## Example ; File path: variables_bss.asm ; Demo reserving space in .bss segment section.bss input_buf resb 128; Reserve 128 bytes for input buffer numbers resd 100; Reserve 100 doublewords (400 bytes) temp resb 1; Reserve 1 byte for temporary variable section.data prompt db'Enter a number: ' prompt_len equ$- prompt section.text global _start _start: ; Output prompt mov eax,4 mov ebx,1 mov ecx, prompt mov edx, prompt_len int 0x80 ; Read input into buffer in .bss segment mov eax,3 mov ebx,0 mov ecx, input_buf ; Use space reserved in .bss segment mov edx,128 int 0x80 ; Fill data into numbers array mov dword,42; numbers = 42 mov dword[numbers +4],100; numbers = 100 mov eax,1 mov ebx,0 int 0x80 * * * ## Memory Layout of Byte, Word, Doubleword x86 uses Little Endianβ€”the low-order byte is stored at the low address. For example, when defining `value dd 0x12345678`, the memory layout is: Address: [value+1] [value+2] [value+3] Content: 0x78 0x56 0x34 0x12 ## Example ; File path: endianness.asm ; Demo little-endian storage section.data value dd 0x12345678; Doubleword, 4 bytes section.text global _start _start: ; Read the same variable with different sizes mov eax,; Read 4 bytes: eax = 0x12345678 mov ax,; Read 2 bytes: ax = 0x5678 (low 2 bytes) mov al,; Read 1 byte: al = 0x78 (lowest 1 byte) ; Verify little-endian: low address stores low byte mov al,; al = 0x78 mov bl,[value +1]; bl = 0x56 mov cl,[value +2]; cl = 0x34 mov dl,[value +3]; dl = 0x12 mov eax,1 mov ebx,0 int 0x80 * * * ## Variable Initialization and Access Variable operations in assembly follow the "load-compute-store" three-step pattern: ## Example ; File path: var_operations.asm ; Demo the three-step pattern for variable operations section.data a dd 100 b dd 200 result dd 0 section.text global _start _start: ; result = a + b ; Step 1: load mov eax,; Load a into eax ; Step 2: compute add eax,; eax = eax + b ; Step 3: store mov,eax; Store result to result ; result = a * 2 - b mov eax, imul eax,2; eax = a *
← Assembly ArithmeticAssembly Syscall β†’