Lesson 02-Basic Instructions

Data Transfer Instructions

Used to move data between registers, memory, and I/O ports.

InstructionFunction DescriptionExample
MOVCopies the source operand to the destination operand (does not change the source)MOV EAX, 5 (EAX ← 5)
MOV EBX, [ESI] (EBX ← Memory[ESI])
PUSHPushes an operand onto the top of the stack (SP decreases by 4)PUSH EAX (Stack Top ← EAX, SP = SP-4)
POPPops data from the top of the stack to the operand (SP increases by 4)POP EBX (EBX ← Stack Top, SP = SP+4)
LEALoads 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.

InstructionFunction DescriptionExample
ADDAddition (Destination = Destination + Source)ADD EAX, EBX (EAX = EAX + EBX)
SUBSubtraction (Destination = Destination – Source)SUB ECX, 10 (ECX = ECX – 10)
MULUnsigned Multiplication (Result stored in EAX/EDX:EAX)MUL EBX (EAX = EAX * EBX, EDX:EAX for high bits)
DIVUnsigned Division (Quotient in EAX, Remainder in EDX)DIV ECX (EAX = EDX:EAX / ECX, EDX = Remainder)
INCIncrement by 1INC EDX (EDX = EDX + 1)
DECDecrement by 1DEC 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).

InstructionFunction DescriptionExample
ANDBitwise AND (Destination = Destination & Source)AND EAX, 0xFF (EAX keeps lower 8 bits)
ORBitwise OR (Destination = DestinationSource)
XORBitwise XOR (Destination = Destination ^ Source)XOR ECX, ECX (Clears ECX)
NOTBitwise NOT (Destination = ~Destination)NOT EAX (Inverts every bit of EAX)
TESTTests 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).

InstructionFunction DescriptionExample
MOVSBCopies a byte from [DS:SI] to [ES:DI]; SI/DI auto-increment/decrement (controlled by DF flag)MOVSB (Copies 1 byte)
CMPSBCompares bytes at [DS:SI] and [ES:DI]; SI/DI auto-increment/decrementCMPSB (Compares 1 byte)
STOSBStores the byte in AL to [ES:DI]; DI auto-increment/decrementSTOSB (Stores 1 byte to ES:DI)
LODSBLoads a byte from [DS:SI] into AL; SI auto-increment/decrementLODSB (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).

InstructionFunction DescriptionExample
JMPUnconditional jump to target addressJMP LABEL (Jump to execute at LABEL)
JZ/JEJump if Zero Flag ZF=1 (Jump if equal)CMP EAX, EBX (Compare EAX and EBX)
JZ EQUAL (Jump if equal)
JNZ/JNEJump if Zero Flag ZF=0 (Jump if not equal)JNZ NOT_EQUAL (Jump if not equal)
JC/JNCJump if Carry Flag CF=1/0ADD EAX, 100 (May produce carry)
JC CARRY (Jump if carry)
CALLCall subroutine (Push return address to stack, jump to function entry)CALL MY_FUNC (Execute MY_FUNC then return)
RETReturn 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:

InstructionMeaningCondition
JE/JZEqual / ZeroZF=1
JNE/JNZNot Equal / Not ZeroZF=0
JA/JNBEUnsigned Greater ThanCF=0 and ZF=0
JB/JNAEUnsigned Less ThanCF=1
JG/JNLESigned Greater ThanSF=OF and ZF=0
JL/JNGESigned Less ThanSF≠OF
JGE/JNLSigned Greater Than or EqualSF=OF
JLE/JNGSigned Less Than or EqualSF≠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.

Share your love