CodeToLive

Basic Assembly Instructions

Assembly language provides a set of basic instructions that perform fundamental operations. These instructions are the building blocks of all assembly programs. This section covers the most commonly used instructions in x86 assembly.

Data Movement Instructions

These instructions move data between registers, memory, and immediate values.

MOV - Move Data

The MOV instruction copies data from the source operand to the destination operand.


mov eax, 42          ; Move immediate value 42 into EAX
mov ebx, eax         ; Copy value from EAX to EBX
mov [var], eax       ; Store EAX value into memory at 'var'
mov eax, [var]       ; Load value from memory 'var' into EAX
      

XCHG - Exchange

Swaps the contents of two operands.


xchg eax, ebx       ; Swap values of EAX and EBX
      

Arithmetic Instructions

These instructions perform basic arithmetic operations.

ADD - Addition


add eax, ebx         ; EAX = EAX + EBX
add eax, 10          ; EAX = EAX + 10
add [sum], eax       ; Add EAX to value at memory location 'sum'
      

SUB - Subtraction


sub eax, ebx         ; EAX = EAX - EBX
sub eax, 5           ; EAX = EAX - 5
      

INC/DEC - Increment/Decrement


inc eax              ; EAX = EAX + 1
dec ebx              ; EBX = EBX - 1
      

MUL/IMUL - Multiplication


; Unsigned multiplication
mov eax, 10
mov ebx, 20
mul ebx              ; EDX:EAX = EAX * EBX (unsigned)

; Signed multiplication
mov eax, -5
mov ebx, 3
imul ebx             ; EDX:EAX = EAX * EBX (signed)
      

DIV/IDIV - Division


; Unsigned division
mov edx, 0          ; Clear upper 32 bits
mov eax, 100        ; Dividend
mov ebx, 3          ; Divisor
div ebx             ; EAX = quotient, EDX = remainder

; Signed division
mov edx, -1         ; Sign extend
mov eax, -100       ; Dividend
mov ebx, 3          ; Divisor
idiv ebx            ; EAX = quotient, EDX = remainder
      

Logical Instructions

These instructions perform bitwise logical operations.

AND/OR/XOR/NOT


and eax, ebx         ; EAX = EAX AND EBX
or eax, ebx          ; EAX = EAX OR EBX
xor eax, eax         ; Fast way to zero EAX
not eax              ; EAX = NOT EAX (bitwise complement)
      

SHL/SHR - Shift Left/Right


shl eax, 1           ; Shift EAX left by 1 (multiply by 2)
shr ebx, 2           ; Shift EBX right by 2 (unsigned divide by 4)
      

SAR - Arithmetic Shift Right


sar eax, 1           ; Shift EAX right by 1 (signed divide by 2)
      

Comparison Instructions

These instructions compare values and set processor flags.

CMP - Compare


cmp eax, ebx         ; Compare EAX with EBX (sets flags)
cmp eax, 100         ; Compare EAX with 100
      

TEST - Logical Compare


test eax, eax        ; Sets ZF if EAX is zero
test ebx, 1          ; Check if least significant bit is set
      

Stack Instructions

These instructions manipulate the stack.

PUSH/POP


push eax             ; Decrement ESP and store EAX on stack
pop ebx              ; Load value from stack into EBX and increment ESP
      

PUSHAD/POPAD


pushad               ; Push all general-purpose registers
popad                ; Pop all general-purpose registers
      

Control Flow Instructions

These instructions alter the program flow.

JMP - Unconditional Jump


jmp label            ; Jump to label unconditionally
      

Conditional Jumps

Instruction Description Condition
JE/JZ Jump if equal/zero ZF = 1
JNE/JNZ Jump if not equal/not zero ZF = 0
JG/JNLE Jump if greater/not less or equal ZF = 0 and SF = OF
JGE/JNL Jump if greater or equal/not less SF = OF
JL/JNGE Jump if less/not greater or equal SF ≠ OF
JLE/JNG Jump if less or equal/not greater ZF = 1 or SF ≠ OF
JC/JB/JNAE Jump if carry/borrow/not above or equal CF = 1
JNC/JNB/JAE Jump if no carry/not borrow/above or equal CF = 0

Practical Examples

Example 1: Simple Arithmetic


section .text
    global _start

_start:
    mov eax, 10         ; EAX = 10
    mov ebx, 20         ; EBX = 20
    add eax, ebx        ; EAX = 30
    sub eax, 5          ; EAX = 25
    mov ecx, eax        ; ECX = 25
      

Example 2: Conditional Logic


section .text
    global _start

_start:
    mov eax, 15
    mov ebx, 20
    cmp eax, ebx
    jg greater          ; Jump if EAX > EBX
    ; Else case
    mov ecx, ebx
    jmp end

greater:
    mov ecx, eax

end:
    ; ECX contains the larger value
      

Example 3: Loop with Counter


section .text
    global _start

_start:
    mov ecx, 5          ; Loop counter
    xor eax, eax        ; Clear EAX (sum = 0)

loop_start:
    add eax, ecx        ; Add counter to sum
    loop loop_start      ; Decrement ECX and loop if not zero

    ; EAX now contains 5+4+3+2+1 = 15
      

Instruction Encoding

Each assembly instruction corresponds to one or more machine code bytes. The encoding depends on:

Example Encodings

Instruction Machine Code
mov eax, 42 B8 2A 00 00 00
add eax, ebx 01 D8
sub [esi+4], eax 29 46 04

Next Steps

Now that you understand basic instructions, you can learn about: