CH 4
CH 4
______________________________________________________________________________________________________________
_______
Chapter 4
What is an Instruction Set?
An instruction set is a collection of commands that a
processor can execute. These commands define
operations like data movement, arithmetic, logic, control,
and branching.
Classification:
The instruction set of 8086 can be broadly classified into the following
categories:
Data Transfer Instructions
Arithmetic Instructions
Logical Instructions
Control Transfer Instructions
String Instructions
Interrupt Control Instructions
Flag Control Instructions
Classification
Data Transfer Instructions
These instructions are used for moving data between registers, memory
and I/O ports. The source can be an immediate value, register, or memory
location, and the destination can be a register or memory location.
1. MOV (Move)
Purpose: Copies data from source to destination.
Syntax: MOV destination, source
Example: MOV AX, BX ;Moves data from register BX to register AX
MOV AX, 05h ; Move hexadecimal value 05 into AX
MOV [BX], AX ; Move the value of AX into the memory address at BX
2. PUSH (Push onto Stack)
Purpose: Pushes the operand (a register or memory value) onto the stack and decrements the stack pointer
(SP).
Syntax: PUSH operand
Example: PUSH AX ;Pushes the contents of AX onto the stack)
3. POP (Pop from Stack)
Purpose: Pops the top value from the stack into the destination operand (a register or memory location) and
increments the stack pointer (SP)..
Syntax: POP destination
Example: POP BX ;Pops the value from the top of the stack into register BX
Classification...
Data Transfer Instructions ...
4. XCHG (Exchange)
Purpose: Exchanges the contents of two operands. Both operands must be registers or one
can be a register and the other a memory location.
Syntax: XCHG operand1, operand2
Example: XCHG AX, BX ; Exchange the contents of AX and BX
XCHG AX, [SI] ; Exchange the contents of AX with the value at memory location [SI]
5. IN (Input from Port)
Purpose: Reads data from an I/O port into a register. The port is specified by an 8-bit or 16-
bit value.
Syntax: IN register, port
Example: IN AL, 60h ;Reads data from I/O port 60h into register AL
6. OUT (Output to Port)
Purpose: Sends data from a register to an I/O port. The port is specified by an 8-bit or 16-bit
value.
Syntax: OUT port, register
Example: OUT 60h, AL ;Sends the data in register AL to I/O port 60h
Classification...
Data Transfer Instructions ...
Summary of Data Transfer Instructions:
MOV: Most commonly used instruction to move
data between registers, memory, and immediate
values.
PUSH/POP: Useful for stack operations (used
for function calls, saving states).
IN/OUT: Used for I/O operations between the
processor and external devices.
XCHG: Used for exchanging data between
registers or between a register and memory.
Classification...
Arithmetic Instructions
These instructions are used to perform basic arithmetic
operations. These instructions are fundamental for
performing calculations in programs that manipulate
numerical data.
1. ADD (Addition)
Purpose: Adds the source operand to the destination operand and stores the result in the
destination..
Syntax: ADD destination, source
Flags Affected: Carry (CF), Zero (ZF), Sign (SF), Overflow (OF), Parity (PF), Auxiliary Carry
(AF).
Example: ADD AX, BX ; AX = AX + BX
ADD AX, 10h ; AX = AX + 10h (Immediate value)
2. SUB (Subtraction)
Purpose: Subtracts the source operand from the destination operand and stores the result in the
destination.
Classification...
Arithmetic Instructions
3. MUL (Multiplication)
Purpose: Multiplies two operands (unsigned). Multiplies the accumulator (AX) by the operand. The
result is stored in AX (low byte) and DX (high byte).
Syntax: MUL operand
Operand: The operand must be 8 bit or 16 bit value.
Example: MUL BX ;Multiplies AX by BX, stores the result in AX (lower 16 bits) and DX (higher
16 bits)
4. IMUL – Signed Multiplication
Syntax: IMUL operand
Description: Multiplies the accumulator (AX) by the operand, considering the sign. The result is stored in AX
(low byte) and DX (high byte).
Operands: The operand must be an 8-bit or 16-bit signed value.
Example: MOV AL, -03h ; Load AL with -03h
IMUL BL ; AX = AL * BL (signed multiplication)
Classification...
Arithmetic Instructions
5. DIV (Division) - Unsigned
Purpose: Divides the value in AX (16-bit value) by the operand. The quotient is stored in AL (low byte),
and the remainder is stored in AH (high byte).
Syntax: DIV operand
Operands: The operand can be an 8-bit or 16-bit value.
Example: DIV BX ;Divides DX:AX by BX, quotient in AX, remainder in DX
MOV AX, 1234h ; Load AX with 1234h (dividend)
MOV BL, 10h ; Load BL with 10h (divisor)
DIV BL ; AX / BL, quotient in AL, remainder in AH
6. IDIV – Signed Division
Description: Divides the value in AX (16-bit signed value) by the operand. The quotient is stored in AL (low byte), and
the remainder is stored in AH (high byte).
Syntax: IDIV operand
Operands: The operand can be an 8-bit or 16-bit signed value.
Example: MOV AX, -1234h ; Load AX with -1234h (signed dividend)
MOV BL, 10h ; Load BL with 10h (divisor)
IDIV BL ; AX / BL (signed), quotient in AL, remainder in AH
Classification...
Arithmetic Instructions
7. INC (Increment)
Purpose: Increases the value of an operand by 1.
Syntax: INC operand
Example: INC AX (Increments AX by 1)
8. DEC (Decrement)
Purpose: Decreases the value of an operand by 1.
Syntax: DEC operand
Example: DEC BX (Decrements BX by 1)
________________________________________________________________
Summary of Arithmetic Instructions:
ADD and SUB: Used for basic addition and subtraction.
INC and DEC: Used for incrementing and decrementing values by 1.
MUL and IMUL: Used for unsigned and signed multiplication, respectively.
DIV and IDIV: Used for unsigned and signed division, with results stored in AX and DX for 16-bit
operations.
Classification...
Logical Instructions
These instructions perform bitwise logical operations.
1. AND (Logical AND)
Purpose: Performs a bitwise AND between two operands.
Syntax: AND destination, source
Flags Affected: Zero Flag (ZF), Sign Flag (SF), Overflow Flag (OF), Parity Flag (PF),
Auxiliary Carry Flag (AF).
Example: AND AX, BX (Performs bitwise AND between AX and BX, result stored in AX)
AND AX, 0F0h ; AX = AX AND 0F0h (bitwise AND with immediate value)
2. OR (Logical OR)
Purpose: Performs a bitwise OR operation between the destination and source operands. The
result is stored in the destination operand.
Flags Affected: Zero Flag (ZF), Sign Flag (SF), Overflow Flag (OF), Parity Flag (PF).
Syntax: OR destination, source
Example: OR AX, BX ; AX = AX OR BX (bitwise OR)
OR AX, 0F0h ; AX = AX OR 0F0h (bitwise OR with immediate value)
Classification...
Logical Instructions
3. XOR (Exclusive OR)
Syntax: XOR destination, source
Description: Performs a bitwise XOR operation between the destination and source
operands. The result is stored in the destination operand.
Flags Affected: Zero Flag (ZF), Sign Flag (SF), Overflow Flag (OF), Parity Flag (PF).
Example: XOR AX, BX ; AX = AX XOR BX (bitwise XOR)
XOR AX, 0F0h ; AX = AX XOR 0F0h (bitwise XOR with immediate value)
4. NOT – Bitwise NOT (Complement)
Syntax: NOT operand
Description: Inverts all the bits of the operand (bitwise NOT operation). The result is
stored in the operand itself.
Flags Affected: Parity Flag (PF), Sign Flag (SF).
Example: NOT AX ; AX = NOT AX (bitwise complement)
Classification...
Logical Instructions
5. TEST – Bitwise AND for Setting Flags
Syntax: TEST destination, source
Description: Performs a bitwise AND between the destination and source operands, but does
not store the result. It only affects the flags, primarily for conditional checks.
Flags Affected: Zero Flag (ZF), Sign Flag (SF), Overflow Flag (OF), Auxiliary Carry Flag
(AF), Parity Flag (PF).
Example: TEST AX, BX ; AX AND BX, but only affects flags
TEST AX, 1 ; Check if the least significant bit of AX is set (odd number)
______________________________________________________________________
Summary of Logical Instructions:
AND: Performs bitwise AND to clear bits in the destination based on the source.
OR: Performs bitwise OR to set bits in the destination based on the source.
XOR: Performs bitwise XOR to toggle bits in the destination based on the source.
NOT: Inverts all bits in the operand (complement).
TEST: Performs bitwise AND but only affects the flags, useful for conditional testing.
Classification...
Control Flow Instructions
Control flow instructions are used to alter the sequence of
execution in a program. These instructions allow the program to
jump to different code locations, make function calls, and loop
through sections of code.
1. JMP – Unconditional Jump
Syntax: JMP target
Description: Unconditionally transfers control to the specified target (can be a
label or address).
Types:
Near jump (to a location within the current segment)
Far jump (to a location in another segment)
Example: JMP label ; Jump to the location specified by "label"
JMP 0x1234:0x5678 ; Far jump to a specific address (segment:offset)
Classification...
Control Flow Instructions
2. CALL – Call Procedure (Subroutine)
Syntax: CALL target
Description: Calls a procedure or subroutine. It pushes the return address (the next
instruction) onto the stack so the program can return after the procedure completes.
Example: CALL my_procedure ; Call the procedure "my_procedure"
• Upon reaching RET, the program will pop the return address from the stack and
continue execution from there.
3. RET – Return from Procedure
Syntax: RET
Description: Returns control to the instruction following the last CALL instruction.
The return address is popped from the stack.
Example: RET ; Return from the current procedure
• After executing RET, control is transferred to the instruction following the most
recent CALL.
Classification...
Control Flow Instructions
4. JE/JZ – Jump if Equal (or Zero)
Syntax: JE target or JZ target
Description: Jumps to the target if the Zero Flag (ZF) is set (i.e., the two operands were
equal in a previous comparison).
Example: JE label ; Jump to "label" if Zero Flag is set (if comparison was equal)
JZ label ; Same as JE, jump if Zero Flag is set
5. JNE/JNZ – Jump if Not Equal (or Not Zero)
Syntax: JNE target or JNZ target
Description: Jumps to the target if the Zero Flag (ZF) is clear (i.e., the operands were
not equal in a previous comparison).
Example: JNE label ; Jump to "label" if Zero Flag is not set (if comparison was not equal)
JNZ label ; Same as JNE, jump if Zero Flag is not set
Classification...
Control Flow Instructions
6. G/JNLE – Jump if Greater
Syntax: JG target or JNLE target
Description: Jumps to the target if the Zero Flag (ZF) is clear and the Sign Flag
(SF) is equal to the Overflow Flag (OF) (i.e., the first operand is greater than the
second operand).
Example: JG label ; Jump to "label" if greater (signed comparis on)
7. JL/JNGE – Jump if Less
Syntax: JL target or JNGE target
Description: Jumps to the target if the Sign Flag (SF) is not equal to the
Overflow Flag (OF) (i.e., the first operand is less than the second operand).
Example: JL label ; Jump to "label" if less (signed comparison)
Classification...
Control Flow Instructions
8. LOOP – Loop with CX Register
Syntax: LOOP target
Description: Decrements the CX register by 1 and jumps to the target if CX is
not zero. This is commonly used for creating loops.
Example: LOOP label ; Decrement CX and jump to "label" if CX != 0
9. LOOPE/LOOPZ – Loop while Equal (or Zero)
Syntax: LOOPE target or LOOPZ target
Description: Decrements CX and jumps to the target if CX is not zero and the
Zero Flag (ZF) is set (i.e., the last comparison was equal or zero).
Example: LOOPE label ; Decrement CX and jump to "label" if CX != 0 and
ZF is set (equal)
Classification...
Control Flow Instructions
10. LOOPNE/LOOPNZ – Loop while Not Equal (or Not Zero)
Syntax: LOOPNE target or LOOPNZ target
Description: Decrements CX and jumps to the target if CX is not zero and the
Zero Flag (ZF) is clear (i.e., the last comparison was not equal or not zero).
Example: LOOPNE label ; Decrement CX and jump to "label" if CX != 0
and ZF is clear (not equal)
Classification...
Summary of Control Flow Instructions:
JMP: Unconditionally jumps to a target address.
CALL: Calls a subroutine and saves the return address
on the stack.
RET: Returns from a subroutine to the calling location.
JE/JZ and JNE/JNZ: Conditional jumps based on the
Zero Flag.
JG/JNLE and JL/JNGE: Conditional jumps based on
signed comparisons (greater or less).
LOOP: Loops based on the value in the CX register.
LOOPE/LOOPZ and LOOPNE/LOOPNZ: Conditional
looping based on the Zero Flag.
Classification...
String instructions are used to operate on arrays of data,
known as strings. These instructions allow for efficient
data processing in 8086 assembly language, making it
easier to handle tasks like copying, comparing, and
scanning blocks of memory.
1. MOVSB / MOVSW – Move String Byte/Word
Syntax: MOVSB (Move Byte) or MOVSW (Move Word)
Description: Moves a byte or word from the source (pointed by SI) to the
destination (pointed by DI).
Operation:
MOVSB: Moves one byte from [SI] to [DI].
MOVSW: Moves one word (2 bytes) from [SI] to [DI].
Example: MOVSB ; Move byte from [SI] to [DI]
MOVSW ; Move word (2 bytes) from [SI] to [DI]
Classification...
2. CMPSB / CMPSW – Compare String Byte/Word
Syntax: CMPSB (Compare Byte) or CMPSW (Compare Word)
Description: Compares a byte or word at [SI] with [DI], and updates
the flags based on the comparison (similar to a CMP instruction).
Operation:
CMPSB: Compares the byte at [SI] with the byte at [DI].
CMPSW: Compares the word (2 bytes) at [SI] with the word at [DI].
Flags Affected: Zero Flag (ZF), Carry Flag (CF), Overflow Flag (OF).
Example: CMPSB ; Compare byte at [SI] with byte at [DI]
CMPSW ; Compare word (2 bytes) at [SI] with word at [DI]
Classification...
3. LODSB / LODSW – Load String Byte/Word
Syntax: LODSB (Load Byte) or LODSW (Load Word)
Description: Loads a byte or word from the string at [SI] into the AL
or AX register, respectively.
Operation:
LODSB: Loads a byte from [SI] into AL.
LODSW: Loads a word from [SI] into AX.
Example: LODSB ; Load byte from [SI] into AL
LODSW ; Load word (2 bytes) from [SI] into AX
Classification...
4. STOSB / STOSW – Store String Byte/Word
Syntax: STOSB (Store Byte) or STOSW (Store Word)
Description: Stores the byte or word in AL or AX at the address [DI].
Operation: STOSB: Stores AL at [DI].
STOSW: Stores AX at [DI].
Example: STOSB ; Store byte in AL at [DI]
STOSW ; Store word in AX at [DI]
5. REP (Repeat Prefix)
Syntax: REP (can be used with MOVSB, MOVSW, CMPSB, CMPSW, etc.)
Description: Repeats the string operation until the CX register becomes zero. The SI
and DI registers are automatically adjusted after each operation (incremented or
decremented depending on the DF flag).
Example: REP MOVSB ; Repeat MOVSB until CX is zero (move string bytes)
REP STOSW ; Repeat STOSW until CX is zero (store word in AX)
Classification...
Summary of String Instructions:
MOVSB/MOVSW: Move byte/word from source
to destination.
CMPSB/CMPSW: Compare byte/word at SI with
byte/word at DI.
SCASB/SCASW: Scan for a byte/word at DI that
matches AL/AX.
LODSB/LODSW: Load a byte/word from SI into
AL/AX.
STOSB/STOSW: Store AL/AX at DI.
Classification...
Interrupt Instructions
Interrupts are essential for handling events
asynchronously in a program. They allow the CPU to
temporarily stop executing its current instructions and
transfer control to an interrupt service routine (ISR) to
handle specific tasks (e.g., hardware events, software
exceptions). The 8086 microprocessor supports both
hardware and software interrupts.
1. INT – Software Interrupt
Syntax: INT interrupt_number
Description: Causes a software interrupt to occur. The processor saves the current state and
jumps to the interrupt service routine (ISR) defined for the specified interrupt number.
Interrupt Vector Table: The 8086 has a predefined vector table for standard interrupts
(e.g., INT 0x10 for video services).
Classification...
Interrupt Instructions
2. INTO – Interrupt on Overflow
Syntax: INTO
Description: Triggers an interrupt if the Overflow Flag (OF) is set.
It is typically used after arithmetic operations that may cause an
overflow. This instruction checks the overflow flag and, if set,
generates an interrupt (interrupt 4).
Example: ADD AX, BX ; Perform addition
INTO ; Trigger interrupt if overflow occurs
Flags Affected: Only the Overflow Flag (OF) is checked. If OF is
set, INT 4 is triggered.
Classification...
Interrupt Instructions
3. IRET – Interrupt Return
Syntax: IRET
Description: Returns from an interrupt service routine (ISR). The processor pops the
return address and the flags from the stack and resumes execution from the point of the
interrupt.
Example: IRET ; Return from interrupt service routine
Flags Affected: The Flags are restored from the stack during the return process.
4. INT 3 – Breakpoint Interrupt
Syntax: INT 3
Description: Causes a software interrupt for debugging purposes. It is often used to set
breakpoints during debugging sessions, triggering a debugger to take control of the
program.
Example: INT 3 ; Trigger a breakpoint interrupt (used in debugging)
Flags Affected: Similar to other interrupt instructions, the IF flag is cleared to disable
further interrupts unless re-enabled.
Classification...
Interrupt Instructions
5. IN – Input from Port (I/O Interrupt)
Syntax: IN AL, port or IN AX, port
Description: Reads data from a specified I/O port and stores it in the AL (8-bit) or
AX (16-bit) register. This can be considered an input interrupt from a peripheral
device.
Example: IN AL, 60h ; Read byte from I/O port 0x60 into AL
IN AX, 3F8h ; Read word from I/O port 0x3F8 into AX
6. OUT – Output to Port (I/O Interrupt)
Syntax: OUT port, AL or OUT port, AX
Description: Writes data from the AL (8-bit) or AX (16-bit) register to the specified
I/O port. This instruction is used to send data to peripheral devices.
Example: OUT 60h, AL ; Write byte from AL to I/O port 0x60
OUT 3F8h, AX ; Write word from AX to I/O port 0x3F8
Classification...
Summary of Interrupt Instructions:
INT: Triggers a software interrupt and jumps to
an ISR.
INTO: Triggers an interrupt if the Overflow Flag
(OF) is set.
IRET: Returns from an interrupt service routine,
restoring flags and return address.
INT 3: Software interrupt used for debugging
(breakpoints).
IN: Reads data from a specified I/O port into the
AL or AX register.
END