Micro-Processors and Assembly Programming
Micro-Processors and Assembly Programming
Programming
References
Microprocessor
A microprocessor or processor (sometimes abbreviated µP) is the heart of the
computer and it performs all the computational tasks, calculations and data
processing etc. inside the computer. Microprocessor is the brain of the computer. A
Central processing unit (CPU) in a computer system or handheld device consists
of one or more microprocessors. The microprocessors can be classified based on
the following features:
1
Instruction Set
It is the set of the instructions that the Microprocessor can execute.
Bandwidth
The number of bits processed by the processor in a single instruction.
4 bit. e.g. Intel 4004
8 bit. e.g. Intel 8085, 8088, Zilog Z80, Z180
16 bit. e.g. Intel 8086, 80186, 80286, 80386,
32 bit. e.g. Intel Pentium, Celeron, AMD Sempron
64 bit. e.g. AMD Athlon.
Clock Speed
Clock speed is measured in the MHz and it determines that how many instructions
a processor can processed.
The speed of the microprocessor is measured in the MHz or GHz. The
processor is also known as the CPU (Central Processing Unit). It contains the
control unit and the arithmetic unit and both works together to process the
commands. CPU is used in every computer whether it is a workstation, server or a
laptop. CPU is a complete computational engine that is designed as a chip. It
starts the work when you turn on your computer.
CPU is designed to perform the arithmetic and logical operations inside the
computer. Common operations inside the computer include adding, subtracting,
multiplying, comparing the values and fetching the different numbers to process
them. The higher the CPU clocks’ speed the more efficient will be the
performance of the computer.
Computer’s performance is also influenced by the system bus architecture,
memory used, type of the processor and software program being running.
2
ALU – Performs all arithmetic and logical operations
Register array – Holds the data temporarily for processing
Control Unit – It supervises/ monitors all the operations carried out in the
computer.
Microcomputer Architecture
A computer system has three main components: a Central Processing Unit
(CPU) or processor, a Memory Unit and Input Output Units (devices). In any
microcomputer system, the component which actually processes data is entirely
contained on a single chip called Microprocessor (MPU). This MPU can be
programmed using assembly language. Writing a program in assembly language
requires a knowledge of the computer hardware (or Architecture) and the details of
its instruction set.
The main internal hardware features of a computer are the processor, memory
and registers (registers are special processor components for holding address and
data).
The external hardware features are the computer Input/Output devices such as
keyboard, monitor…
Software consists of the operating system (O.S) and various programs and data
files stored on disk.
Inside any computer based on a member of the 8086 family, the basic arrangement
of the main components is shown in Figure 2.
3
Figure 2: Data flow between the main components of an 8086
family computer.
Information is sent from one main component to another along the communication
channel, which is often called the System Bus. Both programs and data are stored
in the memory. The Bus Interface Unit (BIU) within the MPU fetches new
instruction or data as necessary. It is also the BIU jobs to interpret or decode
instruction and to route results to their proper destination.
The MPU Execution Unit carries out any arithmetic which is required, including
memory calculation. Microcomputer memories consist of a collection of chips of
two kinds Read Only Memory (ROM) and Random Access Memories (RAM).
System Bus
The components of the computer system must communicate with each other and
with the outside world. Although it may be possible to connect each component to
the CPU separately as a practical matter this would require too many physical
connects. To keep the number of connections manageable, the processor is
connected to memory and all peripherals using a bus. A Bus is a bunch of wires,
and electrical path on the printed IC to which everything in the system is
connected.
4
There are three types of Bus:
1- Address Buss (AB): the width of AB determines the amount of physical
memory addressable by the processor.
2- Data Bus (DB): the width of DB indicates the size of the data transferred
between the processor and memory or I/O device.
The slots and connectors provide access to such components as ROM, RAM, hard
disk, CD-ROM drive, additional memory, video unit, keyboard, mouse, parallel
and serial device, sound adapter and cache memory (the processor use high speed
cache memory to decrease its need to access the slower main memory). A bus with
wires attached to the system board connect the components. It transfers data
between the processor, memory and external devices.
A. The processor
The CPU or processor acts as the controller of all actions or services provided by
the system. The operations of a CPU can be reduced to three basic steps: fetch,
decode, and execute. Each step includes intermediate steps, some of which are:
5
3- Execute the instruction.
- Perform the required calculation.
- Store results in memory or register.
- Set status flag attached to the CPU.
Figure 3 shows a block diagram of a simple imaginary CPU. The CPU is divided
into two general parts. Arithmetic Logic Unit (ALU) and Control Unit (CU).
B. Memory
6
The memory of a computer system consist of tiny electronic switches, with each
switch set in one of two states: open or close. It is however more convenient to
think of these states as 0 and 1.
Thus each switch can represent a binary digit or bit, as it is known, the memory
unit consists of millions of such bits, bits are organized into groups of eight bits
called byte.
8086 can address up to 1 MB (220 bytes) of main memory this magic number
comes from the fact that the address bud of the 8086 has 20 address lines. This
number is referred to as the Memory Address Space (MAS). The memory address
space of a system is determined by the address bus width of the CPU used in the
system. The actual memory in a system is always less than or equal to the MAS.
7
Two basic memory operations
The memory unit supports two fundamental operations: Read and Write. The read
operation read a previously stored data and the write operation stores a value in
memory. See Figure 5
8
start at 0 and increase for successive groups. The term location refers to a group of
bits with a unique address. Table 1 represents Bit, Byte, and Larger units.
Types of memory
The memory unit can be implemented using a variety of memory chips- different
speeds, different manufacturing technology, and different sizes. The two basic
types are RAM and ROM.
9
2- Read/Write Memory
Dynamic RAM: main memory, or RAM is where program, data are kept when a
program is running. It must be refreshed with in less than a millisecond or losses
its contents.
Static RAM, used for special high speed memory called cache memory which
greatly improves system performance. Static RAM keeps its value without having
to be refreshed.
C. INPUT/OUTPUT
Input/output (I/O) devices provide the means by which the computer system can
interact with the outside world. Computers use I/O devices (also called peripheral
devices) for two major purposes:
Devices that are used to communicate like, printer, keyboard, modem, Devices that
are used to store data like disk drive. I/O devices are connected to the system bus
through I/O controller (interface) – which acts as interface between the system
bus and I/O devices.
1- I/O devices exhibit different characteristics and if these devices are connected
directly, the CPU would have to understand and respond appropriately to each I/O
device. This would cause the CPU to spend a lot of time interacting with I/O
devices and spend less time executing user programs.
2- The amount of electrical power used to send signals on the system bus is very
low. This means that the cable connecting the I/O device has to be very short (a
10
few centimeters at most). I/O controllers typically contain driver hardware to send
current over long cable that connects I/O devices. See Figure 6.
8088 and 8086 functionally identical but 8088 lower performance, 80186 run all
8088 and 8086 software, but have 10 new instructions. 80188 in function are
identical to 80186 but lower performances. 80286 run all 8086, 80186 program,
but has extra instruction, more powerful than 8086. 80386 has various operation
mode, which allow it to act as 80286 chip or multiple 8086 chip, as well as a set of
instruction capable of 32 bit operations such as arithmetic.
11
Table 2: Different Microprocessor features descriptions
An Execution Unit (EU) and Bus Interface Unit (BIU). The role of the EU is to
execute instruction, whereas the BIU delivers instruction and data to EU.
The EU contains ALU, CU and number of registers. This feature enables the EU
to execute instructions and perform arithmetic and logical operations. The most
important function of BIU is to manage the bus control unit, segment registers
instruction queue. The BIU controls the busses that transfer data to the EU, to
memory, and to external input/output devices, whereas the segment registers
control the memory addressing.
12
Figure 7: Execution unit and Bus interface unit.
13
Another function of the BIU is to provide access to instructions, because the
instructions for a program that is executing are kept in memory, the BIU must
access instruction from memory and place them in an instruction queue, which
varies in size depending on the processor. This feature enables the BIU to look
ahead and prefetch instructions, so that there is always a queue of instructions
ready to execute.
The EU and BIU work in parallel, with the BIU keeping one step ahead. The EU
notifies the BIU when it needs access to data in memory or I/O devices. Also the
EU request machine code instructions from the BIU instruction queue. The top
instruction is the currently executable one, and while the EU is occupied executing
an instruction, the BIU fetch another instruction from memory. This fetching
overlaps with execution and speeds up processing.
14
The processor expects numeric data in memory to be in reverse byte sequence and
processes the data accordingly, again reverses the bytes, restoring them to correctly
in the register as hexa 0529H.
When programming in assembly language, you have to distinguish between the
address of a memory location and its contents. In the above example the content of
address 04A26H is 29, and the content of address 04A27H is 05.
15
To reference any memory location in a segment, the processor combine the
segment address in a segment register with the offset value of that location, that is,
its distance in byte from the start of the segment.
Specifying addresses
To represent a segment address and its relative offset we use the notation:
Segment: offset
Address Bus in the 8086 is 20 bits wide (20 lines) i.e. the processor can access
memory of size 220 or 1048576 bytes (1MB).
16
Instruction Pointer = 16 bit register which means the processor can only address 0
– 216 (65535) bytes of memory. But we need to write instructions in any of the
1MB of memory. This can be solved by using memory segmentation., where each
segment register is 16-bit (this 16-bit is the high 16-bit of Address Bus (A 4- A19))
i.e. each of the segment registers represent the actual address after shifting the
address 4-bit to get 20 bits.
Registers
Registers are 8, 16, or 32-bit high speed storage locations directly inside the CPU,
designed to be accessed at much higher speed than conventional memory.
17
Figure 8: Intel 16-bit registers
The CPU has an internal data bus that is generally twice as wide as its external data
bus.
Data Registers: The general purpose registers, are used for arithmetic and data
movement. Each register can be addressed as either 16-bit or 8 bit value. Example,
AX register is a 16-bit register, its upper 8-bit is called AH, and its lower 8-bit is
called AL. Bit 0 in AL corresponds to bit 0 in AX and bit 0 in AH corresponds to
bit 8 in AX. See Figure 9.
18
Figure 9: AX register
Instructions can address either 16-bit data register as AX, BX, CX, and DX or 8-bit
register as AL, AH, BL, BH, CL, CH, Dl, and DH. If we move 126FH to AX then
AL would immediately 6FH and AH = 12H.
* Segment Registers: the CPU contain four segment registers, used as base
location for program instruction, and for the stack.
19
1- CS (Code Segment): The code segment register holds the base
location of all executable instructions (code) in a program.
* Index registers: index registers contain the offset of data and instructions. The
term offset refers to the distance of a variable, label, or instruction from its base
segment. The index registers are:
2- SP (Stack Pointer): the stack pointer register contain the offset of the top
of the stack. The stack pointer and the stack segment register combine to
form the complete address of the top of the stack.
3- SI (Source Index): This register takes its name from the string
movement instruction, in which the source string is pointed to by the
source index register.
20
1- IP (Instruction Pointer): The instruction pointer register always contain the
offset of the next instruction to be executed within the current code segment.
The instruction pointer and the code segment register combine to form the
complete address of the next instruction.
2- The Flag Register: is a special register with individual bit positions assigned to
show the status of the CPU or the result of arithmetic operations. The Figure 10
describes the 8086/8088 flags register:
There two basic types of flags: (control flags and status flags)
1- Control Flags: individual bits can be set in the flag register by the
programmer to control the CPU operation, these are - The Direction Flag
(DF): affects block data transfer instructions, such as MOVS, CMPS, and
SCAS. The flag values are 0 = up and 1 = down.
- The Interrupt flag (IF): dictates whether or not a system interrupt can
occur. Such as keyboard, disk drive, and the system clock timer. A program
will sometimes briefly disable the interrupt when performing a critical
operation that cannot be interrupted. The flag values are 1 = enable, 0 =
disable.
- The Trap flag (TF): Determine whether or not the CPU is halted after each
instruction. When this is set, a debugging program can let a programmer to
enter single stepping (trace) through a program one instruction at a time.
21
The flag values are 1 = on, 0 = off. The flag can be set by INT 3
instruction.
2- Status Flags: The status flags reflect the outcomes of arithmetic and logical
operations performed by the CPU, these are:
- The Carry Flag (CF): is set when the result of an unsigned arithmetic
operation is too large to fit into the destination for example, if the sum of 71
and 99 where stored in the 8-bit register AL, the result cause the carry flag to
be 1. The flag values = 1 = carry, 0 = no carry.
- The Overflow (OF): is set when the result of a signed arithmetic operation is
too wide (too many bits) to fit into destination. 1 = overflow, 0 = no
overflow.
- Sign Flag (SF): is set when the result of arithmetic of logical operation
generates a negative result, 1= negative, 0 = positive.
- Zero Flag (ZF): is set when the result of an arithmetic of logical operation
generates a result of zero, the flag is used primarily by jump or loop
instructions to allow branching to a new location in a program based on the
comparison of two values. The flag value = 1 = zero, & 0 = not zero.
- Auxiliary Flag: is set when an operation causes a carry from bit 3 to bit 4 (or
borrow from bit 4 to bit 3) of an operand. The flag value = 1 = carry, 0 = no
carry.
- Parity Flag: reflect the number of 1 bit in the result of an operation. If there is
an even number of bit, the parity is even. If there is an odd number of bits,
parity is odd. This flag is used by the OS to verify memory integrity and by
communication software to verify the correct transmission of data.
22
Every instruction consists of at least one operation, such as MOV, ADD.
Depending on the operation, an instruction may also have one or more operands
that reference the data the operation is to process.
1. Fetch the next instruction to be executed from memory and place it in the
instruction queue.
3. Execute the instruction, performs the request operation, store the result in a
register or memory, and set flags such as zero or carry where required.
For an .EXE program the CS register provides the address of the beginning of a
program code segment, and DS provide the address of the beginning of the data
segment.
The CS contains instructions that are to be executed, where as the DS contain data
that the instruction reference. The IP register indicates the offset address of the
current instruction in the CS that is to be executed. An instruction operand
indicates an offset address in the DS to be referenced.
CS: IP together determine the address of the first instruction to execute 05BE0H +
0000H = 05BE0H. In this way the first instruction in CS being execution, if the first
instruction is two byte long, the processor increment IP by 2, so that , the next
instruction to be executed is 05BE0H + 0002H = 05BE2H.
Assume the program continues executing, and IP contain the offset 0023H. CS: IP
now determine the address of the next instruction to execute, as follows:
23
EX: let's say that MOV instruction beginning at 0FC03H copies the content of a
byte in memory into the AL register. The byte is at offset 0016H in the DS. Here are
the machine code and the symbolic code for this operation.
The second and third byte contains the offset value in reversed byte sequence. In
symbolic code, the operand [0016] in square brackets (an index operator) indicates
an offset value to distinguish it from the actual storage address 16.
Let say that the program has initialized the DS register with DS address 05D1[0]H.
To access the data item, the processor determines its location from the segment
address in DS + the offset (0016H) in the instruction. Operand become DS contain
0FD1[0]H, the actual location of the reference data item is
24
Assume the address 05D26H contain 4AH, the processor now extract the 4AH at
address 05D26H and copy it into AL register.
The processor stores the two byte in memory in revered byte sequence as
Another instruction, MOV AX, [0016], subsequently could retrieve this byte by
copy them from memory back into AX. The operation reverses (and corrects) the
byte in AX as:
Number of Operands
Operands specify the value an instruction is to operate on, and where the result is
to be stored. Instruction sets are classified by the number of operands used. An
instruction may have no, one, two, or three operands.
25
1. Three-Operand instruction:
In instruction that have three operands, one of the operand specifies the destination
as an address where the result is to be saved. The other two operands specify the
source either as addresses of memory location or constants.
EX: A=B+C
ADD destination, source1, source2
ADD A,B,C
ADD T1, X, D
ADD T2, N, 1
Mul Y, T1, T2
MOV T1, X
ADD T1, D
MOV Y, N
ADD Y, 1
MUL Y, T1
26
ADD source
EX: A=B+C
LDA B
ADD C
STA A
EX: Y=(X+D)* (N+1)
LDA X
ADD D
STA T1
LDA N
ADD 1
MUL T1
STA Y
Computers that use Zero operand instruction for arithmetic operations also use one
operand PUSH and POP instruction to copy value between memory and the stack.
PUSH source; Push the value of the memory operand onto the
Top of the stack.
POP destination; POP value from the Top of the stack and copy it
into the memory operand.
27
Assembly Language
Instruction
Assembly language
instructions are provided to
describe each of the basic
operations that can be
performed by a
microprocessor. They are
written using alphanumeric
symbols instead of the 0s and
1s of the microprocessor's machine code. Program written in assembly language
are called source code. An assembly language description of this instruction is
ADD AX, BX
In tins example, the contents of BX and AX are added together and their sum is put
in AX. Therefore, BX is considered to be the source operand and AX the
destination operand.
This instruction statement starts with the word LOOP. It is an address identifier for
the instruction MOV AX, BX. This type of identifier is called a label or tag. The
instruction is followed by "COPY BX INTO AX." This part of the statement is
called a comment. Thus a general format for writing and assembly language
statement is:
28
Various addressing modes:
MOV AX, BX
This stands for move the contents of BX, the source operand, to AX, the
destination operand. Both the source and destination operands have been specified
as the content of the internal registers of the 8086. See Figure 11(a, b).
29
Figure 11(a): Register addressing mode before execution.
30
Figure 11(b): Register addressing mode after execution.
31
Figure 12(a): Immediate addressing mode before execution.
32
Figure 12(b): Immediate addressing mode after execution.
33
This stands for move the contents of the memory location which is offset by BETA
from the current value in DS into internal register CX. See Figure 13(a, b). Notice
that the value assigned to constant BETA is 1234H.
PA = 02000H + 1234H
= 03234H
This instruction moves the contents of the memory location offset by the value of
EA in SI from the current value in DS to the AX register. See Figure 14(a, b). SI
contains 1234H and DS contains 0200H.
PA = 02000H + 1234H
= 03234H
34
Figure 13(a): Direct Addressing mode before execution.
35
Figure 13(b): Direct Addressing mode after execution.
36
Figure 14(b): Register Indirect Addressing mode after execution.
MOV [BX].BETA, AL
As shown in Figure 15(a,b) the fetch and execution of this instruction causes the
BIU to calculate the physical address of the destination operand from the contents
of DS, BX, and the direct displacement. The result is
37
6. Indexed Addressing Mode
Indexed addressing works identically to the based addressing, it uses the contents
of one of the index registers, instead of BX or BP, in the generation of the physical
address, here is an example:
The example in Figure 16(a,b) shows the result of executing the MOV instruction.
First the physical address for the source operand is calculated from DS, SI, and the
direct displacement.
Then the byte of data stored at this location, which is BEH is read into lower byte
AL of the accumulator register.
38
Figure 15(a): Based Addressing before execution.
39
Figure 16(a): Direct Indexed Addressing before execution.
40
Figure 16(b): Direct Indexed Addressing mode after execution.
7. Based Indexed Addressing Mode
Combining the based addressing mode and the indexed addressing mode together
results in a new, more powerful mode known as based indexed addressing. Let us
consider an example of a MOV instruction using this type of addressing.
41
The string instructions of the 8086's instruction set automatically use the source
and destination index registers to specify the effective addresses of the source and
destination operands, respectively. The move string instruction
MOVS
is an example. Notice that neither SI nor DI appears in the string instruction, but
both are used during its execution.
42
Figure 17(b): Based Indexed Addressing mode after execution.
9. Port Addressing Mode
Port addressing is used in conjunction with the IN and OUT instructions to access
input and output ports. Any of the memory addressing modes can be used for the
port address for memory mapped ports. For ports in the I/O address space, only the
Direct addressing mode and an Indirect addressing mode using DX are
available. For example, Direct addressing of an input port is used in the
instruction
IN AL, 15H
This stands for input the data from the byte wide input port at address 15 H of the
I/O address space to register AL. Next, let us consider another example. Using
Indirect port addressing for the source operand in an IN instruction, we get:
IN AL, DX
It means input the data from the byte wide input port whose address is specified by
the contents of register DX. For instance, if DX equals 1234 H the contents of the
port at this I/O address are loaded into AL.
43
Problems:
6. Which register or registers are used as an offset address for string instruction
destination in the microprocessor?
7. The stack memory is addressed by a combination of the-------- segment plus -----
------ offset.
8. Which registers of the 8086 are used in memory segmentation?
9. Categorize each flag bit of the 8086 as either a control flag or as a flag to
monitor the effect of instruction execution.
10. Identify the three part of an assembly language instruction in each of the
following statement:
11. Identify the source and destination operand for each of the statements in 10.
Instructions set
8086 has 117 instructions, these instructions divided into 6 groups:
44
The microprocessor has a group of data transfer instructions that are provided to
move data either between its internal registers or between an internal register and a
storage location in memory. Some of these instructions are:
EXAMPLES:
♣ XCHG: in MOV instruction the original contents of the source location are
preserved and the original contents of the destination are destroyed. But XCHG
instruction can be used to swap data between two general purpose register or
between a general purpose register and storage location in memory.
EXAMPLES:
45
2. XCHG SUM, BX
(DS (0) + SUM) BX
DS=02000 H + 1234 H =03234H
(3234) (BL)
(3235) (BH)
2. Arithmetic Instructions
EXAMPLE:
- ADC AX, BX
AX=AX+BX+CF
- INC AH
AH= AH +1
EXAMPLE:
The original contents of AX, BL, memory location SUM, and CF are AX=1234 H,
BL= ABH, Sum=00CDH and CF=0 respectively, describe the result of execution the
following sequence of instruction:
46
ADD AX, SUM
ADC BL, 05H
INC SUM
- SBB AX, BX
AX= AX - BX – CF
EXAMPLE:
00H – BX
0000 + 2's complement of BX
EXAMPLE:
47
♣ Multiplication and Division MUL, DIV
- MUL CL
(AX)= AL* CL
- MUL CX
(DX, AX) = AX * CX
- DIV CL
(AH), (AL) = AX/CL
And AL the quotient
Where AH is the reminder
- DIV CX
DX, AX= (DX,AX)/CX
AX contain the Quotient
DX contain the reminder
EXAMPLE:
48
4. Shift Instructions
The four types of shift instructions can perform two basic types of shift operations.
They are the logical shift and arithmetic shift. Each of these operations can be
performed to the right or to the left.
49
5. Rotate Instructions
50
♣ RCR (Rotate Carry Right)
EXAMPLE:
51
Write an instruction to save the current content of the flags in memory location
MEM1 and then reload the flags with the contents of memory location MEM2
Solution:
LAHF
MOV MEM1, AH
MOV AH, MEM2
SAHF
2. Compare Instruction
There is an instruction included instruction set which can be used to compare two
8-bit number or 16-bit numbers. It is the compare (CMP) instruction.
The operands can reside in a storage location in memory, a register within the
MPU. Instruction Meaning Format Operation Flag affected the process of
comparison performed by the CMP instruction is basically a subtraction operation.
The source operand is subtracted from the destination operand. However the result
of this subtraction is not saved. Instead, based on the result the appropriate flags
are set or reset.
EXAMPLE:
Lets the destination operand equals 100110012and that the source operand equals
000110112. Subtraction the source from the destination, we get
10011001
00011011
52
Replacing the destination operand with its 2's complement and adding
10011001
11100101
011111102
1. No carry is generated from bit 3 to bit 4, therefore, the auxiliary carry flag AF is
at logic 0.
2. There is a carry out from bit 7. Thus carry flag CF is set.
3. Even through a carry out of bit 7 is generated; there is no carry from bit 6 to bit
7. This is an overflow condition and the OF flag is set.
4. There are an even number of 1s, therefore, this makes parity flag PF equal to 1.
5. Bit 7 is zero and therefore sign flag SF is at logic 0.
6. The result that is produced is nonzero, which makes zero flag ZF logic 0.
3. JUMP Instruction
a. Unconditional jump.
b. Conditional jump.
53
Figure 17: Unconditional jump program sequence.
On the other hand, for a conditional jump instruction, status conditions that exist at
the moment the jump instruction is executed decide whether or not the jump will
occur. If this condition or conditions are met, the jump takes place, otherwise
execution continues with the next sequential instruction of the program. The
conditions that can be referenced by a conditional jump instruction are status flags
such as carry (CF), parity (PF), and overflow (OF). See Figure 18.
54
The following table lists some of the conditional jump instructions:
55
Figure 18: Conditional jump program sequence.
EXAMPLE:
It is necessary to save the contents of certain registers or some other main program
parameters. These values are saved by pushing them onto the stack. Typically,
these data correspond to registers and memory locations that are used by the
subroutine.
The instruction that is used to save parameters on the stack is the push (PUSH)
instruction and that used to retrieve them back is the pop (POP) instruction. Notice
a general-purpose register, a segment register (excluding CS), or a storage location
in memory as their operand.
56
((SP)-1) (AH)
((SP)-2) (AL)
(SP) (SP)-2
This shows that the two bytes of the AX are saved in the stack part of memory and
the stack pointer is decrement by 2 such that it points to the new top of the stack.
POP AX
(AL) ((SP))
(AH) ((SP) + 1)
(SP) (SP)+2
♣ We also can save the contents of the flag register and if saved we will later have
to restore them. These operations can be accomplished with the push flags
(PUSHF) and pop flags (POPF) instructions, respectively. Notice the PUSHF
save the contents of the flag register on the top of the stack. On the other hand,
POPF returns the flags from the top of the stack to the flag register.
5. String Instructions
57
The microprocessor is equipped with special instructions to handle string
operations. By "string" we mean a series of data words or bytes that reside in
consecutive memory locations.
There are five basic string instructions in the instruction set of the 8086, these
instruction are:
a. Move byte or work string (MOVS, MOVSB, and MOVSW).
b. Compare string (CMPS).
c. Scan string (SCAS).
d. Load string (LODS)
e. Store string (STOS).
They are called the basic string instructions because each defines and operations
for one element of a string.
Move String
The instructions MOVES, MOVSB, and MOVSW all perform the same basic
operation. An element of the string specified by the source index (SI) register with
respect to the current data segment (DS) register is moved to the location specified
by the destination index (DI) register with respect to the current extra segment (ES)
register.
After the move is complete, the contents of both SI and DI are automatically
incremented or decremented by 1 for a byte move and by 2 for a word move.
Remember the fact that the address pointers in SI and DI increment or decrement
depends on how the direction flag DF is set.
CMPS BYTE
The source element is pointed to by the address in SI with respect to the current
value in DS and the destination element is specified by the contents of DI relative
58
to the contents of ES. Both SI and DI are updated such that they point to the next
elements in their respective string.
The scan string (SCAS) instruction is similar to CMPS, however, it compares the
byte or word element of the destination string at the physical address derived from
DI and ES to the contents of AL or AX, respectively. The flags are adjusted based
on this result and DI incremented or decremented.
Interrupts (INTs)
Interrupt is a mechanism by which a program's flow of control can be altered, INT
provide a mechanism to transfer control to an interrupt service routine (ISR).
The section of program to which control is passed is called the interrupt service
routine (ISR). When the microprocessor terminates execution in the main program,
it remembers the location where it left off and then picks up execution with the
first instruction in the service routine. After this routine has rum to completion,
program control is returned to the point where the microprocessor originally left
the main body of the program.
The interrupts of the microprocessors include two hardware pins that request
interrupts (INTR and NMI), and one hardware pin (INTA) that acknowledges the
interrupt requested through INTR. In addition to the pins, the microprocessor also
has software interrupts INT, INTO, INT3 and BOUND. Two flag bits IF and TF
are also used with the interrupt structure and a special return instruction IRET.
Interrupt Processing
59
The Interrupt Vector Table (IVT) is located at address 0, each vector takes 4 bytes.
Each vector consist of a (CS:IP) pointer to the associated ISR, 2 byte for specifying
the CS, and 2 byte for the offset (IP) within the CS. As shown in the below Figure
(19).
The IVT layout in the memory since each entry in the IVT is 4 byte long, INT type
is multiplied by 4 to get the corresponding ISR pointer in the table. For example ,
INT 2 can find the ISR pointer at memory address 2*4 =00008H, the first 2 byte at
the specified address are taken as the offset value, and the next 2 byte as the CS
value. Thus executing INT 2 causes the CPU to suspend its current program and
calculate the address in the IVT (which is 2*4=8) and read CS:IP value and
transfer control to that memory location.
Just like procedure ISR, should end with a (RET) inst to send control back to the
INT program. The interrupt return (IRET) is used for this purpose. On receiving an
INT, flag register is automatically saved on the stack. The INT enable flag is clear.
This disable attending further INT until this flag is set. Usually, this flag is set in
ISR unless there is a special reason to disable other INT.
60
Figure 19
61
The current CS and IP values are pushed onto the stack. In most cases, these value
CS and IP point to the instruction following the current instruction the CS and IP
register are loaded with the address of ISR from the IVI.
Interrupt Type
The 8086 microcomputer is capable of implementing any combination of up to 256
interrupts. They are divided into five groups: external hardware interrupts, software
interrupts, internal interrupts, the nonmaskable interrupt, and the reset interrupt.
The function of the external hardware, software, and nonmaskable interrupt and the
rest interrupts can be defined by the user. On the other hand, the internal and reset
interrupts have dedicated system functions.
INT INT-Type
Where INT-Type is an integer number in the range 0-255, thus a total of 256
different types are possible.
62
Hardware Interrupt (HW INT)
This type is usually use by peripheral I/O devices such as KB to alter CPU that
they require its attention.
A NMI can be generated by applying an electronic signal on the NMI pin this INT
is called NMI because the CPU always respond to this signal. In other word, this
INT cannot be disabling under program control, the NMI cussed by INT2.
Most HW INT are maskable type, and electronic signal should be applied to the
INTR (interrupt request) input pin of 8086, 8086 recognize the INTR only if IF=1,
thus this INT can be masked or disable by clear IF(IF=0).
INT 0 Divide error INT the CPU generate an IT type whenever executing a DIV
result in a quotient that is larger than the destination. The default ISR displays a
divide overflow message and terminate a program.
Single Step INT, single step is a useful debugging tool to observe of the behavior
of a program instruction by instruction. To start single stepping the TF bit in flag
63
register should be set (TF=1), when TF=1, the CPU automatically generate a type 1
INT after execution each inst.
To end single step, TF should be cleared, the CPU however does not have any
instruction to manipulate the TF directly
PUSHF
POP AX ;COPY FLAGS INTO AX
OR AX, 100H ;SET TF=1
PUSH AX ;COPY MODIFY FLAG BIT BACK TO FLAG REGISTER
POPF
PUSHF
POP AX ; COPY FLAGS INTO AX
AND AX, 0FEFFH, ;CLEAR TF=0
PUSH AX ; COPY MODIFY FLAG BIT BAXK TO FLAG REGISTER
POPF
Computer typically have more than one I/O device requesting interrupt service,
like keyboard, hard disk, floppy disk, printer all generate an INT when they
required the attention to CPU.
When more than one device INT CPU, we need a mechanism to priority these INT
(if they come at the same time) and forward only one INT request at a time to the
CPU while keeping other INT request pending for their service.
64
An I/O device can be a purely input device (e.g. KB, Mouse), a purely output
device (printer, screen), or both input and output device like (e.g. disk).
Regardless of the intended purpose of I/O devices, all communication with these
devices must involve the system bus. However, I/O devices are not directly
connected to the system bus. Instead, there is usually, On I/O controller that acts as
an interface between the system and the I/O devices.
It is a waste of time and effort if everyone had to develop their own routines to
access I/O devices. In addition system resource could be abused either
intentionally or accidentally. For instance, and improper disk drive could erase the
content of a disk due to a bug in the driver routine.
To avoid this problem and to provide a standard way of accessing I/O devices, OS
provide routine to convent all access I/O devices. Typically, access to I/O devices
can be obtain from two layer of system software, the basic I/O system (BIOS) and
the OS,BIOS is ROM resident and is a collection of routine that control the I/O
devices. Both provide access to routine that control I/O devices through a
mechanism called INT (interrupt).
I/O addresses are 16 bit in length and are output by the 8086 to the I/O interface
over bus lines AD0 through AD15, the most significant bit A16-A19 of the
memory address are held at the 0 logic (not used).
Below Figure 20 show a map of I/O address space of the 8086 system. This is an
independent 64-KB address space that is dedicated for I/O devices. Notice that its
address range is from 000016-FFFF16. Moreover, notice that the eight ports
65
located from address 00F8 to 00FF are specified as reserved. These port addresses
are reserved by Intel for use in their future HW and SW products.
Data transfer between the MPU and I/O devices are performed over the data bus.
Word transfer take place over the complete data bus D0 to D15, and can required
either one or two bus cycle.
Ports: a port is a device that connects the processor to the external world through a
port processor, receive a signal from an input device and send a signal to an output
device.
• ACC = AL or AX
66
Example 1: write a sequence of inst that will output FF16 to a byte wide output
port at address AB16 of the I/O addresses space.
Solution: first the AL register is loaded with FF16 as an immediate operand in the
instruction
MOV AL, 0FFH
Now the data in AL can be output to the byte wide output port with the instruction
OUT 0ABH, AL
Example2: write a series of instruction that will output FF16 to an output port
located at address B00016 of the I/O address space.
Solution: the DX register must first be loaded with the address of the output port
OUT DX, AL
Example 3: data are to be read in from two byte wide input port at address AA16
and A916 respectively, and then output to a word wide output port at address
B00016. Write a sequence of instruction to perform this I/O operation:
Solution: we first read in a byte from the port at address AA16 into AL and move
it to AH
IN AL, 0AA16
MOV AH, AL
IN AL, 0A9H
67
To writhe out the word of data in AX, we can load DX with the addressB00016 and
use a variable output instruction
Isolated I/O: it is the most common I/O transfer techniques. The addressed for
insolated I/O device, called ports, are separate from the memory. Because the ports
are separate from the memory, because the ports are separate. The user can expand
the memory to its full size without using any of memory space for I/O device. A
disadvantage of isolated I/O is that, the data transferred between I/O and the MPU
must be accessed by the IN, OUT instruction. See Figure 21.
Memory- Map I/O: Unlike isolated I/O, memory mapped I/O does not use the IN
or OUT instruction. Instead, it uses any instruction that transfer data between the
MPU and memory. A memory mapped I/O device is treated as a memory location
in memory map. The main advantage of memory-mapped I/O is that any memory
transfer instruction can be used to access the I/O. The main disadvantage is that a
portion of the memory systems used as the I/O map. This reduced the amount of
memory available to application. See Figure 22.
68
Figure 22: Memory-Mapped I/O
69
EENG410: MICROPROCESSORS I
8086
▪ Virtual Memory: a way of fooling the microprocessor into thinking that it has
access to unlimited memory by swapping data between disk storage and RAM.
▪ Real mode (faster operation with maximum of 1 Mbytes of memory) vs.
Protected mode protecting the operating system for accidental or deliberate
destruction by the user.
▪ Protected mode is slower but can use 16 megabytes of memory.
EENG410: MICROPROCESSORS I
8086
History of 80x86 Family of Microprocessors
• Other microprocessors: 80286, 80386, and 80486
▪ 80386: Introduced in 1985 also known as (80386DX)
❖ With 32-bit internal and external data bus.
❖ 32-bit address bus (232 = 4 gigabyte-physical memory). With virtual
memory 64 terabyte(246).
❖ 80386SX was later introduced with the same internal structure with 16-bit
external data bus and 24-bit address bus. 80386SX was much cheaper.
▪ All microprocessors discussed so far were general-purpose microprocessors
and could not handle mathematical operations rapidly. For this reason, 8087,
80287, 80387 numeric data processing chips called math co-processors were
used.
▪ 80486: introduced in 1989 with 32-bit internal-external data bus and 32-bit
address bus.
❖ built in math co-processor in a single chip.
❖ Introduction of cache memory ( Static RAM with very fast access time)
EENG410: MICROPROCESSORS I
8086
History of 80x86 Family of Microprocessors
• Evolution of Intel's Microprocessors
• These two sections work simultaneously. BIU accesses memory and peripherals
while the EU executes the instructions previously fetched.
• It only works if BIU keeps ahead of EU. Thus BIU has a buffer of queue. (8088 has 4
byte, and 8088 has 6 bytes).
• If the execution of any instruction takes to long, the BIU is filled to its maximum
capacity and busses will stay idle. It starts to fetch again whenever there is 2-byte
room in the queue.
• When there is a jump instruction, the microprocessor must flush out the queue.
When a jump instruction is executed BIU starts to fetch information from the new
location in the memory. In this situation EU must wait until the BIU starts to fetch
the new instruction. This is known as branch penalty.
EENG410: MICROPROCESSORS I
8086
Registers of 8086 Microprocessor
• Registers of 8086
❖ In the CPU, registers are used store information temporarily. The information
can be one or two bytes of data, or the address of data.
❖ In 8088/8086 general-purpose registers can be accessed as either16-bit or 8-
bit registers. All other registers can be accessed as full 16-bit registers.
MSB LSB
16-bit register:
MSB LSB
EENG410: MICROPROCESSORS I
8086
Registers of 8086 Microprocessor
▪ Different registers are used for different functions. Registers will be explained
later within the context of instructions and their applications.
▪ The first letter of each general register indicates its use.
❖ AX is used for the accumulator.
❖ BX is used for base addressing register.
❖ CX is used for counter loop operations.
❖ DX is used to point out data in I/O operations.
▪ Registers of 8086
• Low/High-level languages
❖ Assembly Language is a low-level language. Deals directly with the internal structure
of CPU. Assembler translates Assembly language program into machine code.
❖ In high-level languages, Pascal, Basic, C; the programmer does not have to be
concerned with internal details of the CPU. Compilers translate the program into
machine code.
EENG410: MICROPROCESSORS I
8086
Introduction to Assembly Language Programming
• Machine Language
❖ Programs consist of 0s and 1s are called machine language.
❖ Assembly Language program consists of series of lines of Assembly
language instructions.
❖ Instructions consist of a mnemonic and operand(s).
• MOV instruction
mnemonic operands
EENG410: MICROPROCESSORS I
8086
Introduction to Assembly Language Programming
• MOV instruction
Comment sign
Example: (8-bit )
MOV CL,55H ;move 55H into register CL
MOV DL,CL ;move/copy the contents of CL into DL (now DL=CL=55H)
MOV BH,DL ;move/copy the contents of DL into BH (now DL=BH=55H)
MOV AH,BH ;move/copy the contents of BH into AH (now AH=BH=55H)
Example: (16-bit)
MOV CX,468FH;move 468FH into CX (now CH =46 , CL=8F)
MOV AX,CX ;move/copy the contents of CX into AX (now AX=CX=468FH)
MOV BX,AX ;now BX=AX=468FH
MOV DX,BX ;now DX=BX=468FH
MOV DI,AX ;now DI=AX=468FH
MOV SI,DI ;now SI=DI=468FH
MOV DS,SI ;now DS=SI=468FH
MOV BP,DS ;now BP=DS=468FH
EENG410: MICROPROCESSORS I
8086
Introduction to Assembly Language Programming
• MOV instruction
▪ Rules regarding MOV instruction
❖ Data can be moved among all registers except the flag register. There are
other ways to load the flag registers. To be studied later.
❖ Source and destination registers have to match in size.
❖ Data can be moved among all registers (except flag reg.) but data can be
moved directly into nonsegment registers only. You can’t move data
segment registers directly.
Examples:
MOV BX,14AFH ;move 14AFH into BX (legal)
MOV SI,2345H ;move 2345H into SI (legal)
MOV DI,2233H ;move 2233H into DI (legal)
MOV CS,2A3FH ;move 2A3FH into CS (illegal)
MOV DS,CS ;move the content of CS into DS (legal)
MOV FR,BX ;move the content of BX into FR (illegal)
MOV DS,14AFH ;move 14AFH into DS (illegal)
EENG410: MICROPROCESSORS I
8086
Introduction to Assembly Language Programming
• MOV instruction
▪ Important points
❖ Data values cannot be loaded directly into (CS,DS,SS and ES)
MOV AX,1234H ; load 1234H into AX
MOV SS,AX ; load the value in AX into SS