Assembly language syntax is the set of rules and conventions used to write programs in assembly language. It forms the foundation for creating low-level code that directly interacts with a computer's hardware.
Assembly language programs typically consist of three main sections:
Assembly instructions generally follow this format:
[label:] mnemonic [operands] [;comment]
Here's a simple example of assembly code that adds two numbers:
section .data
num1 db 5
num2 db 3
result db 0
section .text
global _start
_start:
mov al, [num1] ; Load first number into AL register
add al, [num2] ; Add second number to AL
mov [result], al ; Store result in memory
; Exit program
mov eax, 1
xor ebx, ebx
int 0x80
Registers are fast storage locations within the CPU. They are referenced directly in assembly code:
mov eax, 42 ; Move value 42 into EAX register
add ebx, eax ; Add value in EAX to EBX
For more details on registers, see Assembly Registers.
Assembly language provides various ways to access memory:
mov al, [variable] ; Direct addressing
mov bl, [eax + 4] ; Indirect addressing with offset
mov cx, [ebx + esi*2] ; Scaled index addressing
Learn more about Assembly Memory Addressing Modes.
Directives are instructions for the assembler, not the CPU. They define data, sections, and other assembly-time behaviors:
section .data
message db 'Hello, World!', 0
number dw 42
Understanding assembly language syntax is crucial for writing efficient, low-level code. As you delve deeper into assembly programming, explore topics like Assembly Arithmetic Operations and Assembly Logical Operations to expand your capabilities.