Microprocessors Notes
Microprocessors Notes
8086
Microprocessor
Introduction to Microprocessor
A microprocessor is an electronic chip that acts as the central processing unit (CPU) of a
computer.
The microprocessors fetch the instructions one after the other and execute them in its
arithmetic and logic unit.
Programs are called Assembly Level Language and then they are converted into binary
machine level language. This conversion may be done manually or using an application
known as Assembler.
In general, the programs are written by the user for a microprocessor to work with the real
world data.
A microprocessor based system needs a set of memory units, a set of interfacing circuits for
inputs and a set of interfacing circuits for outputs. All circuits applied along with the
microprocessor are called microcomputer systems.
Examples of some common devices that are using microprocessor are computers, printers,
automobiles, washing machines, microwave ovens, mobile phones etc.
Generations of Microprocessor
The transistor technology led to the introduction of minicomputers of the 1960s and the
personal computer revolution of the 1970s.
Microprocessors evolution is categorized into 5 generations i.e. first, second, third, fourth,
and fifth generations.
In 1971, Intel Corporation introduced the first 4-bit 4004 microprocessor at 108 kHz.
Some of the popular processors are Motorola’s 6800 and 6809 and Intel’s 8085, Zilog’s Z80.
These are fabricated using low-power version of the HMOS technology called HCMOS.
These carry on-chip functionalities and improvements in the speed of memory and I/O
devices. Their design surpassed 10 million transistors per chip.
Some examples are Pentium, Celeron and dual and quad core processors working with up to
3.5GHz speed.
Microprocessor Architecture
Programming Model
Programming models of 8086 through Core2 are considered program visible because its
registers are used during application programming and specified by the instructions.
Other registers are considered to be program invisible because they are not directly
addressable during programming of applications, but can be used indirectly during system
programming.
Only 80286 and above contains the program-invisible registers to operate and control the
protected memory system and other features of the microprocessor.
8-bit registers includes AH, AL, BH, BL, CH, CL, DH and DL.
16-bit registers includes AX, BX, CX, DX, SP, BP, DI, SI, IP, FLAGS, CS, DS, ES, SS, FS
and GS.
Extended 32-bit registers includes EAX, EBX, ECX, EDX, ESP, EBP, EDI, ESI, EIP and
EFLAGS.
Note: These 32-bit extended registers and 16-bit registers FS and GS are only available in the
80386 and above.
Multipurpose registers includes EAX, EBX, ECX, EDX, EBP, EDI and ESI.
64-bit registers includes RAX, RBX, RCX, RDX, RSP, RBP, RDI, RSI, RIP, RFLAGS and
additional 64-bit registers that are called R8 through R15.
These additional 64-bit registers (R8 through R15) are addressed as a byte, word, double
word, or quad word but only the rightmost 8 bits is a byte. R8 through R15 have no provision
for directly addressing bits 8 through 15 as a byte.
Flat mode 64-bits access to numbered registers
BX (Base Index): BX is addressable as RBX, EBX, BX, BH, or BL. It holds the offset
address of a location in the memory system. It is also used to refer data in memory.
CX (Count): CX is addressable as RCX, ECX, CX, CH, or CL. It holds the count for
various instructions and the offset address of memory data. The value of this register
indicates the number of times the same instructions has to be executed.
DX (Data): DX is addressable as RDX, EDX, DX, DH, or DL. It holds a part of the result
from a multiplication or part of the dividend before a division. It also holds the I/O device
address while executing the IN and OUT instructions. This register can also address
memory data.
SP (Stack Pointer): SP is addressable as RSP, ESP, or SP. It is used to hold the offset
address of the data stored at the top of stack segment. It is used along with SS register to
decide the address at which data is pushed or popped during the execution of PUSH and POP
instructions.
BP (Base Pointer): BP is addressable as RBP, EBP, or BP. It is used to hold the offset
address of data to be read from or write into the stack segment.
SI (Source Index): SI is addressable as RSI, ESI, or SI. It is used to hold the offset address
of source data in data segment while executing string instructions.
DI (Destination Index): DI is addressable as RDI, EDI, or DI. It is used to hold the offset
address of destination data in extra segment while executing string instructions.
R8 through R15: These registers are only found in the Pentium 4 and Core2 if 64-bit
extensions are enabled. The data in these registers are addressed as 64, 32, 16, or 8 - bit sizes
and are of general purpose. The 8-bit portion is the rightmost 8-bit only and bits from 8 to 15
are not directly addressable as a byte.
Special-Purpose Registers
The special-purpose registers include RIP, RSP and RFLAGS and the segment registers
include CS, DS, ES, SS, FS, and GS.
IP (Instruction Pointer): IP is addressable as RIP, EIP or IP. It points to the next instruction
in a section of memory defined as a code segment. It is used by microprocessor to find the
next sequential instruction in the program located within the code segment.
1. Status Flags: They indicate the status of the result that is obtained after the execution of
the arithmetic or logic instruction.
CF (Carry Flag): It holds the carry after addition or borrow after subtraction operation.
It also indicates error conditions.
PF (Parity Flag): It is the count of ones in a number expressed as even or odd. It is logic 0
for odd parity (i.e. odd number of 1s) and logic 1 for even parity (i.e. even number of 1s).
For example, if a number contains three binary one bits, it has odd parity and if a number
contains no one bits, it has even parity.
AF (Auxiliary carry Flag): It holds the carry (half-carry) after addition or borrow after
subtraction between bit positions 3 and 4 of the result in a BCD operation. This is used by
DAA and DAS instructions to adjust the value in AL after a BCD addition or subtraction,
respectively.
ZF (Zero Flag): It indicates that the result of an arithmetic or logic operation is zero. If Z
= 1, the result is zero and if Z = 0, the result is non zero.
SF (Sign Flag): It holds the arithmetic sign of the result after arithmetic or logic
instruction executes. If S = 1, the sign bit (leftmost bit of a number) is set or negative and if
S = 0, the sign bit is cleared or positive.
TF (Trap Flag): It enables trapping using single step technique. If T flag is set (i.e. TF =
1), 8086 gets interrupted after the execution of each instruction in the program. If TF is
cleared (i.e. TF = 0), the trapping or debugging feature is disabled.
IF (Interrupt Flag): It controls the operation of the INTR (interrupt request) input pin. If I
= 1, the INTR pin is enabled and if I = 0, the INTR pin is disabled. The state of I flag bit is
controlled by STI (Set I flag) and CLI (Clear I flag) instructions.
DF (Direction Flag): It selects either the increment or decrement mode for the DI and/or
SI registers during string instructions. If D = 1, the registers are automatically decremented
and if D = 0, the registers are automatically incremented. The D flag is set with the STD
(Set Direction) and cleared with the CLD (Clear Direction) instructions.
OF (Overflow Flag): It occurs when signed numbers are added or subtracted. Signed
numbers are represented in 2’s complement form in microprocessor. It indicates that result
has exceeded the capacity of the machine.
Example: if the 8-bit signed data 7EH (+126) is added with the 8-bit signed data 02H (+2)
then the result is 80H (-128 in 2’s complement form). This result indicates an overflow
condition and hence the overflow flag is set during the above signed addition.
Note: In an 8-bit register, the minimum and maximum value of the signed number that can be
stored is -128 (=80H) and +127 (=7FH) respectively while in a 16 bit register, the minimum
and maximum value of the signed number that can be stored is -32768 (=8000H) and +32767
(=7FFFH) respectively.
IOPL (I/O Privilege Level): It is used in protected mode operation to select the privilege
level for I/O devices. If the current privilege level is higher or more trusted than the IOPL
then I/O executes without hindrance. If the current privilege level is lower than the IOPL then
an interrupt occurs, causing execution to suspend. An IOPL of 00 is the highest or most
trusted and an IOPL of 11 is the lowest or least trusted.
NT (Nested Task): It indicates that the current task is nested within another task in
protected mode operation. This flag is set when the task is nested by software.
RF (Resume Flag): It is used with debugging to control the resumption of execution after
the next instruction.
VIP (Virtual Interrupt Pending): It provides information about a virtual mode interrupt. It
is used in multitasking environments to provide the operating system with virtual interrupt
flags and interrupt pending information.
ID (Identification): It indicates that the Pentium – Pentium 4 microprocessors support the
CPU ID instruction.
Segment Registers
Segment registers generate memory addresses when combined with other registers in the
microprocessor. Segment registers functions differently in real mode and in protected mode
operations of the microprocessor.
The minimum size of a segment is 1 byte and maximum size of a segment is 64 Kbytes. A
segment begins in memory at a memory address which is divisible by 16.
CS (Code Segment): It is a section of memory that holds the code (programs and
procedures) used by the microprocessor. It defines starting address of the section of
memory holding code. In real mode operation, it defines the start of a 64Kb section of
memory and in protected mode operation, it selects a descriptor that describes the starting
address and length of a section of memory holding code.
DS (Data Segment): It is a section of memory that contains data used by a program. The
data is accessed in the data segment by an offset address or the contents of other registers that
hold the offset address.
SS (Stack Segment): It holds the stack of a program which is needed while executing
CALL and RET instructions to handle interrupts. Stack entry point is determined by stack
segment and stack pointer registers. BP register also addresses data within the stack segment.
ES (Extra Segment): It is an additional data segment that is used by some of the string
instructions to hold the destination data.
REAL MODE MEMORY ADDRESSING
Real mode operation allows microprocessor to address only first 1Mb memory space. The
first 1Mb of memory is called the real memory, conventional memory, or DOS memory
system.
All real mode memory addresses must include a segment address and an offset address.
Segment address is located within the segment registers which defines the beginning address
of any 64K memory segment.
Offset address selects any location within the 64Kb of memory segment.
In real mode, each segment register is internally appended with 0H on its rightmost end. This
forms a 20 bit memory address, allowing it to access the beginning of a segment.
A real mode segment of memory is 64K in length, so once the beginning address is known,
the ending address is calculated by adding FFFFH.
Example, if a segment register contains 3000H then the first address of the segment is
30000H and the last address is 3FFFFH (30000H + FFFFH).
Offset address is added to the beginning of the segment to address a memory location within
the memory segment.
Example, if the segment address is 1000H and the offset address is 2000H, the
microprocessor addresses memory location 12000H (10000H+2000H).
The segment and offset address for a segment address of 1000H with an offset of 2000H is
sometimes written as 1000:2000.
Some addressing modes combine multiple registers and one offset value to form an offset
address. When this happens then the sum of these values may exceed FFFFH.
Example, the address accessed in a segment whose segment address is 4000H and whose
offset address is specified as the sum of F000H and 3000H will access memory location
42000H instead of location 52000H. When the F000H and 3000H are added, they form a 16
bit (modulo 16) sum of 2000H used as the offset address; not 12000H, the true sum. Note
that the carry of 1 in addition of F000H and 3000H (12000H) is dropped to form the offset
address of 2000H. The address is generated as 4000:2000 or 42000H.
The microprocessor consists of a set of rules that apply to segments when addressing memory.
These rules define the segment register and offset register combination.
For example, code segment register is always used with instruction pointer to address the
next instruction in a program.
Code segment register defines the beginning of the code segment and the instruction
pointer locates the next instruction within the code segment. The combination (CS:IP or
CS:EIP) locates the next instruction executed by the microprocessor.
Example, if CS = 1400H and IP = 1200H, the microprocessor fetches its next instruction
from memory location 15200H (14000H + 1200H).
Stack data is referred to through the stack pointer which is in the memory space addressed by
the stack pointers (SP / ESP) or the base pointers (BP / EBP).
Stack data is referenced through stack segment at the memory location addressed by either
stack pointer (SP/ESP) or base pointer (BP/EBP).
A memory segment can overlap if 64Kb of memory are not required for a segment. A
program can have more than 4 or 6 segments but can only access 4 or 6 segments at a time.
This scheme of segment plus offset addressing allows DOS programs to be relocated in the
memory system. It also allows programs that are written to function in the real mode to
operate in a protected mode system.
The segment and offset addressing scheme allows both programs and data to be relocated
without changing anything in a program or data.
Relocatable program is a program that can be placed into any area of memory and executed
without change.
Relocatable data is the data that can be placed in any area of memory and used without any
change to the program.
As the memory is addressed within a segment by an offset address, the memory segment can
be moved to any place in the memory system without changing any of the offset addresses.
Example: If an instruction is 4 bytes above the start of the segment, its offset address is 4. If
the entire program is moved to a new area of memory, this offset address of 4 still points to 4
bytes above the start of the segment. Only content of the segment register must be changed to
address the program in new area of memory.
PROTECTED MODE MEMORY ADDRESSING
Protected mode memory addressing allows access to data and programs located above the
first 1M byte of memory as well as within the first 1M byte of memory.
When data and programs are addressed in extended memory, the offset address is still used to
access information located within the memory segment.
In place of segment address, the segment register contains a selector that selects a descriptor
from the descriptor table. The descriptor describes location, length, and access rights of the
memory segment. Let’s understand what are these selectors and descriptors.
The selector is located in the segment register and selects one of 8192 descriptors from one of
the two tables of descriptors. The descriptor describes the location, length, and access rights
of memory segment.
For example, in the real mode, if CS = 0008H, the CS begins at location 00080H. In
protected mode, this segment number can address any memory location in the entire system
for code segment.
1. Global descriptors (System descriptor): They contain segment definitions that apply to
all programs.
Base address of a descriptor indicates the beginning location of the memory segment.
Segment limit contains the last offset address found in a memory segment.
For example, if a segment begins at memory location F00000H and ends at location
F000FFH, the base address is F00000H and the limit is FFH.
Granularity bit or G bit: If G = 0, the limit specifies a segment limit of 00000H to FFFFFH
and if G = 1, the value of the limit is multiplied by 4K bytes (appended with FFFH) so the
segment limit is from 00000FFFFH to FFFFFFFFH.
= 10000000H + 001FFH
= 100001FFH
= 10000000H + 001FFFFFH
= 101FFFFFH
Program-Invisible Registers
The global and local descriptor tables are found in the memory system. In order to access and
specify the address of these tables, the microprocessor 80286–Core2 contains program-
invisible registers. The program-invisible registers are not directly addressed by software so
they are given this name.
Each of the segment registers contains a program-invisible portion used in the protected
mode. The program-invisible portion of these registers is often called cache memory because
cache is any memory that stores information.
The program-invisible portion of the segment register is loaded with the base address, limit,
and access rights each time the number segment register is changed.
When a new segment number is placed in a segment register, the microprocessor accesses a
descriptor table and loads the descriptor into the program-invisible portion of the segment
register. It is held there and used to access the memory segment until the segment number is
again changed. This allows the microprocessor to repeatedly access a memory segment
without referring to the descriptor table.
GDTR (Global Descriptor Table Register) and IDTR (Interrupt Descriptor Table
Register) contain the base address of the descriptor table and its limit. The limit of each
descriptor table is 16 bits because the maximum table length is 64K bytes.
When the protected mode operation is desired, the address of the global descriptor table and
its limit are loaded into the GDTR. Before using the protected mode, the interrupt descriptor
table and the IDTR must also be initialized.
The location of the local descriptor table is selected from the global descriptor table. One of
the global descriptors is set up to address the local descriptor table.
To access the local descriptor table, the LDTR (Local Descriptor Table Register) is loaded
with a selector. This selector accesses the global descriptor table and loads the address, limit,
and access rights of the local descriptor table into the cache portion of the LDTR.
A task is a procedure or application program. The descriptor for the procedure or application
program is stored in the global descriptor table, so access can be controlled through the
privilege levels.
TR (Task Register) holds a selector which accesses a descriptor that defines a task. It allows
a context or task switch in about 17μs. Task switching allows the microprocessor to switch
between tasks in a short amount of time. The task switch allows multitasking systems to
switch from one task to another in a simple and orderly fashion.
Addressing Modes
Memory-to-memory transfers are not allowed by any instruction except for the MOV
instruction.
MOV AX, BX instruction transfers the word contents of the source register (BX) into the
destination register (AX).
A MOV instruction always copies the source data into the destination.
Effective Address (EA) represents the offset address of the data within a segment.
The data-addressing modes are as follows:
1. Register Addressing Mode: In this addressing mode, the data present in register is
moved or manipulated and the result is stored in the register.
The microprocessor contains the following 8-bit registers used with register addressing: AX,
BX, CX, DX, SP, BP, SI, and DI.
Examples: MOV AL, BL; copies content of BL to AL and ADC BX, DX; adds the content
of BX, carry flag and DX and store result in BX
Note: Never mix an 8-bit register with a 16-bit register, an 8-bit register with a 32-bit register,
or a l6-bit register with a 32-bit register because this is not allowed by the microprocessor and
this will result in an error when assembled. For example, MOV AX, AL instruction is not
allowed because the registers are of different sizes.
None of the MOV instructions affect the flag bits. The flag bits are normally modified by
arithmetic or logic instructions.
The code segment register is not changed by a MOV instruction because the address of the
next instruction is found by both IP/EIP and CS. If only CS were changed, the address of the
next instruction would be unpredictable. Therefore, changing the CS register with a MOV
instruction is not allowed.
MOV BX, CX; This instruction moves (copies) 1234H from register CX into register BX.
This overwrites the old content (76AFH) of register BX, but the contents of CX remain
unchanged. The contents of the destination register or destination memory location change
for all instructions except the CMP and TEST instructions. Note that this instruction does not
affect the leftmost 16 bits of register EBX.
Register Relative Addressing: In this memory addressing mode, the data in a segment of
memory are addressed by adding the displacement to the contents of a base or an index
register.
Example: MOV AX, [BX + 4] This instruction loads AX from the data segment address
formed by BX plus 4.and MOV AX, ARRAY [BX] this instruction loads AX from the data
segment memory location in ARRAY plus the contents of BX.
Example: If register BX contains 1000H and the MOV AX, [BX] instruction executes, the
word contents of data segment offset address 1000H are copied into register AX.
The data segment is used by default with register indirect addressing or any other addressing
mode that uses BX, DI, or SI to address memory. The stack segment is used by default if the
BP register addresses memory.
MOV CX,[BX] 16 bits Copies the word contents of the data segment memory
location addressed by BX into CX
MOV [BP],DL 8 bits Copies DL into the stack segment memory location
addressed by BP
MOV [DI],BH 8 bits Copies BH into the data segment memory location
addressed by DI
RIP Relative Addressing: This mode allows access to any location in the memory system by
adding a 32-bit displacement to the 64-bit contents of the 64-bit instruction pointer.
Example, if RIP = 1000000000H and a 32-bit displacement is 300H, the location accessed is
1000000300H. MOV CX, [BX + SI + 50H] instruction executes a word from the memory address
33050H and stores in CX.
2. Immediate Addressing Mode: In this addressing mode, the data is immediately
transferred from source register to the destination register.
Immediate implies that the data immediately follow the hexadecimal opcode in the memory.
Immediate data are constant data, whereas the data transferred from a register or memory
location are variable data. Immediate addressing operates upon a byte or word of data.
Example: MOV EAX, 13456H This instruction copies 13456H from the instruction located
in the memory immediately following the hexadecimal opcode into register EAX.
The letter H appends hexadecimal data. If hexadecimal data begin with a letter, the assembler
requires that the data start with a 0. For example, to represent a hexadecimal F2, 0F2H is used
in assembly language.
An ASCII-coded character or characters may be depicted in the immediate form if the ASCII
data are enclosed in apostrophes.
Example: MOV BH, ‘A’ moves an ASCII-coded letter A [41H] into register BH.
Binary data are represented if the binary number is followed by the letter B.
Example: MOV AL, [1000H]; EA is given within square bracket in the instruction in this
addressing mode and hence EA=1000H in this instruction. Since the destination is an 8 -bit
register (i.e. AL), a byte is taken from memory at the address given by DS * 10H +
EA=31000H and stored in AL.
Direct Addressing: Direct addressing with a MOV instruction transfers data between a
memory location, located within the data segment, and the AL (8-bit), AX (l6-bit), or EAX
(32-bit) register.
A MOV instruction using this type of addressing is usually a 3-byte long instruction.
[1234H] is an absolute memory location. The effective address is formed by adding 1234H
(the offset address) and 10000H (the data segment address of 1000H times 10H) in a system
operating in the real mode.
Examples: MOV BL, [SI] instruction takes a byte from the memory address 31000H and
stores in BL.
EA = (SI) = 1000H
= 30000H + 1000H
= 31000H
Index Relative Addressing: This mode is same as base relative addressing mode except that
instead of BP or BX register, SI or DI register is used.
Example: MOV BX, [SI-100H] instruction takes a word from the memory address 30F00H
is taken and stores in BX.
EA = (SI) - 100H
= 30F00H
Scaled-Index Addressing: Scaled-index addressing uses two 32-bit registers (a base register
and an index register) to access the memory. The second register (index) is multiplied by a
scaling factor. The scaling factor can be 1x, 2x, 4x or 8x.
Example: MOV AX, [EDI + 2 x ECX] This instruction uses a scaling factor of 2x, which
multiplies the contents of ECX by 2 before adding it to the EDI register to form the memory
address. If ECX contains a 00000000H, word-sized memory element 0 is addressed and if
ECX contains a 00000001H, word-sized memory element 1 is accessed. This scales the index
(ECX) by a factor of 2 for a word-sized memory array.
EA = (BX) = 2000H
= 32000H
Base Relative Addressing: In this mode, the EA is obtained by adding the content of the
base register with an 8-bit or 16 bit displacement.
The displacement is a signed number with negative values represented in 2’s complement
form. The 16 bit displacement can have value from -32768 to +32767 and 8 bit displacement
can have the value from -128 to +127.
Example: MOV AX, [BX+5] instruction takes a word from the memory address 32005H
and stores in AX.
EA = (BX) + 5
= 30000H + 2000H + 5
= 32005H
This type of addressing uses one base register (BP or BX) and one index register (DI or SI) to
indirectly address memory. The base register often holds the beginning location of a memory
array whereas the index register holds the relative position of an element in the array.
A major use of the base-plus-index addressing mode is to address elements in a memory
array.
In this mode, the Effective Address (EA) is obtained by adding the content of a base register
and index register.
Example: Suppose DS = 3000H, BX = 2000H and SI = 1000H and MOV AX, [BX+SI]
instruction executes, then a word from the memory address 33000H is taken and stored in AX.
EA = (BX) + (SI)
= 33000H
In this mode, the EA is obtained by adding the content of a base register, an index and a
displacement.
Example: Suppose DS = 3000H, BX = 2000H and SI = 1000H and MOV CX, [BX + SI +
50H] instruction executes, then a word from the memory address 33050H is taken and stored
in CX.
EA = (BX) + (SI) + 50H
= 33050H
Program memory-addressing modes, used with the JMP (jump) and CALL instructions,
consist of 3 distinct forms:
The direct program memory addressing stores both the segment and offset address where the
control has to be transferred with the opcode.
Direct program memory addressing is used for all jumps and calls.
Example: when JMP 32000H instruction is executed, the 16 bit offset value 2000H is
loaded in IP register and the 16 bit segment value 3000H is loaded in CS. When the
microprocessor calculates the memory address from where it has to fetch an instruction using
the relation CS * 10H + IP, the address 32000H will be obtained using the above CS and IP
values.
An intersegment jump is a jump to any memory location within the entire memory system.
The direct jump is called a far jump because it can jump to any memory location for the next
instruction.
Relative Program Memory Addressing
The term relative here means relative to the instruction pointer (IP).
Relative JMP and CALL instructions contain either an 8 bit or a 16 bit signed displacement
that is added to the current instruction pointer and based on the new value of IP thus obtained,
the address of the next instruction to be executed is calculated using the relation CS * 10H +
IP.
All assemblers automatically calculate the distance for the displacement and select the proper
1-, 2- or 4-byte form.
The 8-bit or 16 bit signed displacement which allows a forward memory reference or a
reverse memory reference. An 8 bit displacement has a jump range of between +127 and -
128 bytes from the next instruction while a 16 bit displacement has a jump range of between -
32768 and +32767 bytes from the next instruction following the jump instruction in the
program.
While using assembler to develop 8086 program, the assembler directive SHORT and NEAR
PTR is used to indicate short jump and near jump instruction respectively. The opcode of
relative short jump and near jump instructions are EBH and E9H respectively.
Examples:
a) JMP SHORT TD
In the above examples, TD and ABHI are the labels of memory locations that are present in
the same code segment in which the above instructions are present.
Indirect Program Memory Addressing
The indirect jump or CALL instructions use either any 16 bit register (AX, BX, CX, DX, SP,
BP, SI or DI) or any relative register ([BP], [BX], [DI] or [SI]) or any relative register with
displacement.
If a 16-bit register holds the address of a JMP instruction, the jump is near.
Example: if the CX register contains 2000H and JMP CX instruction present in a code
segment is executed, the microprocessor jumps to offset address 2000H in the current code
segment to take the next instruction for execution.
If a relative register holds the address, the jump is also considered to be an indirect jump.
Example: JMP [BX] instruction refers to the memory location within the data segment at the
offset address contained in BX. The offset address is a 16-bit number that is used as the offset
address in the intrasegment jump. This type of jump is sometimes called an indirect-indirect
or double-indirect jump.
Example: when the instruction JMP [DI] is executed, the microprocessor first reads a word
in the current data segment from the offset address specified by DI and puts that word in the
IP register. Now using this new value of IP, 8086 calculates the address of the memory
location where it has to jump using the relation CS * 10H + IP.
STACK MEMORYADDRESSING MODES
The stack holds temporarily data and also stores return address for procedures and interrupt
service routines. The stack memory is a last-in-first-out (LIFO) memory. Data are placed into
the stack using PUSH instruction and taken out from the stack using POP instruction. The
CALL instruction uses the stack to hold the return address for procedures and RET
instruction is used to remove return address from stack.
The stack memory is maintained by two registers: the stack pointer and the stack segment.
Always a word is entered into stack. Whenever a word of data is pushed onto the stack, the
high-order 8 bits are placed in the location addressed by SP – 1. The low-order 8 bits are
placed in the location addressed by SP – 2. The SP is then decremented by 2.
The SP/ESP register always points to an area of memory located within the stack segment.
The SP/ESP register adds to SS * 10H to form the stack memory address in the real mode. In
protected mode operation, the SS register holds a selector that accesses a descriptor for the
base address of the stack segment.
Whenever data are popped from the stack, the low-order 8 bits are removed from the location
addressed by SP. The high-order 8 bits are removed from the location addressed by SP + 1.
The SP register is then incremented by 2.
Since SP gets decremented for every push operation, the stack segment is said to be growing
downwards as for successive push operations, the data are stored in lower memory addresses
in stack segment. Due to this, the SP is initialized with highest offset address according to the
requirement, at the beginning of the program.
Data may be popped off the stack into any register or any segment register except CS. The
reason that data may not be popped from the stack into CS is that this only changes part of
the address of the next instruction.
The PUSHA and POPA instructions either push or pop all of the registers, except segment
registers, onto the stack.
Example PUSH and POP instructions
POPF Removes a word from the stack and places it into the flag
register
POPFD Removes a double-word from the stack and places it into the
EFLAG register
POP BX Removes a word from the stack and places it into the BX
register
PUSHA Copies AX, CX, DX, BX, SP, BP, DI, and SI to the stack
POPA Removes the word contents for the following registers from
the stack: SI, DI, BP, SP, BX, DX, CX, and AX
The segment override prefix allows the programmer to deviate from the default segment and
offset register mechanism.
The jump and call instructions cannot be prefixed with the segment override prefix since they
use only code segment register (CS) for address generation.
Example: MOV AX, [BP] instruction accesses data within stack segment by default since
BP is the offset register for stack segment. But if the programmer wants to get data from data
segment using BP as offset register in the above instruction, then the instruction is modified
as MOV AX, DS: [BP].
Data Movement Instruction
Machine language is the native binary code that the microprocessor understands and uses as
its instructions to control its operation. Machine language instructions vary in length from 1
byte to 13 bytes.
Opcode 1–2 bytes MOD-REG-R/M 0–1 Displacement 0–1 Immediate 0–2 bytes
bytes bytes
(a) 16-bit instruction mode
The first 2 bytes of the 32-bit instruction mode format are called override prefixes because
they are not always present. The first modifies the size of the operand address used by the
instruction and the second modifies the register size. If the microprocessor operates in 16-bit
instruction mode with a 32-bit register, the register-size prefix (66H) is appended to the
front of the instruction.
The l6-bit instruction mode uses 8- and l6-bit registers and addressing modes, while the 32-
bit instruction mode uses 8- and 32-bit registers and addressing modes by default. The
prefixes override these defaults so that a 32-bit register can be used in the l6-bit mode or a l6-
bit register can be used in the 32-bit mode.
The mode of operation (16 or 32 bits) should be selected to function with the current
application. Mode selection is a function of the operating system.
Note: DOS can operate only in the l6-bit mode, where Windows can operate in both modes.
Opcode: The opcode selects the operation that is performed by the microprocessor. The
opcode is either 1 or 2 bytes long for most machine language instructions.
opcode
D W
Byte 1 of many machine language instructions
The first 6 bits of the first byte are the binary opcode. The remaining 2 bits indicate the
direction (D) of the data flow, and indicate whether the data are a byte or a word (W). In the
80386 and above, words and double-words are both specified when W = 1. The instruction
mode and register-size prefix (66H) determine whether W represents a word or a double-
word.
If the direction bit D = 1, data flow to the register REG field from the R/M field located
in the second byte of an instruction. If D = 0 in the opcode, data flow to the R/M field
from the REG field. If W = 1, the data size is a word or double-word and if W = 0, the
data size is always a byte.
MOD Field: The MOD field specifies the addressing mode (MOD) for the selected
instruction. The MOD field selects the type of addressing and whether a displacement is
present with the selected type.
MOD field for the 16-bit instruction mode
MOD Function
00 No displacement
01 8-bit sign-extended displacement
10 16-bit signed displacement
11 R/M is a register
Example: MOV AL, [DI] instruction is an example that contains no displacement, MOV
AL,[DI + 2] instruction uses an 8-bit displacement (+2), and MOV AL,[DI + 1000H]
instruction uses a 16-bit displacement (+1000H).
All 8-bit displacements are sign-extended into 16-bit displacements when the
microprocessor executes the instruction.
(c) To sign-extend a number, its sign-bit is copied to the next higher-order byte, which
generates either a 00H or an FFH in the next higher-order byte.
MOD Function
00 No displacement
01 8-bit sign-extended displacement
10 32-bit signed displacement
11 R/M is a register
MOD field is interpreted as selected by the address-size override prefix or the operating mode
of the microprocessor.
When the MOD field is a 10, this causes the 16-bit displacement to become a 32-bit
displacement, to allow any protected mode memory location (4G bytes) to be accessed.
If the microprocessor is operated in the 16-bit instruction mode, this instruction is converted
to binary and placed in the instruction format of bytes 1 and 2.
opcode DW
1 0 0 0 1 0 1 1
1 1 1 0 1 1 0 0
Both the D and W bits are a logic 1, which means that a word moves into the destination
register specified in the REG field. The REG field contains a 101, indicating register BP, so
the MOV instruction moves data into register BP. The MOD field contains 11, the R/M (R/M
= 100) field also indicates a register (SP). Therefore, the instruction is MOV BP, SP that
moves data from SP into BP.
Example: Suppose that a 668BE8H instruction is operated in the 16-bit instruction mode.
The first byte (66H) is the register-size override prefix that selects 32-bit register operands for
the 16-bit instruction mode. The remainder of the instruction indicates that the opcode is a
MOV with a source operand of EAX and a destination operand of EBP. This instruction is a
MOV EBP, EAX. The same instruction becomes a MOV BP, AX instruction if it is operated
in the 32-bit instruction mode because the register-size override prefix selects a 16-bit
register.
REG and R/M (when) MOD = 11 assignments
R/M Memory Addressing: If MOD = 00 and R/M = 101, the addressing mode is [DI]. If
MOD = 01 or 10, the addressing mode is [DI + 33H] or LIST [DI + 22H] for the 16 -bit
instruction mode.
Example: MOV DL, [DI] instruction is 2 bytes long and has an opcode 100010, D = 1 (to
REG from R/M), W = 0 (byte), MOD = 00 (no displacement), REG = 010 (DL), and R/M =
101 ([DI]).
opcode D W
1 0 0 0 1 0 1 0
MOD REG R/M
0 0 0 1 0 1 0 1
Special Addressing Mode: It occurs whenever memory data are referenced by only the
displacement mode of addressing for 16-bit instructions.
32-Bit Addressing Modes: The 32-bit addressing modes found in the 80386 and above are
obtained by either running these machines in the 32-bit instruction mode or in the 16-bit
instruction mode by using the address-size prefix 67H.
When R/M = 100, an additional byte called a scaled-index byte appears in the instruction.
The scaled-index byte is mainly used when two registers are added to specify the memory
address in an instruction. Because the scaled-index byte is added to the instruction, there are
7 bits in the opcode and 8 bits in the scaled-index byte to define. This means that a scaled-
index instruction has 215 (32K) possible combinations.
ss index base
Above figure shows the format of the scaled-index byte as selected by a value of 100 in the
R/M field of an instruction when the 80386 and above use a 32-bit address. The leftmost 2
bits select a scaling factor (multiplier) of 1*, 2*, 4*, 8*. A scaling factor is implicit if none is
used in an instruction that contains two 32-bit indirect address registers.
Example: The instruction MOV EAX, [EBX + 4*ECX] is encoded as 67668B048BH. Both
the address size (67H) and register size (66H) override prefixes appear in this instruction.
Immediate Instruction: MOV WORD PTR [BX + 1000H], 1234H instruction moves a
1234H into the word-sized memory location addressed by the sum of 1000H, BX, and DS *
10H. This 6-byte instruction uses 2 bytes for the opcode, W, MOD, and R/M fields. Two of
the 6 bytes are the data of 1234H and rest of the 2 of 6 bytes are for the displacement of
1000H.
Segment MOV Instructions: If the contents of a segment register are moved by the MOV,
PUSH, or POP instructions, a special set of register bits (REG field) selects the segment
register.
opcode D W
1 0 0 0 1 1 0 0
1 1 0 0 1 0 1 1
64- Bit Mode: In the 64-bit mode, an additional prefix called REX (register extension) is
added. The REX prefix, which is encoded as a 40H–4FH, follows other prefixes and is placed
immediately before the opcode to modify it for 64-bit operation. The purpose of the REX
prefix is to modify the REG and R/M fields in the second byte of the instruction. REX is
needed to be able to address registers R8 through R15.
The REG field can only contain register assignments as in other modes of operation and the
R/M field contains either a register or memory assignment.
As with 32-bit instructions, the modes allowed by the scaled-index byte are fairly all
conclusive allowing pairs of registers to address memory and also an index factor of 2*, 4* or
8*.
Example: MOV RAXW, [RDX + RCX – 12] instruction requires the scaled-index byte with
an index of 1.
The 64-bit register and memory designators for rrrr and mmmm
The PUSH and POP instructions stores and retrieves data from the LIFO (last-in-first-out)
stack memory.
The microprocessor has 6 forms of the PUSH and POP instructions: register, memory,
immediate, segment register, flags, and all registers.
3. Immediate addressing: It allows immediate data to be pushed onto the stack, but not
6. All register: It’s content may be pushed or popped from the stack.
PUSH
PUSH instruction always transfers 2 bytes of data to the stack. The source of the data may be
any internal 16- or 32-bit register, immediate data, any segment register, or any 2 bytes of
memory data.
PUSHF (Push Flags) instruction copies the contents of the flag register to the stack.
PUSHAD and POPAD instructions push and pop the contents of the 32-bit register set.
Whenever data are pushed onto the stack, the first (most-significant) data byte moves to the
stack segment memory location addressed by SP – 1 and the second (least-significant) data
byte moves into the stack segment memory location addressed by SP - 2. After the data are
stored by a PUSH, the contents of the SP register decrement by 2.
PUSHA (Push All) instruction copies the contents of the internal register set, except the
segment registers, to the stack. This instruction copies the registers to the stack in the
following order: AX, CX, DX, BX, SP, BP, SI, and DI.
PUSHA instruction pushes all the internal 16-bit registers onto the stack. This instruction
requires 16 bytes of stack memory space to store all eight 16-bit registers. After all registers
are pushed, the contents of the SP register are decremented by 16.
PUSHA instruction is very useful when the entire register set must be saved during a task.
PUSH immediate data instruction has 2 different opcodes, but in both cases, a 16-bit
immediate number moves onto the stack and if PUSHD is used, a 32-bit immediate number is
pushed. If the values of the immediate data are 00H–FFH, the opcode is a 6AH and if the data
are 0100H–FFFFH, the opcode is 68H.
Example of PUSH immediate is the PUSH ‘A’ instruction, which pushes a 0041H onto the
stack.
POP
POP instruction removes data from the stack and places it into the target 16-bit register,
segment register, or a 16- bit memory location.
POPF (Pop Flags) instruction removes a 16-bit number from the stack and places it into the
flag register.
POPFD removes a 32-bit number from the stack and places it into the extended flag register.
POPA (Pop All) instruction removes 16 bytes of data from the stack and places them into the
following registers, in the order shown: DI, SI, BP, SP, BX, DX, CX, and AX. This is the
reverse order from the way they were placed on the stack by the PUSHA instruction, causing
the same data to return to the same registers.
Example: Suppose that a POP BX instruction executes. The first byte of data removed from
the stack moves into register BL and the second byte moves into register BH. After both
bytes are removed from the stack, the SP register is incremented by 2.
POP CS instruction is not a valid instruction in the instruction set. If a POP CS instruction
executes, only a portion of the address (CS) of the next instruction changes. This makes the
POP CS instruction unpredictable and therefore not allowed.
When the stack area is initialized, load both the stack segment (SS) register and the stack
pointer (SP) register. It is normal to designate an area of memory as the stack segment by
loading SS with the bottom location of the stack segment.
Note: All segments are cyclic in nature i.e. the top location of a segment is contiguous with
the bottom location of the segment.
LOAD EFFECTIVE ADDRESS
There are several load-effective address instructions in the microprocessor instruction set.
Some of them are LEA, LDS, LES, LSS, LFS and LGS.
LEA
LEA instruction loads any 16-bit register with the offset address, as determined by the
addressing mode selected for the instruction.
In other words, this instruction determines the offset address of the variable or memory
location named as the source and puts this offset address in the indicated 16 bit register.
Example: LEA BX, ABHI instruction loads BX with offset address of ABHI in data
segment where ABHI is the name assigned to a memory location in data segment.
Example: LEA AX, [BX][SI] instruction loads AX with the value equal to (BX)+(SI) where
(BX) and (SI) represents content of BX and SI respectively.
By comparing LEA with MOV, we observe that LEA BX,[DI] loads the offset address
specified by [DI] (contents of DI) into the BX register; MOV BX,[DI] loads the data stored
at the memory location addressed by [DI] into register BX. The OFFSET directive performs
the same function as an LEA instruction if the operand is a displacement.
Example: MOV BX, OFFSET COST performs the same function as LEA BX, COST.
Both instructions load the offset address of memory location LIST into the BX register.
LDS
LDS instruction load any 16-bit or 32-bit register with an offset address and DS segment
register with a segment address.
In other words, this instruction copies a word from the memory location specified in the
instruction into the register and then copies a word from the next memory location into the
DS register.
The general form of LDS instruction is LDS register, memory address of first word
LDS is useful for initializing SI and DS registers at the start of a string before using one of
the String instructions.
Example: LDS SI,[2000H] instruction copies the content of memory at offset address 2000H
in data segment to lower byte of SI, content of 2001H to higher byte of SI, content of 2002H
to lower byte of DS and 2003H to higher byte of DS.
LDS instruction use any of the memory-addressing modes to access a 32-bit or 48-bit section
of memory that contains both the segment and offset address. This instruction may not use
the register addressing mode (MOD = 11).
In the 64-bit mode, LDS instructions are invalid and not used because the segments have no
function in the flat memory model.
LES
LES instruction load any 16-bit or 32-bit register with an offset address and ES segment
register with a segment address.
The general form of LES instruction is LES register, memory address of first word
LES instruction use any of the memory-addressing modes to access a 32-bit or 48-bit section
of memory that contains both the segment and offset address. This instruction may not use
the register addressing mode (MOD = 11).
In the 64-bit mode, LES instructions are invalid and not used because the segments have no
function in the flat memory model.
LSS
LSS instruction load any 16-bit or 32-bit register with an offset address and SS segment
register with a segment address.
The general form of LSS instruction is LSS register, memory address of first word
LSS instruction use any of the memory-addressing modes to access a 32-bit or 48-bit section
of memory that contains both the segment and offset address. This instruction may not use
the register addressing mode (MOD = 11).
LFS
LFS instruction load any 16-bit or 32-bit register with an offset address and FS segment
register with a segment address.
LFS instruction use any of the memory-addressing modes to access a 32-bit or 48-bit section
of memory that contains both the segment and offset address. This instruction may not use
the register addressing mode (MOD = 11).
LGS
LGS instruction load any 16-bit or 32-bit register with an offset address and GS segment
register with a segment address.
LGS instruction use any of the memory-addressing modes to access a 32-bit or 48-bit section
of memory that contains both the segment and offset address. This instruction may not use
the register addressing mode (MOD = 11).
The LDS, LES, LFS, LGS, and LSS instructions obtain a new far address from memory.
The offset address appears first, followed by the segment address. This format is used for
storing all 32-bit memory addresses.
Example: ADDR DD FAR PTR HORSE instruction stores the offset and segment address
(far address) of HORSE in 32 bits of memory at location ADDR. The DD directive tells the
assembler to store a double-word (32-bit number) in memory address ADDR.
Q: Why is the LEA instruction available if the OFFSET directive accomplishes the
same task?
A: OFFSET only functions with simple operands such as LIST. It may not be used for an
operand such as [DI], LIST [SI], and so on. The OFFSET directive is more efficient than the
LEA instruction for simple operands. It takes the microprocessor longer to execute the LEA
BX, LIST instruction than the MOV BX, OFFSET LIST because the assembler calculates
the offset address of LIST, whereas the microprocessor calculates the address for the LEA
instruction.
Suppose that the microprocessor executes an LEA BX,[DI] instruction and DI contains a
1000H. Because DI contains the offset address, the microprocessor transfers a copy of DI into
BX. Thus, MOV BX, DI instruction performs this task in less time and is often preferred to
the LEA BX,[DI] instruction.
STRING DATA TRANSFERS
There are 5 string data transfer instructions: LODS, STOS, MOVS, INS, and OUTS. Each
string instruction allows data transfers that are single byte, word, or double-word.
Direction Flag
Direction flag (D) selects the auto-increment (D = 0) or the auto decrement (D = 1) operation
for the DI and SI registers during string operations.
The CLD instruction clears the D flag (D = 0) and the STD instruction sets it (D = 1).
Therefore, the CLD instruction selects the auto-increment mode (D = 0) and STD selects the
auto-decrement mode (D = 1).
Whenever a string instruction transfers a byte, the contents of DI and/or SI are incremented or
decremented by 1. If a word is transferred, the contents of DI and/or SI are incremented or
decremented by 2. Double-word transfers cause DI and/or SI to increment or decrement by 4.
Only the actual registers used by the string instruction are incremented or decremented.
Example: STOSB instruction uses the DI register to address a memory location. When
STOSB executes, only the DI register is incremented or decremented without affecting SI
register. The same is true for LODSB instruction, which uses the SI register to address
memory data. A LODSB instruction will only increment or decrement SI without affecting
DI register.
DI and SI
During the execution of a string instruction, memory accesses occur through either or both of
the DI and SI registers.
The DI offset address accesses data, by default, in the extra segment while the SI offset
address accesses data, by default, in the data segment for all string instructions that use it.
The segment assignment of SI may be changed with a segment override prefix. The DI
segment assignment is always in the extra segment when a string instruction executes. This
assignment cannot be changed.
LODS
The LODS instruction loads AL, AX, or EAX with data stored at the data segment offset
address indexed by the SI register. After loading AL with a byte, AX with a word, or EAX
with a double-word, the contents of SI increment, if D = 0 or decrement, if D = 1.
STOS
The STOS instruction stores AL, AX, or EAX at the extra segment memory location
addressed by the DI register. STOS instruction may be appended with a B, W, or D for byte,
word, or double-word transfers.
Repeat Prefix (REP) is added to any string data transfer instruction, except the LODS
instruction. REP prefix causes CX to decrement by 1 each time the string instruction executes.
After CX decrements, the string instruction repeats. If CX reaches a value of 0, the
instruction terminates and the program continues with the next sequential instruction. Thus, if
CX is loaded with 100 and a REP STOSB instruction executes, the microprocessor
automatically repeats the STOSB instruction 100 times. Because the DI register is
automatically incremented or decremented after each datum is stored, this instruction stores
the contents of AL in a block of memory instead of a single byte of memory.
MOVS instruction transfers data from one memory location to another. It transfers a byte,
word, or double-word from the data segment location addressed by SI to the extra segment
location addressed by SI.
Only the source operand (SI), located in the data segment, may be overridden so that another
segment may be used. The destination operand (DI) must always be located in the extra
segment.
INS instruction transfers a byte, word, or double-word of data from an I/O device into the
extra segment memory location addressed by the DI register. The I/O address is contained in
the DX register.
This instruction is useful for inputting a block of data from an external I/O device directly
into the memory.
1. INSB instruction inputs data from an 8-bit I/O device and stores it in the byte-sized
2. INSW instruction inputs 16-bit I/O data and stores it in a word-sized memory location.
These instructions can be repeated using the REP prefix, which allows an entire block of
input data to be stored in the memory from an I/O device.
OUTS instruction transfers a byte, word, or double-word of data from the data segment
memory location address by SI to an I/O device. The I/O device is addressed by the DX
register as it is with the INS instruction.
Some miscellaneous data transfer instructions are XCHG, LAHF, SAHF, XLAT, IN, OUT,
BSWAP, MOVSX, MOVZX, and CMOV.
XCHG
XCHG (exchange) instruction exchanges the contents of a register with the contents of any
other register or memory location. It cannot exchange directly the contents of two memory
locations. Exchanges are byte-, word-, or doubleword-sized and they use any addressing
mode except immediate addressing mode. The segment registers cannot be used in this
instruction.
Example: XCHG AL, BL instruction exchanges content of AL and BL and XCHG AX,
[BX] instruction exchanges content of AX with content of memory at [BX].
LAHF
LAHF instruction transfers the low byte (rightmost 8 bits) of the flag register into the AH
register.
SAHF
SAHF instruction transfers the AH register into the low byte (rightmost 8 bits) of the flag
register.
XLAT (translate) instruction converts the content of the AL register into a number stored in a
memory table. This instruction is used to translate a byte in AL from one code to another
code. An XLAT instruction first adds the contents of AL to BX to form a memory address
within the data segment. It then copies the contents of this address into AL. This is the only
instruction that adds an 8-bit number to a 16-bit number.
IN/OUT
IN instruction transfers data from an external I/O device (port) into AL, AX, or EAX whereas
OUT instruction transfers data from AL, AX, or EAX to an external I/O device (port).
Both IN and OUT instructions have 2 forms of I/O device (port): fixed port and variable port.
Fixed-port addressing: It allows data transfer between AL, AX, or EAX using an 8-bit I/O
port address. It is called fixed-port addressing because the port number follows the
instruction’s opcode. A fixed port instruction stored in ROM has its port number permanently
fixed. With this form, anyone of 256 possible ports can be addressed.
Example: IN AL, 80H instruction input a byte from the port with address 80H to AL.
Variable-port addressing: It allows data transfers between AL, AX, or EAX and a 16-bit
port address. It is called variable-port addressing because the I/O port number is stored in
register DX, which can be changed during the execution of a program. Since DX is a 16 bit
register, the port address can be any number between 0000H and FFFFH. Hence up to 65536
ports are addressable in this mode.
Example: MOV DX, 0FE50H instruction initializes DX with port address of FE50H and IN
AX, DX instruction input a word from 16-bit port with port address FE50H into AX.
MOVSX and MOVZX
These instructions move data and at the same time either sign- or zero- extend it.
When a number is zero-extended, the most significant part fills with zeros.
Zero-extension is used to convert unsigned 8- or 16-bit numbers into unsigned 16- or 32-bit
numbers by using the MOVZX instruction.
When a number is sign-extended, its sign-bit is copied into the most significant part.
Sign extension is used to convert 8- or 16-bit signed numbers into 16- or 32-bit signed
numbers by using the MOVSX instruction.
Example: if an 8-bit 84H is sign-extended into a 16-bit number, it becomes FF84H. The
sign-bit of an 84H is 1, which is copied into the most significant part of the sign-extended
result.
BSWAP
This instruction takes the contents of any 32-bit register and swaps first byte with fourth byte
and second byte with the third byte.
Example: BSWAP EAX instruction with EAX = 00112233H swaps bytes in EAX, resulting
in EAX = 33221100H.
CMOV
This instruction (conditional move) moves the data only if the condition is true.
Example: CMOVZ instruction moves data only if the result from some prior instruction was
a zero.
Conditional move instructions
Arithmetic Instructions
Whenever arithmetic and logic instructions execute, the contents of the flag register change
while the contents of the interrupt, trap, and other flags do not change.
Only the flags located in the rightmost 8 bits of the flag register and the overflow flag
changes. These rightmost flags denote the result of the arithmetic or a logic operation.
ADD (Addition): This instruction adds the data from the source and destination and the
result is placed in the destination.
The source can be an immediate number, a register or a memory location while the
destination can be a register or memory location.
The data from the source and destination must be of the same type either bytes or words.
Example: ADD AX, CX It adds the content of AX and CX and stores the result in AX.
ADD AL, [BX] It adds the content of AL and byte from memory at [BX] and store result in
AL.
AF, CF, OF, PF, SF and ZF flags are affected by the execution of ADD instruction.
ADC (Addition with carry): This instruction adds the data in source and destination along
with the content of carry flag and stores the result in the destination.
Example: ADC CX, BX It adds the content of CX, BX and the carry flag and stores result in
CX.
ADI (Add Immediate): This addition instruction adds the immediate data from source,
which is a constant, to destination and stores the result in the destination.
Example: ADI DL, 15H It adds the content of DL and 15H and stores the result in DL.
ACI (Add Immediate with carry): This instruction adds the immediate data from source to
destination along with carry and stores the result in the destination.
Example: ACI DL, 43H It adds the content of DL, 43H and carry and stores result in DL.
INC (Increment Addition): This instruction adds 1 to any register or memory location,
except a segment register.
Example: INC BX It adds 1 to the content of BX and stores the result in BX.
XADD (Exchange and Add): This instruction adds the source to destination and stores the
result in the destination while copying the original value of destination into source.
Example: If BX = 15H and CX = 28H and XADD BX, CX instruction executes, the BX
register contains the sum of 43H and CX becomes 15H.
SUB (Subtraction): This instruction subtracts the content of source from the content of
destination and stores the result in destination.
For subtraction, the carry flag (CF) functions as borrow flag. If the result is negative after
subtraction, CF is set, otherwise it is reset.
AF, CF, OF, PF, SF and ZF flags are affected by SUB instruction.
Example: SUB AX, BX It subtracts the content of BX from AX and stores the result in AX.
SBB (Subtraction with Borrow): This instruction subtracts the content of source and
content of carry flag from the content of destination and stores the result in destination.
Example: SBB AX, BX It subtracts the content of BX along with the content of carry flag
from AX and stores the result in AX.
SUI (Subtract Immediate): This instruction subtracts the data of source, which is a constant,
from the destination and stores the result in destination.
Example: SUI DL, 15H It subtracts the content of 15H from DL and stores result in DL.
SBI (Subtract Immediate with Borrow): This instruction subtracts the immediate data of
source as well as content of carry flag from destination and stores the result in destination.
Example: SBI DL, 43H It subtracts 43H and carry flag from DL and stores result in DL.
DEC (Decrement Subtraction): This instruction subtracts 1 from a register or the contents
of a memory location.
Example: DEC BX It subtracts 1 from the content of BX and stores the result in BX.
Program Control Instructions
JUMP GROUP
The main program control instruction, jump (JMP), allows the programmer to skip sections
of a program and branch to any part of the memory for the next instruction.
1. Unconditional Jump
2. Conditional Jump
a. Short Jump
b. Near Jump
c. Far Jump
Short Jump
Short jump is a 2-byte instruction that allows jumps or branches to memory locations within
+127 and –128 bytes from the address following the jump.
Short jumps are called relative jumps because they can be moved to any location in the
current code segment without a change.
Short jump stores a distance or displacement which follows the opcode instead of the jump
address. The short jump displacement is a distance represented by a 1-byte signed number
whose value ranges between +127 and -128.
When the microprocessor executes a short jump, the displacement is sign-extended and added
to the instruction pointer (IP/EIP) to generate the jump address within the current code
segment.
A label is a symbolic name for a memory address. Whenever a jump instruction references an
address, a label normally identifies the address. For example, JMP NEXT instruction jumps
to label NEXT for the next instruction.
Near Jump
Near jump is a 3-byte instruction that allows a branch or jump within ±32K bytes (or
anywhere in the current code segment) from the instruction in the current code segment.
The near jump contains an opcode followed by a signed 16-bit displacement. The signed
displacement adds to the instruction pointer (IP) to generate the jump address.
Near jump is also relocatable because it is also a relative jump means that if the code
segment moves to a new location in the memory then the distance between the jump
instruction and the operand address remains the same.
Note: Segments are cyclic in nature, which means that one location above offset address
FFFFH is offset address 0000H. So, if we jump 2 bytes ahead in memory and the instruction
pointer addresses offset address FFFFH then the flow continues at offset address 0001H.
Far Jump
Far jump is a 5-byte instruction that allows a jump to any memory location within the real
memory system.
Far jump instruction obtains a new segment and offset address to accomplish the jump.
The bytes 2 and 3 of this 5-byte instruction contain the new offset address while bytes 4 and 5
contain the new segment address.
If the microprocessor is operated in the protected mode, the segment address accesses a
descriptor that contains the base address of the far jump segment. The offset address, which is
either 16 or 32 bits, contains the offset address within the new code segment.
The jump instruction can also use a 16- or 32-bit register as an operand. This automatically
sets up the instruction as an indirect jump because the address of the jump is in the register
specified by the jump instruction and the contents of the register are transferred directly into
the instruction pointer.
Example: JMP AX instruction copies the contents of the AX register into the IP when the
jump occurs.
The jump instruction may also use the [ ] form of addressing to directly access the jump table.
The jump table can contain offset addresses for near indirect jumps, or segment and offset
addresses for far indirect jumps. This type of jump is also known as a double-indirect jump
if the register jump is called an indirect jump. The assembler assumes that the jump is near
unless the FAR PTR directive indicates a far jump instruction.
Example: JMP TABLE [SI] instruction points to a jump address stored at the code segment
offset location addressed by SI and jumps to the address stored in the memory at this location.
Conditional Jump
Conditional jump instructions are always short jumps in the 8086 through the 80286
microprocessors. This limits the range of the jump to within +127 bytes and -128 bytes from
the location following the conditional jump. In the 80386 and above, conditional jumps are
either short or near jumps (±32K).
The conditional jump instructions test the following flag bits: sign (S), zero (Z), carry (C),
parity (P), and overflow (O). If the condition under test is true, a branch to the label
associated with the jump instruction occurs and if the condition is false, the next sequential
step in the program executes.
The conditional set instructions either set a byte to 01H or clear a byte to 00H, depending on
the outcome of the condition under test.
Conditional LOOPs
LOOPE (loop while equal) instruction jumps if CX != 0 while an equal condition exists. It
will exit the loop if the condition is not equal or if the CX register decrements to 0.
LOOPNE (loop while not equal) instruction jumps if CX != 0 while a not-equal condition
exists. It will exit the loop if the condition is equal or if the CX register decrements to 0.
WHILE Loops
The .WHILE statement is used with a condition to begin the loop and the .ENDW statement
ends the loop.
REPEAT-UNTIL Loops
A series of instructions is repeated until some condition occurs. The .REPEAT statement
defines the start of the loop and the end is defined with the .UNTIL statement, which
contains a condition.
Procedures
A procedure is a group of instructions that usually performs one task. It is a reusable section
of the software that is stored in memory once but used as often as necessary. So, this saves
memory space and makes it easier to develop software.
CALL instruction links to the procedure and the RET (return) instruction returns from the
procedure.
The stack stores the return address whenever a procedure is called during the execution of a
program. The CALL instruction pushes the address of the instruction following the CALL
(return address) on the stack. The RET instruction removes an address from the stack so the
program returns to the instruction following the CALL.
With the assembler, there are specific rules for storing procedures. A procedure begins with
the PROC directive and ends with the ENDP directive. Each directive appears with the name
of the procedure. The PROC directive is followed by the type of procedure: NEAR or FAR.
USES statement allows any number of registers to be automatically pushed to the stack and
popped from the stack within the procedure.
The near return instruction uses opcode C3H and the far return uses opcode CBH. A near
return removes a 16-bit number from the stack and places it into the instruction pointer to
return from the procedure in the current code segment. A far return removes a 32-bit number
from the stack and places it into both IP and CS to return from the procedure to any memory
location.
Procedures that are to be used by all software (global) should be written as far procedures
while the procedures that are used by a given task (local) are normally defined as near
procedures.
CALL
The CALL instruction transfers the flow of the program to the procedure. The CALL
instruction differs from the jump instruction because a CALL saves a return address on
stack. The return address returns control to the instruction that immediately follows the
CALL in a program when a RET instruction executes.
Near CALL
The near CALL is a 3 bytes long instruction with first byte contains the opcode, and the
second and third bytes contain the displacement, or distance of ±32K.
When the near CALL executes, it first pushes the offset address of the next instruction onto
the stack. The offset address of the next instruction appears in the instruction pointer (IP or
EIP). After saving this return address, it then adds the displacement from bytes 2 and 3 to the
IP to transfer control to the procedure. There is no short CALL instruction.
Ans: The instruction pointer always points to the next instruction in the program. For the
CALL instruction, the contents of IP/EIP are pushed onto the stack, so program control
passes to the instruction following the CALL after a procedure ends.
Far CALL
The far CALL is a 5-byte instruction with bytes 2 and 3 contain the new contents of the IP,
and bytes 4 and 5 contain the new contents for CS.
The far CALL instruction is like a far jump because it can call a procedure stored in any
memory location in the system.
This instruction places the contents of both IP and CS on the stack before jumping to the
address indicated by bytes 2 through 5 of the instruction. This allows the far CALL to call a
procedure located anywhere in the memory and return from that procedure.
RET
The return instruction removes a 16-bit number (near return) from the stack and places it into
IP, or removes a 32-bit number (far return) and places it into IP and CS.
Far return removes 6 bytes from the stack. The first 4 bytes contain the new value for EIP
and the last 2 contain the new value for CS.
Near return removes 4 bytes from the stack and places them into EIP.
Interrupt
Interrupt Vectors
An interrupt vector is a 4-byte number stored in the first 1024 bytes of the memory (00000H–
003FFH) when the microprocessor operates in the real mode.
In the protected mode, the vector table is replaced by an interrupt descriptor table that uses 8 -
byte descriptors to describe each of the interrupts.
There are 256 different interrupt vectors, and each vector contains the address of an interrupt
service procedure. Each vector contains a value for IP and CS that forms the address of the
interrupt service procedure. The first 2 bytes contain the IP and the last 2 bytes contain the
CS.
Interrupt Instructions
The microprocessor has 3 different interrupt instructions that are available to the programmer:
INT, INTO, and INT 3.
In the real mode, each of these instructions fetches a vector from the vector table, and then
calls the procedure stored at the location addressed by the vector.
In the protected mode, each of these instructions fetches an interrupt descriptor from the
interrupt descriptor table. The descriptor specifies the address of the interrupt service
procedure.
The interrupt call is similar to a far CALL instruction because it places the return address
(IP/EIP and CS) on the stack.
Each INT instruction has a numeric operand whose range is 0 to 255 (00H–FFH).
Example: INT 100 uses interrupt vector 100, which appears at memory address 190H–193H.
The address of the interrupt vector is determined by multiplying the interrupt type number by
4.
Example: INT 10H instruction calls the interrupt service procedure whose address is stored
beginning at memory location 40H (10H * 4) in the real mode.
In the protected mode, the interrupt descriptor is located by multiplying the type number by 8
instead of 4 because each descriptor is 8 bytes long.
Each INT instruction is 2 bytes long. The first byte contains the opcode and the second byte
contains the vector type number.
The INT instruction performs as a far CALL except that it not only pushes CS and IP onto
the stack but it also pushes the flags onto the stack.
The INT instruction performs the operation of a PUSHF, followed by a far CALL
instruction.
The INT instruction is 2 bytes long, whereas the far CALL is 5 bytes long.
When the INT instruction executes, it clears the interrupt flag (I), which controls the external
hardware interrupt input pin INTR (interrupt request). When I = 0, the microprocessor
disables the INTR pin and when I = 1, the microprocessor enables the INTR pin.
Software interrupts are most commonly used to call system procedures because the address of
the system function need not be known. The system procedures are common to all system and
application software. The interrupts often control printers, video displays, and disk drives.
IRET/IRETD
Interrupt Return (IRET) instruction is used only with software or hardware interrupt service
procedures.
The IRET instruction accomplishes the same tasks as the POPF followed by a far RET
instruction.
Whenever an IRET instruction executes, it restores the contents of I and T from the stack
because it preserves the state of these flag bits.
IRETD instruction is used to return from an interrupt service procedure that is called in the
protected mode.
IRETD differs from IRET because it pops a 32-bit instruction pointer (EIP) from the stack.
IRET is used in the real mode while IRETD is used in the protected mode.
INT 3
It is common to insert an INT 3 instruction in software to interrupt or break the flow of the
software.
INTO
Interrupt on overflow (INTO) is a conditional software interrupt that tests the overflow flag
(O). The INTO instruction appears in software that adds or subtracts signed binary numbers.
Interrupt Control
The Set Interrupt (STI) instruction enables INTR and the Clear Interrupt (CLI)
instruction disables INTR.
Miscellaneous Instructions
HLT: The halt instruction (HLT) stops the execution of software. There are 3 ways to exit a
halt: by an interrupt, by a hardware reset, or during a DMA operation.
NOP: When the microprocessor encounters a no operation instruction (NOP), it takes a short
time to execute.
WAIT: This instruction tests the condition of the BUSY or TEST pin on the microprocessor.
If BUSY or TEST = 1, WAIT does not wait; but if BUSY or TEST = 0, WAIT continues
testing the BUSY or TEST pin until it becomes a logic 1.
LOCK: The LOCK prefix causes the LOCK pin to become logic 0 for the duration of the
locked instruction. The ESC instruction passes instruction to the numeric coprocessor.
BOUND: This instruction compares the contents of any 16-bit register against the contents of
2 words of memory: an upper and a lower boundary. If the value in the register compared
with memory is not within the upper and lower boundary, a type 5 interrupt ensues.
ENTER and LEAVE: The ENTER and LEAVE instructions are used with stack frames. A
stack frame is a mechanism used to pass parameters to a procedure through the stack memory.
The stack frame also holds local memory variables for the procedure. The ENTER instruction
creates the stack frame and the LEAVE instruction removes the stack frame from the stack.
The BP register addresses stack frame data.