Buses and operation All internal registers, as well as internal and external data buses, are 16 bits wide, which firmly established the "16-bit microprocessor" identity of the 8086. A 20-bit external address bus provides a 1
MiB physical address space (220 = 1,048,576 x 1
byte). This address space is addressed by means of internal memory "segmentation". The data bus is
multiplexed with the address bus in order to fit all of the control lines into a standard 40-pin
dual in-line package. It provides a 16-bit I/O address bus, supporting 64
KB of separate I/O space. The maximum linear address space is limited to 64 KB, simply because internal address/index registers are only 16 bits wide. Programming over 64 KB memory boundaries involves adjusting the segment registers (see below); this difficulty existed until the
80386 architecture introduced wider (32-bit) registers (the memory management hardware in the
80286 did not help in this regard, as its registers are still only 16 bits wide).
Hardware modes of 8086 Some of the control pins, which carry essential signals for all external operations, have more than one function depending upon whether the device is operated in
min or
max mode. The former mode is intended for small single-processor systems, while the latter is for medium or large systems using more than one processor (a kind of multiprocessor mode). Maximum mode is required when using an 8087 or 8089 coprocessor. The voltage on pin 33 (MN/) determines the mode. Changing the state of pin 33 changes the function of certain other pins, most of which have to do with how the CPU handles the (local) bus. The mode is usually hardwired into the circuit and therefore cannot be changed by software. The workings of these modes are described in terms of timing diagrams in Intel datasheets and manuals. In minimum mode, all control signals are generated by the 8086 itself.
Registers and instruction The 8086 has eight more-or-less general 16-bit
registers (including the
stack pointer but excluding the instruction pointer, flag register and segment registers). Four of them, AX, BX, CX, DX, can also be accessed as 8-bit register pairs (see figure) while the other four, SI, DI, BP, SP, are 16-bit only. Due to a compact encoding inspired by 8-bit processors, most instructions are one-address or two-address operations, which means that the result is stored in one of the operands. At most one of the operands can be in memory, but this memory operand can also be the
destination, while the other operand, the
source, can be either
register or
immediate. A single memory location can also often be used as both
source and
destination which, among other factors, further contributes to a
code density comparable to (and often better than) most eight-bit machines at the time. The degree of generality of most registers is much greater than in the 8080 or 8085. However, 8086 registers were more specialized than in most contemporary
minicomputers and are also used implicitly by some instructions. While perfectly sensible for the assembly programmer, this makes register allocation for compilers more complicated compared to more orthogonal 16-bit and 32-bit processors of the time such as the
PDP-11,
VAX,
68000,
32016, etc. On the other hand, being more regular than the rather minimalistic but ubiquitous 8-bit microprocessors such as the
6502,
6800,
6809,
8085,
MCS-48,
8051, and other contemporary accumulator-based machines, it is significantly easier to construct an efficient
code generator for the 8086 architecture. Another factor for this is that the 8086 also introduced some new instructions (not present in the 8080 and 8085) to better support stack-based high-level programming languages such as Pascal and
PL/M; some of the more useful instructions are
push mem-op, and
ret size, supporting the "Pascal
calling convention" directly. (Several others, such as
push immed and
enter, were added in the subsequent 80186, 80286, and 80386 processors.) A 64 KB (one segment)
stack growing towards lower addresses is supported in
hardware; 16-bit words are pushed onto the stack, and the top of the stack is pointed to by SS:SP. There are 256
interrupts, which can be invoked by both hardware and software. The interrupts can cascade, using the stack to store the
return addresses. The 8086 has 64 K of 8-bit (or alternatively 32 K of 16-bit word)
I/O port space.
Flags The 8086 has a 16-bit
flags register. Nine of these condition code flags are active, and indicate the current state of the processor:
Carry flag (CF),
Parity flag (PF),
Auxiliary carry flag (AF),
Zero flag (ZF),
Sign flag (SF),
Trap flag (TF),
Interrupt flag (IF),
Direction flag (DF), and
Overflow flag (OF). Also referred to as the status word, the layout of the flags register is as follows:
Segmentation There are also four 16-bit
segment registers (see figure) that allow the 8086
CPU to access one
megabyte of memory in an unusual way. Rather than concatenating the segment register with the address register, as in most processors whose address space exceeds their register size, the 8086 shifts the 16-bit segment four bits left before adding it to the 16-bit offset (16×segment + offset), therefore producing a 20-bit external (or effective or physical) address from the 32-bit segment:offset pair. As a result, any external address could be referred to by up to 212 = 4096 different segment:offset pairs. Although considered complicated and cumbersome by many programmers, this scheme also has advantages; a small program (less than 64 KB) can be loaded starting at a fixed offset (such as 0000) in its own segment, avoiding the need for
relocation, with at most 15 bytes of alignment waste. Compilers for the 8086 family commonly support two types of
pointer,
near and
far. Near pointers are 16-bit offsets implicitly associated with the program's code or data segment and so can be used only within parts of a program small enough to fit in one segment. Far pointers are 32-bit segment:offset pairs resolving to 20-bit external addresses. Some compilers also support
huge pointers, which are like far pointers except that
pointer arithmetic on a huge pointer treats it as a linear 20-bit pointer, while pointer arithmetic on a far pointer
wraps around within its 16-bit offset without touching the segment part of the address. To avoid the need to specify
near and
far on numerous pointers, data structures, and functions, compilers also support "memory models" which specify default pointer sizes. The
tiny (max 64K),
small (max 128K),
compact (data > 64K),
medium (code > 64K),
large (code,data > 64K), and
huge (individual arrays > 64K) models cover practical combinations of near, far, and huge pointers for code and data. The
tiny model means that code and data are shared in a single segment, just as in most 8-bit based processors, and can be used to build
.com files for instance. Precompiled libraries often come in several versions compiled for different memory models. According to Morse et al.,. the designers actually contemplated using an 8-bit shift (instead of 4-bit), in order to create a 16 MB physical address space. However, as this would have forced segments to begin on 256-byte boundaries, and 1 MB was considered very large for a microprocessor around 1976, the idea was dismissed. Also, there were not enough pins available on a low cost 40-pin package for the additional four address bus pins. In principle, the address space of the x86 series
could have been extended in later processors by increasing the shift value, as long as applications obtained their segments from the operating system and did not make assumptions about the equivalence of different segment:offset pairs. In practice the use of "huge" pointers and similar mechanisms was widespread and the flat 32-bit addressing made possible with the 32-bit offset registers in the 80386 eventually extended the limited addressing range in a more general way. The instruction stream is fetched from memory as words and is addressed internally by the processor to the byte level as necessary. An instruction stream queuing mechanism allows up to 6 bytes of the instruction stream to be queued while waiting for decoding and execution. The queue acts as a
first-in-first-out (FIFO) buffer, from which the Execution Unit (EU) extracts instruction bytes as required. Whenever there is space for at least two bytes in the queue, the BIU will attempt a word fetch memory cycle. If the queue is empty (following a branch instruction, for example), the first byte into the queue immediately becomes available to the EU.
Porting older software Small programs could ignore the segmentation and just use plain 16-bit addressing. This allows
8-bit software to be quite easily ported to the 8086. The authors of most
DOS implementations took advantage of this by providing an
Application Programming Interface very similar to
CP/M as well as including the simple
.com executable file format, identical to CP/M. This was important when the 8086 and MS-DOS were new, because it allowed many existing CP/M (and other) applications to be quickly made available, greatly easing acceptance of the new platform.
Interrupts Interrupts on the 8086 can be either software or hardware-initiated. Interrupts are long calls that also save the processor status. Interrupt routines typically end with a IRET instruction. All interrupts have a 8-bit interrupt number associated with them. This number is used to look up a segment:offset in a 256 element
interrupt vector table stored at addresses 0-3FFH. When any type of interrupt is encountered, the processor status is pushed, CS and IP are pushed, and the interrupt number is multiplied by four to index a new execution address which is loaded from the vector table. There are three types of software
interrupt instructions: INT n, INTO, and a single-byte INT 3 used for debugging. There are two kinds of hardware interrupts: maskable and non-maskable.
Non-maskable interrupts are higher priority than maskable interrupts. They cannot be disabled by interrupt enable. A low to high transition on the NMI pin essentially causes an INT 2 to execute.
Maskable interrupts are enabled and disabled by the STI and CLI instructions respectively. When the INTR is asserted by a hardware device, the 8086 asserts INTA twice, reading an 8-bit interrupt number from the bus. This number is multiplied by four to point to the associated interrupt service routine address in the vector table. Maskable interrupts are disabled when INTA is asserted, but are re-enabled upon executing the IRET instruction at the end of the interrupt service routine. == Example code ==