📌 Assembly Language Syntax Structure
Assembly language is a “machine-oriented” low-level language that corresponds directly to machine instructions. Different CPU architectures (such as x86, ARM, RISC-V) have different assembly syntax, but the core structure is generally similar.
An assembly source program typically consists of “pseudo-instructions, data definitions, and instruction statements”.
1️⃣ Basic Structure of an Assembly Program
A typical assembly program usually contains three parts:
-
“Data Segment” is used to store constants, variables, strings, and other data.
section .data msg db 'Hello, World!', 0 ; Define a null-terminated string num dw 10 ; Define a 16-bit integer -
“Code Segment” contains executable instructions.
section .text global _start ; Program entry point (Linux) _start: mov eax, 1 ; System call number: write mov ebx, 1 ; File descriptor: stdout mov ecx, msg ; Buffer address mov edx, 13 ; Length int 0x80 ; Call kernel -
“Stack Segment (optional)” is used for function calls, parameter passing, and local variable storage. (Most modern assembly automatically uses the stack, no separate definition needed)
2️⃣ Basic Form of Assembly Statements
An assembly statement typically consists of four parts:
[label] mnemonic operand(s) ; comment
-
“Label (optional)” serves as an “anchor” in the program for jumps or references.
loop_start: dec ecx jnz loop_start -
“Mnemonic” is the name of the instruction, serving as the “code name” for machine instructions. For example:
<span>MOV</span>,<span>ADD</span>,<span>JMP</span>. -
“Operand(s) (optional)” are the objects of the instruction operation, which can be registers, memory addresses, or immediate values.
mov eax, ebx ; Assign the value of ebx to eax add eax, 5 ; eax = eax + 5 mov [num], eax ; Store eax into variable num -
“Comment (optional)” starts with
<span>;</span>and explains the code.
3️⃣ Common Pseudo-Instructions
Pseudo-instructions are not machine instructions but are “commands” for the assembler, used to assist programming.
<span>db</span>(define byte): defines byte data<span>dw</span>(define word): defines 2-byte data<span>dd</span>(define double word): defines 4-byte data<span>equ</span>: defines constants<span>section</span>: defines segments (code segment/data segment)<span>global</span>: declares global symbols
4️⃣ Instruction Classification
- “Data Transfer Instructions”
<span>MOV</span>,<span>PUSH</span>,<span>POP</span>,<span>XCHG</span> - “Arithmetic Instructions”
<span>ADD</span>,<span>SUB</span>,<span>MUL</span>,<span>DIV</span>,<span>INC</span>,<span>DEC</span> - “Logical Instructions”
<span>AND</span>,<span>OR</span>,<span>XOR</span>,<span>NOT</span>,<span>SHL</span>,<span>SHR</span> - “Control Transfer Instructions”
<span>JMP</span>,<span>JZ</span>,<span>JNZ</span>,<span>CALL</span>,<span>RET</span> - “Input/Output Instructions” (available in certain architectures, such as x86’s
<span>IN</span>,<span>OUT</span>)
5️⃣ A Complete Example (x86, Linux)
section .data
msg db "Hello, Assembly!", 0xA ; Define string
len equ $ - msg ; Calculate string length
section .text
global _start
_start:
mov eax, 4 ; sys_write
mov ebx, 1 ; File descriptor: stdout
mov ecx, msg ; Buffer address
mov edx, len ; Length
int 0x80 ; System call
mov eax, 1 ; sys_exit
xor ebx, ebx ; Return 0
int 0x80
Output: 👉 <span>Hello, Assembly!</span>