Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 67 ยท Instruction Set Architecture

Machine Instruction Families (8086 Style)

Tour the families of machine instructions in an 8086-style processor: data transfer, arithmetic, logic, shift, string, program control and processor control, with a Python emulator.

An instruction set can have hundreds of instructions, but they fall into a handful of families. Once you know the families, a new instruction set is much less intimidating: you look for the family, then for the exact name and operand rules. This lesson tours the families using the classic Intel 8086 as the model, because its instruction set is small enough to hold in your head, it is still the ancestor of every x86 PC, and it is the one most courses and exams use.

You will see what each family does, which instructions belong to it, and how a few of them behave in a tiny Python emulator. It continues from Addressing Modes and prepares you for Assembly Language Basics. The syntax shown is Intel style: the destination comes first (MOV AX, BX copies BX into AX).

The seven families

flowchart TD
  I["Machine instructions"] --> D["Data transfer"]
  I --> A["Arithmetic"]
  I --> L["Logic, shift and rotate"]
  I --> S["String"]
  I --> P["Program control"]
  I --> C["Processor control"]
  I --> IO["Input / output"]

Most instruction sets can be sorted into these groups.

Data transfer

These instructions copy data without changing it. They do not alter the flags (except POPF and SAHF).

Instruction Meaning
MOV dst, src Copy src into dst (register, memory or immediate; never memory to memory)
PUSH src / POP dst Put a word on the stack, take one off
XCHG a, b Swap two operands
LEA reg, mem Load the address of mem, not its contents
XLAT Table lookup: AL = [BX + AL]
LAHF / SAHF Copy the flag byte to/from AH
PUSHF / POPF Push or pop the whole flags register

LEA is a favourite exam trap. MOV BX, [SI] loads the memory contents at address SI; LEA BX, [SI] loads SI's value itself, i.e. the address.

Arithmetic

Instruction Meaning
ADD, ADC Add; add with carry (for multi-word numbers)
SUB, SBB Subtract; subtract with borrow
INC, DEC Add or subtract 1 (do not change the carry flag)
NEG Two's complement negate
CMP a, b Subtract, set flags, discard the result
MUL, IMUL Unsigned/signed multiply (result is twice as wide)
DIV, IDIV Unsigned/signed divide (quotient and remainder)
DAA, AAA... Decimal (BCD) adjust instructions

Arithmetic instructions update the flags (carry, zero, sign, overflow, parity, auxiliary carry), which the program-control family then tests. Tracing Register and Flag Contents goes through examples.

Logic, shift and rotate

Instruction Meaning
AND, OR, XOR, NOT Bitwise operations (AND/OR/XOR clear the carry and overflow flags)
TEST a, b AND that only sets flags
SHL/SAL, SHR Shift left / logical right (fills with 0)
SAR Arithmetic right shift (copies the sign bit)
ROL, ROR, RCL, RCR Rotate, optionally through the carry flag

Useful idioms: XOR AX, AX zeroes a register (smaller than MOV AX, 0), SHL AX, 1 doubles an unsigned number, and TEST AL, 1 checks whether a number is odd.

String instructions

String instructions work on blocks of memory: the source is DS:SI, the destination is ES:DI, and after each step SI and DI move by the element size, upward or downward depending on the direction flag (CLD = up, STD = down). Add the repeat prefix REP to run the instruction CX times.

Instruction Meaning
MOVSB/MOVSW Copy a byte/word from [SI] to [DI]
CMPSB/CMPSW Compare [SI] with [DI]
SCASB/SCASW Compare AL/AX with [DI] (search)
LODSB/LODSW Load [SI] into AL/AX
STOSB/STOSW Store AL/AX at [DI] (fill)
REP, REPE, REPNE Repeat while CX is not zero (and ZF as stated)

REP MOVSB is a whole memcpy in one instruction; REP STOSB is memset; REPNE SCASB is strlen.

Program control

These change the instruction pointer.

Instruction Meaning
JMP target Unconditional jump
JE/JZ, JNE/JNZ, JC, JG, JL... Jump if a flag condition holds
CALL target / RET Push the return address and jump; pop it and return
LOOP target Decrement CX; jump if CX is not zero
INT n / IRET Software interrupt; return from interrupt
HLT Stop until an interrupt

Conditional jumps pair with CMP: after CMP AX, BX, use the unsigned conditions (JA, JB) for unsigned numbers and the signed ones (JG, JL) for two's complement numbers. CALL differs from JMP in that it saves a return address on the stack. See Assembly: Branches, Loops and Functions.

Processor control and I/O

Instruction Meaning
CLC, STC, CMC Clear, set, complement the carry flag
CLD, STD Clear/set the direction flag
CLI, STI Disable/enable maskable interrupts
NOP Do nothing
WAIT, LOCK, ESC Synchronize with a co-processor, lock the bus
IN AL, port / OUT port, AL Read/write an I/O port (see Memory-Mapped vs Isolated I/O)

Trying a few families

The emulator below implements one instruction from several families on a 16-bit machine with a small memory, so you can see transfer, arithmetic, logic, loop and string behaviour together.

Python A tiny 8086-style emulator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A second experiment contrasts MOV and LEA, and the effect of the direction flag on a string copy.

Python MOV versus LEA, and the direction flag

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading an instruction set

When you meet any ISA, do this:

  1. Find the data-transfer family and learn which operand combinations are legal.
  2. Learn what the arithmetic and logic instructions do to the flags.
  3. Find the branch family and the conditions it understands.
  4. Look for special families (string, vector, atomic, system) that the architecture adds.

RISC instruction sets such as RISC-V and ARM keep the same families but have fewer, more regular instructions and use load and store instructions for all memory access. See Instruction Set Architecture.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the difference between MOV BX, [SI] and LEA BX, [SI]?

    LEA computes the effective address and places it in the register without accessing memory.
  2. 2. Which instruction does a comparison without storing a result?

    CMP subtracts to set the flags and throws the difference away.
  3. 3. What does REP MOVSB do?

    MOVSB copies one byte and updates SI and DI; REP repeats it CX times.
  4. 4. How does CALL differ from JMP?

    CALL pushes the address of the next instruction so that RET can come back; JMP does not.
  5. 5. Which family contains CLI and STI?

    CLI and STI disable and enable interrupts, which is a processor-control operation, like CLC and NOP.

Summary

  • Machine instructions fall into families: data transfer, arithmetic, logic/shift/rotate, string, program control, processor control and I/O.
  • In Intel syntax the destination operand comes first; memory-to-memory moves are not allowed.
  • Arithmetic and logic instructions set the flags; CMP and TEST set flags and discard the result.
  • String instructions use SI and DI, the direction flag and the REP prefix for block operations.
  • Program-control instructions change the instruction pointer; CALL saves a return address, LOOP counts with CX.
  • LEA computes an address, and XOR reg, reg is the idiom for zero.

Next: Assembly Language Basics.