This guide provides an overview of x86 assembly language, including a comprehensive list of common instructions, an explanation of CPU registers, and a simple example program in assembly.
The x86 architecture is a family of instruction set architectures based on the Intel 8086 CPU. It includes registers, a well-defined instruction set, and a variety of addressing modes.
x86 has several registers that are categorized as general-purpose, segment, index/pointer, and control registers:
Register | Purpose | Notes |
---|---|---|
EAX |
Accumulator for operands | Used in arithmetic operations. |
EBX |
Base register | Used in addressing memory. |
ECX |
Counter for loops | Used for iteration. |
EDX |
Data register | Used in I/O and arithmetic. |
Register | Purpose |
---|---|
CS |
Code Segment |
DS |
Data Segment |
ES |
Extra Segment |
SS |
Stack Segment |
Register | Purpose |
---|---|
ESI |
Source index for string ops |
EDI |
Destination index for string ops |
ESP |
Stack Pointer |
EBP |
Base Pointer |
Here is a categorized list of commonly used x86 instructions:
MOV
- Move dataPUSH
- Push data onto the stackPOP
- Pop data off the stackLEA
- Load effective addressXCHG
- Exchange data
ADD
- AddSUB
- SubtractMUL
- Multiply (unsigned)IMUL
- Multiply (signed)DIV
- Divide (unsigned)IDIV
- Divide (signed)INC
- IncrementDEC
- Decrement
AND
- Bitwise ANDOR
- Bitwise ORXOR
- Bitwise XORNOT
- Bitwise NOTSHL
- Shift leftSHR
- Shift rightCMP
- Compare two operands
JMP
- Unconditional jumpJE
/JZ
- Jump if equal/zeroJNE
/JNZ
- Jump if not equal/not zeroJG
/JNLE
- Jump if greaterJL
/JNGE
- Jump if lessCALL
- Call a procedureRET
- Return from a procedure
Below is a basic program written in x86 assembly language that demonstrates moving data, arithmetic operations, and using loops.
section .data ; Data segment
result db 0 ; Store the result here
section .bss ; Uninitialized data
temp resb 1
section .text ; Code segment
global _start ; Entry point for the program
_start:
mov ecx, 10 ; Set up the loop counter (1 to 10)
xor eax, eax ; Clear the accumulator (EAX = 0)
xor ebx, ebx ; EBX will hold the sum
loop_start:
add ebx, ecx ; Add the current value of ECX to EBX
loop loop_start ; Decrement ECX and repeat until ECX = 0
; Store the result in memory
mov [result], bl ; Move the lower byte of EBX to the result
; Exit the program
mov eax, 1 ; System call number for exit
int 0x80 ; Call the kernel
- Data Section:
- The
result
variable is declared to store the final sum.
- The
- Initialization:
ECX
is set to 10 (the loop counter).EAX
andEBX
are cleared to prepare for arithmetic.
- Loop:
- The
ADD
instruction adds the current value ofECX
toEBX
. - The
LOOP
instruction decrementsECX
and jumps to the label ifECX
is not zero.
- The
- Exit:
- The program exits gracefully by invoking a system call (
int 0x80
).
- The program exits gracefully by invoking a system call (
To assemble and run the above program:
- Save the code to a file (e.g.,
sum.asm
). - Use the NASM assembler to assemble the code:
nasm -f elf32 sum.asm
- Link the object file using the ld linker:
ld -m elf_i386 -o sum sum.o
- Run the program:
./sum