Data Transfer Instructions
Used to move data between registers, memory, and I/O ports.
| Instruction | Function Description | Example |
|---|---|---|
| MOV | Copies the source operand to the destination operand (does not change the source) | MOV EAX, 5 (EAX ← 5)MOV EBX, [ESI] (EBX ← Memory[ESI]) |
| PUSH | Pushes an operand onto the top of the stack (SP decreases by 4) | PUSH EAX (Stack Top ← EAX, SP = SP-4) |
| POP | Pops data from the top of the stack to the operand (SP increases by 4) | POP EBX (EBX ← Stack Top, SP = SP+4) |
| LEA | Loads effective address (stores memory address into a register) | LEA EAX, [EBX+4*ECX] (EAX ← EBX+4*ECX) |
Arithmetic Instructions
Used to perform mathematical operations such as addition, subtraction, multiplication, and division.
| Instruction | Function Description | Example |
|---|---|---|
| ADD | Addition (Destination = Destination + Source) | ADD EAX, EBX (EAX = EAX + EBX) |
| SUB | Subtraction (Destination = Destination – Source) | SUB ECX, 10 (ECX = ECX – 10) |
| MUL | Unsigned Multiplication (Result stored in EAX/EDX:EAX) | MUL EBX (EAX = EAX * EBX, EDX:EAX for high bits) |
| DIV | Unsigned Division (Quotient in EAX, Remainder in EDX) | DIV ECX (EAX = EDX:EAX / ECX, EDX = Remainder) |
| INC | Increment by 1 | INC EDX (EDX = EDX + 1) |
| DEC | Decrement by 1 | DEC ECX (ECX = ECX – 1) |
Add and Subtract Instructions
ADD Instruction:
- Function: Adds the source operand to the destination operand.
- Format:
ADD dest, src - Flags Affected: ZF, SF, OF, CF, PF, AF
mov eax, 10 ; eax = 10
add eax, 5 ; eax = eax + 5 = 15
add byte [var], 1 ; Add 1 to memory variable
SUB Instruction:
- Function: Subtracts the source operand from the destination operand.
- Format:
SUB dest, src - Flags Affected: ZF, SF, OF, CF, PF, AF
mov ebx, 20 ; ebx = 20
sub ebx, 8 ; ebx = ebx - 8 = 12
sub word [counter], 1 ; Subtract 1 from memory variable
Multiply and Divide Instructions
MUL Instruction (Unsigned Multiplication):
- Function: Multiplies AL/AX/EAX with the source operand.
- Result Storage:
- 8-bit operand: AX = AL * src
- 16-bit operand: DX:AX = AX * src
- 32-bit operand: EDX:EAX = EAX * src
- Flags Affected: CF, OF
mov al, 5 ; AL = 5
mov bl, 10 ; BL = 10
mul bl ; AX = AL * BL = 0x0032 (50)
DIV Instruction (Unsigned Division):
- Function: Divides DX:AX/EDX:EAX by the source operand.
- Result Storage:
- 8-bit operand: AL = Quotient, AH = Remainder
- 16-bit operand: AX = Quotient, DX = Remainder
- 32-bit operand: EAX = Quotient, EDX = Remainder
- Flags Affected: None (but source cannot be 0)
mov ax, 50 ; AX = 50
mov bl, 10 ; BL = 10
div bl ; AL = 5 (Quotient), AH = 0 (Remainder)
Logical Instructions
Used for bit-level operations (AND, OR, NOT, XOR).
| Instruction | Function Description | Example |
|---|---|---|
| AND | Bitwise AND (Destination = Destination & Source) | AND EAX, 0xFF (EAX keeps lower 8 bits) |
| OR | Bitwise OR (Destination = Destination | Source) |
| XOR | Bitwise XOR (Destination = Destination ^ Source) | XOR ECX, ECX (Clears ECX) |
| NOT | Bitwise NOT (Destination = ~Destination) | NOT EAX (Inverts every bit of EAX) |
| TEST | Tests bits (Does not affect destination, only sets flags) | TEST EAX, 0x80 (Checks if MSB of EAX is 1) |
Basic Logical Operations
AND Instruction:
- Function: Bitwise AND operation.
- Format:
AND dest, src - Flags Affected: ZF, SF, PF, CF=0, OF=0
mov al, 0b11001100
and al, 0b10101010 ; AL = 0b10001000 (136)
OR Instruction:
- Function: Bitwise OR operation.
- Format:
OR dest, src - Flags Affected: ZF, SF, PF, CF=0, OF=0
mov bl, 0b00110011
or bl, 0b11001100 ; BL = 0b11111111 (255)
XOR Instruction:
- Function: Bitwise XOR operation.
- Format:
XOR dest, src - Characteristics: Result is 0 if operands are same, 1 otherwise.
- Flags Affected: ZF, SF, PF, CF=0, OF=0
mov cl, 0b10101010
xor cl, 0b10101010 ; CL = 0 (XORing a value with itself results in 0)
NOT Instruction:
- Function: Bitwise NOT operation.
- Format:
NOT dest - Characteristics: Does not affect flags.
mov dl, 0b00001111
not dl ; DL = 0b11110000
Shift Instructions
Basic Shift Operations
SHL Instruction (Logical Left Shift):
- Function: Shifts left by specified number of bits, filling right with 0s.
- Format:
SHL dest, count - Flags Affected: CF = Last bit shifted out, OF = When count=1, check MSB change.
mov al, 0b00001111
shl al, 2 ; AL = 0b00111100 (60), CF=1
SHR Instruction (Logical Right Shift):
- Function: Shifts right by specified number of bits, filling left with 0s.
- Format:
SHR dest, count - Flags Affected: CF = Last bit shifted out, OF = When count=1, check MSB change.
mov bl, 0b11110000
shr bl, 3 ; BL = 0b00011110 (30), CF=0
ROL Instruction (Rotate Left):
- Function: Rotates left by specified number of bits.
- Format:
ROL dest, count - Flags Affected: CF = Last bit rotated out.
mov cl, 0b10000001
rol cl, 1 ; CL = 0b00000011 (3), CF=1
ROR Instruction (Rotate Right):
- Function: Rotates right by specified number of bits.
- Format:
ROR dest, count - Flags Affected: CF = Last bit rotated out.
mov dl, 0b11000000
ror dl, 2 ; DL = 0b00110000 (48), CF=0
String Operation Instructions
Used for efficient processing of contiguous data in memory (e.g., copying, comparing strings).
| Instruction | Function Description | Example |
|---|---|---|
| MOVSB | Copies a byte from [DS:SI] to [ES:DI]; SI/DI auto-increment/decrement (controlled by DF flag) | MOVSB (Copies 1 byte) |
| CMPSB | Compares bytes at [DS:SI] and [ES:DI]; SI/DI auto-increment/decrement | CMPSB (Compares 1 byte) |
| STOSB | Stores the byte in AL to [ES:DI]; DI auto-increment/decrement | STOSB (Stores 1 byte to ES:DI) |
| LODSB | Loads a byte from [DS:SI] into AL; SI auto-increment/decrement | LODSB (Loads 1 byte from DS:SI into AL) |
Compare and Test Instructions
Compare Instruction
CMP Instruction:
- Function: Compares two operands (dest – src), but does not save the result.
- Format:
CMP dest, src - Flags Affected: ZF, SF, OF, CF, PF, AF
mov eax, 10
mov ebx, 20
cmp eax, ebx ; Calculates EAX-EBX, sets flags
je equal_label ; Jump if equal
TEST Instruction:
- Function: Performs bitwise AND but does not save the result.
- Format:
TEST dest, src - Typical Use: Testing if specific bits are set.
- Flags Affected: ZF, SF, PF, CF=0, OF=0
mov al, 0b00001111
test al, 0b00000001 ; Test if LSB is 1
jnz bit_set ; Jump if ZF=0
Program Flow Control Instructions
Used to change program execution flow (e.g., jumps, subroutine calls).
| Instruction | Function Description | Example |
|---|---|---|
| JMP | Unconditional jump to target address | JMP LABEL (Jump to execute at LABEL) |
| JZ/JE | Jump if Zero Flag ZF=1 (Jump if equal) | CMP EAX, EBX (Compare EAX and EBX)JZ EQUAL (Jump if equal) |
| JNZ/JNE | Jump if Zero Flag ZF=0 (Jump if not equal) | JNZ NOT_EQUAL (Jump if not equal) |
| JC/JNC | Jump if Carry Flag CF=1/0 | ADD EAX, 100 (May produce carry)JC CARRY (Jump if carry) |
| CALL | Call subroutine (Push return address to stack, jump to function entry) | CALL MY_FUNC (Execute MY_FUNC then return) |
| RET | Return from subroutine (Pop return address from stack, jump to that address) | RET (Return to next instruction after CALL) |
Conditional Jump Instructions
Equal/Not Equal Jumps:
cmp eax, ebx
je equal_label ; Jump if ZF=1 (equal)
jne not_equal ; Jump if ZF=0 (not equal)
Unsigned Comparison Jumps:
cmp ecx, edx
ja above_label ; CF=0 and ZF=0 (greater than)
jb below_label ; CF=1 (less than)
jae above_or_equal ; CF=0 (greater than or equal)
jbe below_or_equal ; CF=1 or ZF=1 (less than or equal)
Signed Comparison Jumps:
cmp esi, edi
jg greater_label ; SF=OF and ZF=0 (greater than)
jl less_label ; SF≠OF (less than)
jge greater_or_equal ; SF=OF (greater than or equal)
jle less_or_equal ; SF≠OF or ZF=1 (less than or equal)
Jump Instruction Table:
| Instruction | Meaning | Condition |
|---|---|---|
| JE/JZ | Equal / Zero | ZF=1 |
| JNE/JNZ | Not Equal / Not Zero | ZF=0 |
| JA/JNBE | Unsigned Greater Than | CF=0 and ZF=0 |
| JB/JNAE | Unsigned Less Than | CF=1 |
| JG/JNLE | Signed Greater Than | SF=OF and ZF=0 |
| JL/JNGE | Signed Less Than | SF≠OF |
| JGE/JNL | Signed Greater Than or Equal | SF=OF |
| JLE/JNG | Signed Less Than or Equal | SF≠OF or ZF=1 |
Instruction Usage Examples
Comprehensive Application Example
Calculate Sum of Array Elements:
section .data
array dd 1, 2, 3, 4, 5
len equ ($ - array) / 4
sum dd 0
section .text
global _start
_start:
mov ecx, len ; Loop counter
mov esi, array ; Array pointer
xor eax, eax ; Clear accumulator
sum_loop:
add eax, [esi] ; Accumulate array element
add esi, 4 ; Move pointer to next element
loop sum_loop ; ECX decrements, continue if not 0
mov [sum], eax ; Store result
; Exit program
mov eax, 1 ; sys_exit
xor ebx, ebx ; Return code 0
int 0x80
Bit Manipulation Example:
; Check if 3rd bit is set
mov al, 0b00001000
test al, 0b00001000
jnz bit3_set
; Set 5th bit
or al, 0b00100000
; Clear 2nd bit
and al, 0b11111101
; Toggle 4th bit
xor al, 0b00001000
By mastering these basic instructions, you can build complete assembly programs, ranging from simple calculations to complex system functionalities. It is recommended to use debugging tools like GDB during practice to observe changes in registers and memory, deepening your understanding of the instruction execution process.



