Assembly Language - Basic Syntax
This chapter introduces the basic structure, syntax rules, and writing conventions of a NASM assembly program, helping you understand the skeleton of assembly code.
Basic structure of an assembly program
A complete NASM assembly program usually consists of the following parts:
Example
; File path: structure.asm
; Example of basic NASM program structure
section
.data
; Data segment: stores initialized data
; Define variables and constants here
msg
db
'Hello, RUNOOB!'
,
0xA
len
equ
$
msg
section
.bss
; BSS segment: stores uninitialized data
; Reserve memory space here
buffer
resb
64
; Reserve a 64-byte buffer
section
.text
; Code segment: stores executable instructions
global
_start
_start
:
; Write the program logic here
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
msg
mov
edx
,
len
int
0x80
mov
eax
,
1
mov
ebx
,
0
int
0x80
| Segment (Section) | Purpose | Feature |
|---|---|---|
.data |
Stores initialized global variables and constants | Size and content are determined at compile time and stored in the executable file |
.bss |
Stores uninitialized global variables | Space is allocated only at runtime and does not take up executable file size |
.text |
Stores executable machine instructions | Read-only, contains all the logic code of the program |
At least the
.textsegment is required to form a valid assembly program. If there is no data, you can omit the.dataand.bsssegment.
Assembly statement format
The general format of each assembly statement is:
[标签:] 指令助记符 [操作数1 [, 操作数2 [, 操作数3]]] [; 注释]
Description of each part:
| Section | Required | Description |
|---|---|---|
| Label | Optional | A symbolic name representing a memory address, ending with a colon |
| Instruction mnemonic | Must | Such as mov, add, sub, etc., telling the CPU what to do |
| operands | Optional (some instructions have no operands) | The data objects the instruction operates on, which can be registers, memory addresses, or immediate values |
| Notes | Optional | Starts with a semicolon and continues to the end of the line |
Example
; Examples of various statement formats
; Instruction only, no operands
ret
; Return from a subroutine
; Instruction + single operand
push
eax
; Push the value of eax onto the stack
inc
ecx
; Add 1 to ecx
; Instruction + two operands (most common)
mov
eax
,
42
; Copy 42 into the eax register
add
ebx
,
ecx
; ebx = ebx + ecx
; With a label
loop_start
:
; Label: marks the start of the loop
dec
ecx
; Subtract 1 from ecx
jnz
loop_start
; If ecx is not 0, jump back to loop_start
Comment conventions
NASM uses a semicolon (;) to indicate a comment; everything from the semicolon to the end of the line is ignored by the assembler.
Example
; Full-line comment: explains the purpose of the code block below
; Compute the sum of two numbers and output the result
mov
eax
,
10
; Inline comment: put 10 into eax
add
eax
,
20
; Inline comment: add 20 to eax, now eax = 30
Comments are extremely important in assembly code. Without comments, even the author may not understand the assembly code after a few weeks. Get into the habit of writing a comment for every instruction.
Identifier naming rules
Identifiers (labels, variable names, constant names, etc.) must follow these rules:
| Rule | Description |
|---|---|
| Allowed characters | Letters, digits, underscore _, dot ., question mark ?, @, $, #, etc. |
| Starting character | Must start with a letter, underscore, dot, or question mark; cannot start with a digit |
| Case | Case-sensitive by default (can be changed via assembler options) |
| Reserved words | Cannot share a name with instruction mnemonics, register names, or NASM keywords |
Example
; Valid identifiers
my_variable
:
; Starts with a letter + underscore
.
loop_start
:
; Starts with a dot (local label)
?error_handler
:
; Starts with a question mark
counter2
:
; Letter + digit
; Invalid identifiers (for reference only, do not use)
; 1st_value: ; Error: cannot start with a number
; mov: ; Error: mov is a reserved word
; my-variable: ; Error: the minus sign is not a valid character
Directives
A directive is a command to the assembler, not an instruction to the CPU; it controls the assembly process and defines data structures.
| Directive | Purpose | Example |
|---|---|---|
db |
Define byte (1 byte) | byte_val db 0x55 |
dw |
Define word (2 bytes) | word_val dw 0x1234 |
dd |
Define double word (4 bytes) | dword_val dd 0x12345678 |
equ |
Defining Constants | MAX_SIZE equ 256 |
resb |
Reserve byte space | buffer resb 128 |
resw |
Reserve word space | wbuf resw 64 |
resd |
Reserve double-word space | dbuf resd 32 |
%define |
Macro-defined constant | %define COUNT 10 |
Case conventions
By default, NASM is case-sensitive for labels and identifiers:
Example
; Example of case sensitivity
section
.data
msg
db
'RUNOOB'
,
0
; Define the variable msg
section
.text
global
_start
_start
:
mov
eax
,
MSG
; Error: MSG and msg are different (unless case-insensitivity is enabled)
mov
eax
,
msg
; Correct: msg matches the definition exactly
MOV
EAX
,
42
; Valid syntax: instruction mnemonics are case-insensitive
mov
eax
,
42
; Recommended style: use lowercase for better readability
Instruction mnemonics and register names are case-insensitive (
MOV,Mov,movhas the same effect), but the recommended style is to use lowercase consistently.
Numeric representation
NASM supports numeric literals in multiple bases:
Example
; Representation of different bases in NASM
mov
eax
,
42
; Decimal: write the number directly
mov
eax
,
0x2A
; Hexadecimal: 0x prefix (recommended)
mov
eax
,
2Ah
; Hexadecimal: h suffix
mov
eax
,
0o52
; Octal: 0o prefix
mov
eax
,
52o
; Octal: o suffix
mov
eax
,
101010b
; Binary: b suffix
mov
eax
,
0b101010
; Binary: 0b prefix
it is recommended to use
0xthe prefix denotes hexadecimal (e.g.0x2A), so it is not easily confused with a label.
A complete syntax example
The following program uses the above syntax elements together to compute the sum from 1 to 10 and output it:
Example
; File path: sum.asm
; Compute 1+2+...+10 and output the result character
section
.data
result
db
0
; Store the computation result (1 byte)
newline
db
0xA
; Newline character
section
.text
global
_start
_start
:
; Initialize registers and variables
mov
ecx
,
10
; Loop counter: count down from 10
mov
eax
,
0
; eax holds the accumulated sum, initially 0
sum_loop
:
; Loop start label
add
eax
,
ecx
; eax = eax + ecx
dec
ecx
; Subtract 1 from ecx
jnz
sum_loop
; If ecx != 0, continue the loop
; At this point eax = 55 (10+9+...+1)
add
eax
,
'0'
; Convert the number to an ASCII character ('0'=48, 55+48=103='g', wrong)
; An actual demo needs a more complex conversion, see later chapters
; Here we only output result (simplified demo)
mov
[
result
]
,
al
; Store the accumulated result into result
; Exit the program
mov
eax
,
1
mov
ebx
,
0
int
0x80
Note: in the example above, directly adding '0' is only correct when the number is in the range 0-9. Converting numbers with two or more digits will be explained in detail in later chapters.