Essential x86 Assembly Instructions: A Cheat Sheet

Essential x86 Assembly Instructions: A Cheat Sheet

This comprehensive guide serves as a cheat sheet for essential x86 assembly instructions, covering a wide range of operations from data movement and arithmetic to logic, control flow, and more. Understanding these instructions is crucial for low-level programming, reverse engineering, and optimizing performance-critical code. While this guide focuses primarily on 32-bit x86, many concepts translate directly to 64-bit architectures.

I. Data Movement Instructions:

These instructions facilitate moving data between registers, memory locations, and the stack.

  • MOV: The most fundamental instruction, MOV copies data from source to destination. It supports various operand combinations:

    • MOV reg, reg (Register to Register)
    • MOV reg, mem (Memory to Register)
    • MOV mem, reg (Register to Memory)
    • MOV reg, imm (Immediate value to Register)
    • MOV mem, imm (Immediate value to Memory)
    • Note: Direct memory-to-memory moves are generally not supported.
  • PUSH: Pushes a value (register or memory operand) onto the stack. The stack pointer (ESP) is decremented accordingly.

  • POP: Pops a value from the stack into a register or memory operand. The stack pointer (ESP) is incremented.

  • LEA (Load Effective Address): Calculates the effective address of a memory operand and stores it in a register. Useful for accessing array elements and complex data structures.

  • XCHG (Exchange): Swaps the contents of two operands (registers or a register and a memory location).

II. Arithmetic Instructions:

These instructions perform arithmetic operations on integer data.

  • ADD: Adds the source operand to the destination operand, storing the result in the destination.

  • SUB: Subtracts the source operand from the destination operand, storing the result in the destination.

  • INC: Increments the operand by 1.

  • DEC: Decrements the operand by 1.

  • MUL: Multiplies two unsigned operands. The size of the operands determines where the result is stored (AL/AX/EAX/EDX:EAX).

  • IMUL: Signed multiplication, similar to MUL.

  • DIV: Unsigned division. The dividend is assumed to be in AX/DX:AX depending on the divisor size. The quotient is stored in AL/AX and the remainder in AH/DX.

  • IDIV: Signed division, similar to DIV.

  • NEG: Negates the operand (two’s complement).

III. Logical Instructions:

These instructions perform bitwise logical operations.

  • AND: Performs a bitwise AND operation between the source and destination operands.

  • OR: Performs a bitwise OR operation.

  • XOR: Performs a bitwise XOR (exclusive OR) operation.

  • NOT: Inverts the bits of the operand (one’s complement).

  • SHL (Shift Left): Shifts the bits of the operand to the left. Zeroes are shifted into the least significant bits.

  • SHR (Shift Right): Shifts the bits of the operand to the right. Zeroes are shifted into the most significant bits (logical shift).

  • SAR (Shift Right Arithmetic): Shifts the bits to the right, preserving the sign bit (arithmetic shift).

  • ROL (Rotate Left): Rotates the bits to the left. Bits shifted out of the most significant bit are shifted into the least significant bit.

  • ROR (Rotate Right): Rotates the bits to the right.

  • RCL (Rotate Left through Carry): Includes the carry flag in the rotation.

  • RCR (Rotate Right through Carry): Includes the carry flag in the rotation.

IV. Control Flow Instructions:

These instructions control the execution flow of the program.

  • JMP (Jump): Unconditionally transfers control to a specified address.

  • Jcc (Conditional Jump): Jumps to a specified address if a certain condition is met. Examples include:

    • JE (Jump if Equal)
    • JNE (Jump if Not Equal)
    • JZ (Jump if Zero)
    • JNZ (Jump if Not Zero)
    • JG (Jump if Greater)
    • JGE (Jump if Greater or Equal)
    • JL (Jump if Less)
    • JLE (Jump if Less or Equal)
    • JA (Jump if Above)
    • JAE (Jump if Above or Equal)
    • JB (Jump if Below)
    • JBE (Jump if Below or Equal)
  • CALL: Calls a subroutine. Pushes the return address onto the stack and jumps to the specified address.

  • RET: Returns from a subroutine. Pops the return address from the stack and jumps to that address.

  • LOOP: Decrements ECX and jumps to a specified address if ECX is not zero. Used for implementing loops.

  • INT (Interrupt): Generates a software interrupt.

V. Comparison and Flag Manipulation Instructions:

These instructions compare operands or manipulate flags in the EFLAGS register.

  • CMP: Compares two operands by subtracting the source from the destination without storing the result. Sets flags based on the comparison.

  • TEST: Performs a bitwise AND operation between two operands without storing the result. Sets flags based on the result.

  • SETcc: Sets a byte register to 1 if the specified condition is true, 0 otherwise.

  • CLC: Clears the carry flag.

  • STC: Sets the carry flag.

  • CMC: Complements the carry flag.

  • CLD: Clears the direction flag.

  • STD: Sets the direction flag.

VI. String Instructions:

These instructions operate on strings of data.

  • MOVS: Moves a string of data from one location to another.

  • CMPS: Compares two strings of data.

  • SCAS: Scans a string for a specific value.

  • STOS: Stores a value into a string.

  • LODS: Loads a value from a string into a register.

  • Rep prefixes (REP, REPE, REPNE) can be used with string instructions to repeat the operation multiple times.

VII. Other Important Instructions:

  • NOP (No Operation): Does nothing. Used for padding or timing purposes.

  • HLT (Halt): Halts the processor until an interrupt occurs.

  • IN: Reads data from an input port.

  • OUT: Writes data to an output port.

VIII. Addressing Modes:

Understanding addressing modes is crucial for effective assembly programming. x86 supports various addressing modes, including:

  • Register Addressing: Operand is a register. e.g., MOV EAX, EBX

  • Immediate Addressing: Operand is a constant value. e.g., MOV AX, 1234h

  • Direct Addressing: Operand is a memory location specified by its address. e.g., MOV AX, [1234h]

  • Indirect Addressing: Operand’s address is stored in a register. e.g., MOV AX, [EBX]

  • Base-Plus-Index Addressing: Operand’s address is calculated by adding a base register and an index register. e.g., MOV AX, [EBX + ESI]

  • Base-Plus-Index-Plus-Displacement Addressing: Operand’s address is calculated by adding a base register, an index register, and a displacement value. e.g., MOV AX, [EBX + ESI + 10h]

IX. Example Code Snippet (Calculating the sum of an array):

“`assembly
section .data
array dw 1, 2, 3, 4, 5
array_len equ $-array

section .bss
sum resw 1

section .text
global _start

_start:
mov esi, array ; Point ESI to the start of the array
mov ecx, array_len / 2 ; Set ECX to the number of elements
xor ax, ax ; Initialize sum to zero

loop_start:
add ax, [esi] ; Add the current element to the sum
add esi, 2 ; Move to the next element (word size)
loop loop_start ; Decrement ECX and loop if not zero

mov [sum], ax       ; Store the sum in memory

; Exit program
mov eax, 1
xor ebx, ebx
int 80h

“`

This cheat sheet provides a concise overview of essential x86 assembly instructions. While not exhaustive, it covers a significant portion of the instruction set and provides a solid foundation for further exploration. Remember to consult Intel’s official documentation for detailed information and specific nuances of each instruction. By mastering these fundamental instructions, you can unlock the power of low-level programming and gain deeper insights into computer architecture.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top