Assembly Basic Syntax
## Assembly Language - Basic Syntax\\n\\nThis chapter introduces the basic structure, syntax rules, and writing conventions of the NASM assembly program, helping you understand the skeleton of assembly code.\\n\\n* * *\\n\\n## Basic Structure of an Assembly Program\\n\\nA complete NASM assembly program usually consists of the following parts:\\n\\n## Example\\n\\n; File path: structure.asm\\n\\n; NASM Basic program structure example\\n\\nsection.data; Data Section: Stores initialized data\\n\\n; Define variables and constants here\\n\\n msg db'Hello, TUTORIAL!',0xA\\n\\n len equ$- msg\\n\\nsection.bss; BSS SectionοΌStores uninitialized data\\n\\n; Reserve memory space here\\n\\n buffer resb 64; Reserve a 64-byte buffer\\n\\nsection.text; Code Section: Stores executable instructions\\n\\nglobal _start\\n\\n_start:\\n\\n; Write program logic here\\n\\nmov eax,4\\n\\nmov ebx,1\\n\\nmov ecx, msg\\n\\nmov edx, len\\n\\nint 0x80\\n\\nmov eax,1\\n\\nmov ebx,0\\n\\nint 0x80\\n\\n| Section | Purpose | Characteristics |\\n| --- | --- | --- |\\n| `.data` | Stores initialized global variables and constants | Size and content determined at compile time, stored in the executable file |\\n| `.bss` | Stores uninitialized global variables | Space allocated only at runtime, does not increase executable file size |\\n| `.text` | Stores executable machine instructions | Read-only, contains all the program's logic code |\\n\\n> At least the `.text` section is required to form a valid assembly program. If there is no data, the `.data` and `.bss` sections can be omitted.\\n\\n* * *\\n\\n## Assembly Statement Format\\n\\nThe general format for each assembly statement is:\\n\\n[Label:] Instruction Mnemonic [Operand1 [, Operand2 [, Operand3]]] [; Comment]\\nExplanation of each part:\\n\\n| Part | Required | Description |\\n| --- | --- | --- |\\n| Label | Optional | A symbolic name representing a memory address, ending with a colon |\\n| Instruction Mnemonic | Required | Such as mov, add, sub, etc., telling the CPU what to do |\\n| Operand | Optional (some instructions have no operands) | The data object the instruction operates on, can be a register, memory address, or immediate value |\\n| Comment | Optional | Starts with a semicolon and continues to the end of the line |\\n\\n## Example\\n\\n; Examples of various statement formats\\n\\n; Instruction only, no operand\\n\\nret; FromSubroutine return\\n\\n; Instruction + single operand\\n\\npush eax; Set eax Push the value onto the stack\\n\\ninc ecx; ecx Add 1\\n\\n; Instruction + two operands (most common)\\n\\nmov eax,42; Set 42 Copy to eax register\\n\\nadd ebx,ecx; ebx = ebx + ecx\\n\\n; With label\\n\\n loop_start:; Label: marks the start of the loop\\n\\ndec ecx; ecx Subtract 1\\n\\njnz loop_start ; If ecx is not 0, jump back to loop_start\\n\\n* * *\\n\\n## Comment Conventions\\n\\nNASM uses a semicolon (;) to indicate a comment. Everything from the semicolon to the end of the line is ignored by the assembler.\\n\\n## Example\\n\\n; Full-line comment: Explain the purpose of the code block below\\n\\n; Calculate the sum of two numbers and output the result\\n\\nmov eax,10; Inline comment: Set 10 into eax\\n\\nadd eax,20; Inline comment: Add 20 to eax, now eax = 30\\n\\n> Comments are extremely important in assembly code. Without comments, even the author might not understand their own assembly code after a few weeks. Develop the habit of writing comments for every instruction.\\n\\n* * *\\n\\n## Identifier Naming Rules\\n\\nIdentifiers (labels, variable names, constant names, etc.) must follow these rules:\\n\\n| Rule | Description |\\n| --- | --- |\\n| Composing characters | Letters, numbers, underscore _, dot ., question mark ?, @, $, #, etc. |\\n| Starting character | Must start with a letter, underscore, dot, or question mark; cannot start with a number |\\n| Case sensitivity | Case-sensitive by default (can be modified via compilation options) |\\n| Reserved words | Cannot have the same name as instruction mnemonics, register names, or NASM keywords |\\n\\n## Example\\n\\n; Valid identifier\\n\\n my_variable:; Starts with letter + underscore\\n\\n.loop_start:; Starts with a dot (local label)\\n\\n ?error_handler:; Starts with a question mark\\n\\n counter2:; Letter + Number\\n\\n; Invalid identifiers (for reference only, do not use)\\n\\n; 1st_value: ; Error: Cannot start with Number\\n\\n; mov: ; Error: mov is a reserved word\\n\\n; my-variable: ; Error: hyphen is not a valid character\\n\\n* * *\\n\\n## Pseudo-Instructions (Directives)\\n\\nPseudo-instructions are commands for the assembler, not instructions for the CPU, used to control the assembly process and define data structures.\\n\\n| Directive | Purpose | Example |\\n| --- | --- | --- |\\n| `db` | Define byte (1 byte) | `byte_val db 0x55` |\\n| `dw` | Define word (2 bytes) | `word_val dw 0x1234` |\\n| `dd` | Define double word (4 bytes) | `dword_val dd 0x12345678` |\\n| `equ` | Define constant | `MAX_SIZE equ 256` |\\n| `resb` | Reserve byte space | `buffer resb 128` |\\n| `resw` | Reserve word space | `wbuf resw 64` |\\n| `resd` | Reserve double word space | `dbuf resd 32` |\\n| `%define` | Macro define constant | `%define COUNT 10` |\\n\\n* * *\\n\\n## Case Conventions\\n\\nNASM is case-sensitive for labels and identifiers by default:\\n\\n## Example\\n\\n; Case-sensitive example\\n\\nsection.data\\n\\n msg db'TUTORIAL',0; Define variable msg\\n\\nsection.text\\n\\nglobal _start\\n\\n_start:\\n\\nmov eax, MSG ; Error: MSG and msg are different (unless case-insensitive mode is enabled)\\n\\nmov eax, msg ; Correct: msg matches the definition exactly\\n\\nMOV EAX,42; Syntax correct: Instruction mnemonics are case-insensitive\\n\\nmov eax,42; Recommended: use lowercase for better readability\\n\\n> Instruction mnemonics and register names are case-insensitive (`MOV`, `Mov`, `mov` have the same effect), but the recommended style is to use consistent lowercase.\\n\\n* * *\\n\\n## Numeric Representation\\n\\nNASM supports numeric representations in various bases:\\n\\n## Example\\n\\n; NASM Representation of different number bases in\\n\\nmov eax,42; Decimal: Write Number directly\\n\\nmov eax,0x2A; Hexadecimal: 0x prefix (recommended)\\n\\nmov eax,2Ah; Hexadecimal: h suffix\\n\\nmov eax, 0o52 ; Octal: 0o prefix\\n\\nmov eax,52o; Octal: o suffix\\n\\nmov eax,101010b; Binary: b suffix\\n\\nmov eax, 0b101010 ; Binary: 0b prefix\\n\\n> It is recommended to use the `0x` prefix for hexadecimal (e.g., `0x2A`), as this makes it less likely to be confused with labels.\\n\\n* * *\\n\\n## A Complete Syntax Example\\n\\nThe following program comprehensively uses the above syntax elements to calculate the sum of 1 to 10 and output it:\\n\\n## Example\\n\\n; File path: sum.asm\\n\\n; Calculate 1+2+...+10 And output the result character\\n\\nsection.data\\n\\n result db 0; Store calculation result (1 byte)\\n\\n newline db 0xA; Newline character\\n\\nsection.text\\n\\nglobal _start\\n\\n_start:\\n\\n; Initialize registers and variables\\n\\nmov ecx,10; Loop counter: Count down from 10\\n\\nmov eax,0; eax Stores the cumulative sum, initialized to 0\\n\\nsum_loop:; Loop start label\\n\\nadd eax,ecx; eax = eax + ecx\\n\\ndec ecx; ecx Subtract 1\\n\\njnz sum_loop ; If ecx != 0οΌContinue loop\\n\\n; At this point eax = 55οΌ10+9+...+1οΌ\\n\\nadd eax,'0'; SetNumberConvert to ASCII character ('0'=48οΌ55+48=103='g'οΌIncorrect)\\n\\n; Actual demonstration requires more complex conversion, see subsequent chapters\\n\\n; Only output result here (simplified demo)\\n\\nmov,al; SetStore the accumulated result in result\\n\\n; Exit program\\n\\nmov eax,1\\n\\nmov ebx,0\\n\\nint 0x80\\n\\n> Note: In the example above, directly adding '0' is only correct if the number is in the range 0-9. Handling the conversion of two-digit numbers and above will be explained in detail in subsequent chapters.
YouTip