Lyla B Das
Lyla B Das
Lyla B Das
OBJECTIVES
We will start our learning of the x86 family of microprocessors, by understanding the
architecture of the 8086 ,on which is based the architecture of the whole family.
The 8086 processor is a 16 bit processor internally as well as externally –which means
that its data bus width as well as the bit size of its internal data registers is 16 .Thus it
can access an external source 16 bits at a time through its data bus. . But it also has the
capability to access and work on 8 bit (byte) data.
As the first step in understanding the architecture of the 8086 ,let us examine the internal
block diagram of the 8086 . Fig 1-1a shows the simplified block diagram and Fig 1-1b
show the detailed one . Go through the simplified one first .Though not specifically
shown in the block diagram ,it is to be noted that all actions in the processor are
synchronized by a system clock ,which provides the basic timing .There is a control unit
which provides the control signals for the overall functioning of the processor.
We see that the internal block diagram has been partitioned into two logical units
,namely the Bus Interface Unit (BIU) and the Execution Unit .
They interact directly with each other through the internal bus ,but perform separately
as two units with well defined functions for each unit. Let us explore the internal
configuration of these units, one by one.
It contains the arithmetic and logic unit ,the control unit ,an internal bus ,plus a few
registers. Let us get a bird’s eye view of the EU starting with the register set.
Why Registers?
All processors have internal registers ,which is actually on-chip RAM.
Some are program visible ,while others are not directly visible to programmers.
The more the number of program visible registers ,the better it is for the programmer
.This is because internal registers are used as temporary storage for operands ,and if the
operand is already in a register ,it takes less time for execution of the associated
instruction. Such registers are called general purpose or scratch pad registers .
AX
This is a 16 bit data register ,but can also be used as two unrelated 8 bit registers AH and
AL . AL/AX is sometimes called the accumulator ,but the relevance of the accumulator
is less for 8086 compared to the earlier 8085 in which one operand is implied to be in the
A register for many instructions.However , for the 8086 ,only for certain specialized
instructions is the AL or AX register implied to contain one of the operands .In most
other instructions ,the source and destination must be specifically mentioned in the
instruction. (The destination is the register or memory location in which the result of any
operation is to be put )
BX,CX and DX
These are the other working registers of the 8086 ,which means that temporary data
storage ,arithmetic calculations ,data manipulation etc can be done with these registers
.They also can be used as 8 bit or 16 bit registers as mentioned earlier . Still, each of them
has special functions .
Base register BX is frequently used as an address register in many based addressing
modes. Counting register CX is used as a counter in many instructions .
Data register DX is used in I/O instructions as a pointer to data by storing the address of
the I/O port . .Also, multiply and divide operations involving large numbers assume DX
and AX as a pair.
It is a 16 bit register ,of which 7 bits are unused .Out of the remaining bits , 6 bits are
used as conditional flags .The others are control .flags.
U=unused
CONDITIONAL FLAGS CONTROL FLAGS
OF= Overflow flag DF= Direction Flag
SF= Sign Flag IF= Interrupt Flag
ZF= Zero Flag TF= Trap Flag
AF= Auxiliary carry Flag
PF= Parity Flag
CF= Carry Flag
A conditional flag is a single bit flip flop which is set or reset according to the result of
an arithmetic or logic operation .
The conditional flags available are the Carry (CF), Zero (ZF) ,parity (PF) ,overflow
(OF)and the sign flag(SF). Since conditional flags are an important ingredient in
computation using assembly language ,we will not defer our discussion on them.
Zero Flag(ZF)
When the result of an arithmetic or logic operation is zero ,the Zero flag gets set .For
example ,if we keep on decrementing the contents of a register (say ,the CX register
which is used as a count), it will finally become zero . At this instant ,the zero flag gets
set i.e. ZF=1. Another case is when two numbers are compared .Comparison is achieved
by subtraction .If the numbers compared are equal ,the zero flag is set (ZF=1) indicating
equality of the operands.
Parity Flag (PF)
The setting of this flag indicates the presence of an even number of bits in the least
significant byte (lower 8 bits) of the destination.
For example ,after a particular arithmetic or logic operation ,if the destination contains
the number 11100111 ,the parity flag is set to indicate even parity. Even if the operation
is on 16 bit data, only the parity of the lower 8 bits is verified. This flag does not find
much applications in current programming practices.
Sign Flag(SF)
After an arithmetic or logic operation ,if the result contains a negative number ,the sign
flag is set .In essence ,it contains the MSB of the result ,which can be interpreted as the
sign bit in signed arithmetic operations.
Overflow Flag(OF)
This flag is set under one of the following conditions
i) there is an overflow into the MSB (8th or 16th bit) from the bit of lower
significance, but no carry out from the MSB
ii) there is a carry out from the MSB ,but no carry into the MSB.
This flag indicates the that the result of a signed number operation is too large , causing
the higher order bit to overflow into the sign bit ,thus changing the sign bit .
---------------------------------------------------------------------------------------------------------
Example 1-1
Find the status of the flags CF ,SF, AF after the following instructions are executed
MOV AL,35H
ADD AL,0CEH
Solution:
The format of a MOV instruction is MOV destination ,source .
In the above two instructions ,a number 35H is first moved to AL .In the next line ,0CEH
is added to AL .The sum will be in AL ,the destination register .
35 H 0011 0101 +
+ CE H 1100 1110
103 H 1 0000 0011
Solution:
This is the case of 16 bit addition
45EC H 0100 0101 1110 1100
+ 7723 H 0111 0111 0010 0011
BD0F H 1011 1101 0000 1111
Assuming we are adding signed 8 bit numbers , how is the result of the following
addition to be interpreted?
MOV AL,125
ADD AL,75
Solution:
Both operands here are decimal numbers
This program causes AL to be loaded with +125 and then +75 to be added to AL .The
result is to be in AL.
Signed numbers consider the MSB as the sign bit .With 8 bits ,the maximum range of
signed numbers -128 to +127 .
When adding the given signed numbers,
+125 0111 1101
+75 0100 1011
+200 1100 1000
There has been an overflow from D6 to D7 (i.e to the MSB). In this case we find OF=1
.The sum is interpreted as a negative number with value of the sign bit =1 .
The setting of the OF flag indicates that the magnitude of the result has caused an
inversion of the sign bit ,indicating a mistake .This interpretation is done ,because the
maximum range of signed positive numbers is only +127 for 8 bit representation.
Incidentally the Sign Flag is also set (SF=1)
---------------------------------------------------------------------------------------------------------
Control Flags:
Besides the conditional flags ,there are three control flags namely the Trap flag ,
Direction flag and Interrupt flag .The control flags have to be deliberately set or reset
according to the requirements of the program,.
The Trap flag (TF) is set to perform step by step execution ,during debugging .
The Interrupt Flag (IF) is set to enable interrupts.
The Direction flag (DF) is used in string operations .
Details of the functioning of these flags will be discussed in later chapters.
It is the part of a computer that performs all arithmetic and logic computations. The ALU
is the most important unit of the processor .Instructions which are fetched and decoded,
are executed in the ALU. Thus the ALU has direct access to the general purpose
registers and flags.
Instructions are found in memory ,from where they are fetched and decoded as and when
they need to be executed .But in 8086,there is a queue which fetches instructions ahead
of the execution time and places them in a six byte first in first out (FIFO) queue. This
pre-fetching is done when the buses are free i.e. not being used for the execution of the
current instruction .
The advantage of pre-fetching is that when a particular instruction is to be executed, there
is a good chance of finding it in the queue(which is on-chip), rather than having to go to
memory to fetch it . But the queue will have to be emptied in the event of a branch
instruction ,i.e. when the next instruction to be executed is not the next one in the
sequence. After this ,the queue needs to be re-loaded from the new ‘branch’ .address.
This pre-fetching belongs to a class of ideas called pipelining ,which means that both
execution and fetching take place at the same time. i.e, while the execution of one
instruction is going on ,the fetching of another one can be done.
Pipelining greatly speeds up processing.
The 8086 has a 20 bit address bus , but let us remember that we have not seen any 20 bit
registers so far . .It is understandable that the general purpose registers are used as 16 bit
or 8 bit registers ,as this is the way data is organized. But we see that all the address
registers also are 16 bit . An ingenuous idea called segmentation has been used to take
care of this situation. A segment is just an area in memory . Thus the total memory size
can be considered to be divided into segments of various sizes . The idea of dividing
memory this way is called segmentation
In memory ,data is stored as bytes. Each byte has a specific address.
With 20 address lines ,the memory that can be addressed is 220 bytes. This will amount
to 1,048,576 bytes (1 Megabyte). Thus the 8086 can access a physical memory with
addresses ranging from 00000H to FFFFFH .A 20 bit physical address is to be placed
on the address bus to access any location in memory.
Memory as a whole is considered to have four different types of segments
labeled as data, code ,stack and extra. One idea of segmentation is to keep data and code
separately . Thus the code segment contains code only . Data is stored in the data segment
,and the extra segment is just another type of data segment used in a special way.
The stack segment is an area of memory which functions and is accessed differently
from the other three segments
.
1.3.2.1 Segment Registers
Each of these segments is addressed by an address stored in corresponding segment
registers .These registers are all 16 bit in size .Each register stores the base address of
the corresponding segment .A base address is the starting address of a segment .But
since the segment registers cannot store 20 bits (of the base address) ,they store only the
upper 16 bits. The least significant nibble is implied to be zero.
How is a 20 bit physical address obtained if there are only 16 bit registers?
The 20 bit address of a byte is called its physical address .But it is specified as a logical
address which is in the form of two 16 bit numbers in the format base address: offset
See the following figure ,as a typical example .
BASE ADDRESS
SS
A0000H
ES
90000H
SEGMENT REGISTERS
A000H SS
70000H DS 9000H ES
7000H DS
5000 H CS
CS
50000H
Similar is the case for the other segments . All offsets are limited to 16 bits which
means that the maximum size possible for a segment is only 216 which is 65,536 bytes
or 64K bytes. We can conclude that every memory reference by the 8086 will use one of
the segment registers (i.e., DS, ES, SS, or CS, ) which is combined with an offset
(usually given in the instruction) to determine the physical address being accessed.
A data byte has the logical address of the form DS: offset
The stack is an area of memory which is used in a special way . Data is generally pushed
in and popped out of the stack . The stack of the 8086 is a Last in First out ( LIFO )stack
,which means that the last data that was pushed in ,is the first one that can be popped out
.This also means that data can only be taken out or put in only from the top of the stack,
But the s‘ stack top’ changes as data is put in or taken out.
There is a 16 bit register called Stack Pointer which points to the top f the stack .
The stack segment is like any other segment ,and the upper 16 bits of its base address is
available is the SS register. A stack address of the form 4466H: 0122H means that the SS
register contains 4466 H, the stack pointer (SP)contains the number 0122H , and the
physical address of the top of the stack is 44660H+0122H =44782H
Address of the
top of stack 44782H Byte 0122 H SP
Byte
Byte
-
-
-
-
-
-
Base Address 44660H - 4466H SS
The stack is used mostly to keep aside address and data temporarily , when a subprogram
is called . This will be taken back when the subprogram ends .The 8086 has a stack which
grows downwards(i.e. to lower memory addresses ) . This means that each push
instruction causes the SP to be decremented (by two for 8086) , and data is pushed deeper
downwards into the stack segment .We will discuss the operation of the stack in more
detail in Chapter 4.
Even though ,a stack is usually accessed only from the top ,the 8086 has another register
named BP (base pointer) which is used to reference data anywhere within the stack .
Thus the format SS:BP is also a logical address. This has certain interesting
applications.
Table 1-1: Segments and corresponding registers used for specifying offsets
Table 1.1 shows the segment registers and the corresponding registers for offsets
------------------------------------------------------------------------------------------------------------
Example 1-4
The content of DS is 345BH . The amount of data that is to be stored in the data segment
is 12K bytes . Where in memory ,will this segment be located?
Solution:
DS contains the number 345BH.
This number corresponds to the upper 4 hex digits of the starting (base address)of the
data segment .
The base address of the segment is 345B0H .
The last address will have an offset of 12K from the base address .
1K =1024 bytes
12 K=12 x1024 =12228=3000H
The last address of the segment =345B0H +3000H=375B0H
Hence the data segment occupies the memory addresses from 345B0H to 375B0H.
---------------------------------------------------------------------------------------------------------
Example 1.5
The contents of the following segment registers are as given
CS =1111H ,DS =3333H ,SS=2526H
IP =1232H ,SP =1100H ,offset in data segment =0020H
Calculate the corresponding physical addresses for the addressed byte in a)CS b)SS and
c)DS.
Solution:
a)The base address of CS is 1110H .
The address of the next instruction to be executed is referenced by CS and IP .
which is given by 11110H + 1232H =12342H
b)The current top of the stack is referenced by SS and SP .The base address of the stack
segment is 25260H .The corresponding physical address is 25260H +1100H =26350H
c)The data that needs to be accessed is given by DS and the offset .The base address of
the data segment is 33330H .The physical address of this data is calculated as
33330H+0020H =33350H
------------------------------------------------------------------------------------------------------------
Segmentation-General Concerns
2 Segments can overlap with a new segment starting every 16 bytes. This also
means that there can be more than one logical address for the same memory
location. (physical address) For example, 0000:0100, 0001:00F0, and 0010:0000
all refer to physical address 00100H.
3 We can have segments with the same base address ,but the offsets within the
segment will have to be taken care of to prevent overwriting of data .
For example ,in string operations ,we might use the same segment as the
data and extra segments. This is achieved by loading the same number in the ES
and DS registers.
4. A segment can have a maximum size of 64 K .
5 . A program need not have all four segments ,but it will have at least the code
segment ,
6.. The addresses used in instructions are called logical addresses which are
translated by the BIU to physical addresses.
Advantages of Segmentation
1. It allows all address registers to have the same size as the data registers(16 bit) ,
while allowing the use of 20 bit physical addresses.
2. All addresses in memory are re-locatable . This means that any program or data
can be loaded in any address in memory .A re-locatable program is one which
can be placed in any area of memory and executed without change. Data is also
re-locatable. Changing the base address of the corresponding segment is the only
action we need to perform, in order to re-locate .. All addresses within the
program are relative to the base address, as they are of the form Base address:
offset .All processors in the x86 family have this kind of segmentation . The
higher order processors have protection also added to this structure.
Memory Organization
We have seen that data is stored in memory as bytes . Each byte location has an address .
If a word (two bytes) is to be stored ,two locations of consecutive addresses are needed .
Consider that the number 34E6H is to stored . Then 34H is stored in one location and
E6H is in the next location .Intel’s processors use what is termed the little endian format
which means the lower byte is stored in the lower address and the upper byte in the
higher address .Thus if the above word has an address of 60088H , the storage is as
shown in the fig
ADDRESS DATA
60088H E6H
60089H 34H
Fig: Little endian format
This is in contrast to certain other processors (like Motorola) which uses the big endian
format.
(The origin of the word ‘endian’ is supposed to be from Gulliver’s travels ,referring to a
controversy about whether an egg should be broken at the little end or big end)
Since the data in this case is a word ,two memory locations with two addresses have to be
used to access it .The hardware connection between the processor and memory is
designed in such a way that both these locations are accessed and a word (16 bits) access
is obtained, even though only one logical address is mentioned in the instruction.
2) Immediate Addressing
In this mode ,the source will be a constant data
MOV AL, 45H ;copy 45H to AL
MOV BX, 34E3H ;move the hex number 34E3H to BX
MOV CL.’Q’ ;move the ASCII value of Q to CL
MOV PRICE,40 ;move the hex number 40 to the memory
;location with label PRICE
MOV NUMS, 0FC6H ;move the hex number 0FC6H to the memory
; location NUMS
Segment registers are not allowed to use this mode of addressing.
MOV DS,2300H ;gives an error as DS is a segment register
3) Direct Addressing
Here either the source or the destination will be a memory address
MOV AX,[2345H] ;move the word in location 2345H into AX
MOV [1089H],AL ;the byte in AL is moved to location 1086H
It is to be remembered that the addresses in the instructions are offsets within the data
segment (i.e the logical address) . We need to know the content of DS to calculate the
physical address . The size of the registers indicate the size of the operand . Hence ,in the
first instruction ,a data word is referred to ,while in the second ,a data byte is moved.
The square brackets are necessary to indicate that the number is an address and not data .
However , we may use labels for addresses ,and re-write the above two instructions
MOV AX , PRICE
MOV COST,AL
We must then ensure that the referred items have been defined earlier with these labels.
We will see this aspect later
Show the location of data in memory, after the execution of each of these instructions , if
the content of registers are as given
DS= 1112H , AX= EE78H and BX=3400H
i) MOV [0422H], AL
ii) MOV [0424H],AX
iii) MOV [BX] ,AX
Solution:
i)This is a case of direct addressing . The destination is a memory location which is
specified directly in the instruction. AL contains 78H (lower byte of AX)
DS =1112H
Data segment base address is 11120H
The offset in the instruction is 0422H
Physical address is 11120H +
0422H
11542H
After the instruction execution ,the content of the addressed location is as shown
PHYSICAL CONTENT
ADDRESS
11542H 78H
However since AX contains two bytes of data , the lower byte (AL) is stored in the
address 11544H and the higher byte (AH) in the next address (recollect the ‘little endian’
concept)
PHYSICAL CONTENT
ADDRESS
11544H 78H
11545H EEH
PHYSICAL CONTENT
ADDRESS
14520H 78H
14521H EEH
Solution:
i) MOV CL,1234H[SI]
This is a case of register relative addressing .The effective address is obtained from
the instruction, to be the sum of the displacement and SI
Effective address =1234H +
4442H
5676H
The segment base address is obtained from DS to be 40220H
The physical address is the sum of the segment base address and the effective
address. i.e. 40220H +
5676H
45896H
This is a case of relative based indexed mode .The effective address is calculated as
the sum of the displacement and the contents of the registers SI and BP .
Effective address = 0005 H +
4442 H
1402 H
5849 H
The physical address is the sum of the segment base address and the effective
address . In this case ,as BP is one of the address registers ,the segment referred is
the stack segment
Physical address is 23440 H (base address of the stack segment)
+ 5849 H
28C89 H
------------------------------------------------------------------------------------------------------------
Table 1-2 : Effective address and referred segments for various memory based
addressing modes
Note that BP cannot be used without a displacement. See it being used in ‘register
relative’ mode but not in register indirect mode.
Segment Override
Table 1-2 shows the default segments to be used in the various memory based addressing
modes. However this can be overridden by a segment override prefix .i.e.. We can use
segments other than those specified in the table .The format of this override prefix is
shown below.
MOV AL , ES: [BX] overrides the fact that an instruction of this sort implies that the
data segment is to be used . Here it says that ES is to be used instead .Hence physical
address calculations use the content of the CS register as the base address.
Other examples are
MOV DS: [BP+7], BL ;DS to be used instead of SS
MOV AX, CS:[BX ] ;CS to be used instead of DS
• The architecture of 8086 is representative of the architecture of all the members of the
x86 family .
• The data bus width of 8086 is 16 ,and internal registers also have the same bit size.
• The internal block diagram shows two units named the ‘execution unit’ and the ‘bus
interface unit’ with well defined and separate functions.
• The BIU and EU communicate through the internal bus .Both have a common timing
and control unit.
• The execution unit takes care of computations ,and thus it contains the necessary
registers and flags.
• The conditional flags are set or reset according to the result of certain arithmetic/logic
operations
• The 8086 has 20 address lines which means it can access 1 MByte of memory.
• Memory is segmented into four types of segments .
• Code and data are kept in separate segments.
• The upper 16 bits of the base address of any segment is available in the
corresponding segment registers.
• The stack segment is a type of segment which grows downwards as data is pushed
into it.
• The BIU converts the logical addresses to physical addresses.
• Segmentation makes data and code to be re-locatable.
• The addressing mode dictates where operands are available.
• 8086 follows the ‘little endian’ scheme of data storage in memory.
QUIZ
EXERCISE
1. Find the content of the destination and the status of the carry flag (CF) and zero
flag (ZF) after the execution of the following set of instructions
a) MOV AL,09H
ADD AL,CFH
b) MOV BX,0876H
MOV AX,0034H
ADD BX,AX
c)MOV AL,0FFH
ADD AL,01
3. Show the binary number that will be available in AL after the execution of each
of the following instructions?
a)MOV AL ,’C’ b) MOV AL,25 c)MOV AL,34H
4. Find the physical address of the top of the stack if SS =0777H ,and SP =1234H .
5. If IP contains the number 0034H ,and CS= 5555H ,where will the next instruction
be fetched from?
6. If CS contains the number 0FC2H ,and the volume of code is 2400 bytes ,which
is the last address in this code segment?
7. Write the addressing mode of the following instructions .
a) MOV [4560H],AX
b) ADD BL,89H
c) ADD BX,[DI]
d) MOV 6[BP][DI],AL
e) MOV CX,[BP+9]
f) ADD CL,BL
8 Find the physical address of the memory locations referred in the following
instructions if DS=0223H , DI =0CCCH ,SI =1234H .
a) MOV [DI], AL
b) MOV [SI][56H] ,BL
9.Which segments will be accessed in the following cases?
g) MOV [6][BX],CX
h) MOV [BP]+4,CL
i) PUSH AX
j) MOV CS:[BX] ,DL
k) MOV AL,ES:[45H]
10 The memory addresses starting from the logical address 0124H: 0056H store
the numbers 0978H ,CE45H ,45H and 7809H in consecutive locations
Draw a diagram showing the physical address and the corresponding data stored .
CHAPTER 2
PROGRAMMING CONCEPTS –I
OBJECTIVES
In chapter 0 ,we have seen why assembly language is very important .To code efficiently
in assembly language for a particular processor ,the prerequisites are a good knowledge
of the internal architecture of the processor and addressing modes ,which means that
understanding the contents of chapter 1 is absolutely necessary .
When we want to write and execute an ALP ,we first type it using an editor .Then we
assemble link and run it .There are many assembly language program development tools
available for each of these actions .We shall examine them one by one .
Let us first get an idea of the assembly process. An interesting definition for an
assembler is that it is a translator that translates source instructions( in symbolic
language) into target instructions (in machine language), on a one to one basis. The point
is that each source instruction is translated to exactly one target instruction. This is indeed
the main task of the translator. But an assembler is more than just a translator . It is an
important program development tool ,the quality of which can affect the ease of
programming . Since assembly language programming is more prone to errors (than
HLLs) ,the assembler should be good enough to give appropriate error messages to the
programmer and guide every step of his programming .
When code is written in assembly language ,it is easy if we can use labels (names) for
memory locations. All constants and variables are given names so that they can be
referenced by name . Managing and utilizing these labels (called symbols) efficiently
reflects on the quality of an assembler .
Assemblers that support macros are called Macro Assemblers .A Macro is a name given
to a sequence of instruction lines . Once a macro is defined , its name may be used in
place of this set of lines .In the assembly process, each macro is replaced by the text lines
.Thus a macro seems to the programmer to be like a mnemonic ,and he can use it as
frequently as he wants ,without incurring any of the overheads that a procedure or
function may cause.
The use of macros can help to make programs structured.
In this example, BEGIN is a label, which corresponds to the address at which this line
of code is stored . MOV is a mnemonic. This instruction means that the content of COST
is to be moved to register AX . COST is a label (for a memory location) which acts as
one operand in this two operand instruction .A label when defined is called a symbol . A
symbol can only be defined once but it can be used many times.
There will be a memory address corresponding to all symbols. In this instruction, the
other operand is AX . The text coming after ‘;’ is a comment .
All current assemblers allow the use of labels , rather than numeric addresses of memory
locations . But finally, the assembler must translate these labels to memory addresses .As
such, an important issue that an assembler will have to sort out is what is called the
‘forward reference problem’ .
See the code below
BEGIN : MOV AX , BX
JMP AGAIN
---------------
----------------
-
-
-
AGAIN: ----------
As the assembler reads the second line of this program ,an undefined label AGAIN is
encountered .The assembler has no knowledge of it as yet ,but the value of the symbol
AGAIN is needed before it can be defined .This is also called the future symbol problem
or the problem of unresolved references .
This problem is solved in various ways depending of the type of the assembler .
There are many types of assemblers-one pass , two pass and multi pass assemblers .Each
pass optimizes the assembly process.
Assembly language is specific to a particular processor .As such ,we should concern
ourselves with assemblers for the x86 family of processors ,which includes the whole set
from 8086 to Pentium . A large set of assemblers are available ,but the code running on
one assembler will not run on another .Another matter to be taken care of is the OS under
which it can run.
A86 was the first assembler to appear on the scene .It could run only under DOS .Now
there are assemblers which run under DOS ,Windows and Linux .
The names of a few popular assemblers are NASM ,FASM ,MASM ,TASM and HLA.
FASM and NASM can run under all these three OSes.
Two of the very popular assemblers are TASM and MASM. TASM is Borland’s Turbo
Assembler ,while MASM is Microsoft’s Macro Assembler . It is found that code written
on TASM runs on MASM ,though the reverse is need not be true
Both of them run under DOS and Windows, which means they can use 16 bit and 32 bit
coding .Both of them are classified as High level Assemblers which mean that we can use
high level control structures (IF, WHILE, REPEAT….UNTIL etc.) which makes
programming easy .
HLA is another assembler which uses high level control structures ,high level data types
and High level procedure calls .
For programs in this book ,we will choose MASM because it is one of the best
assemblers and also because of its popularity . A popular assembler gives us the chance
to get easy access to references and material . Also MASM has many constructs and
features which makes ALP easy and fun for beginners
Another reason is that the documentation available for it is very good . MASM was a
commercial product for a very long time ,and is a sort of industry standard. Microsoft has
written considerable documentation for this assembler and many third parties have
written assembly language reference manuals for MASM. The versions of MASM 6.0
and above have a lot more features (aimed at simplification in writing code )than
previous versions .
Incidentally ,MASM is a proprietary product of Microsoft. We will now use the 16 bit
version which runs under DOS ,because we can use DOS and BIOS interrupts in this
version . . There is a 32 bit version which runs under Windows This is freely available on
the net (The 32 bit version does not allow the use of DOS and BIOS interrupts) . In later
chapters, we will use MASM32 for the 32 bit x86 processors This approach gives a very
good understanding of assembly language in general . The DOS version used for
programs in this book is MASM 6.14.These programs will run in all versions of MASM
from 6.0 upwards.
iii) Link the object file . The linking involves combining separately assembled
modules into one executable file ( filename.exe)
iv) Run the exe file .This will cause the program to be loaded in memory and
executed .
v) The results of execution will be in memory or in registers .If we want to view
it on our display unit ,we will have to use special codes for it (this will be
discussed later). Otherwise ,we can examine the memory and register contents
using the debugger . The debugger has all the commands needed to do this ,
and the full set of commands is detailed in Appendix A.
In the BIN directory , assemble and link the file use the command ml tinym.asm .
Example 2.2 shows the DOS command window corresponding to this .The ml command
causes assembling and linking to be done at one go . .
Now to get the list file , type the command ml/Fl tinym.asm .The list file will be saved
in the BIN directory as tinym.lst
(More commands and options are available in Appendix A)
Earlier versions of MASM required two steps for the same actions.
To assemble , the command was masm filename.asm
Linking required the command link filename .obj
The list file was obtained by using masm/l filename.asm
Examples 2.2 shows the DOS command window as we do assembling and linking .
Example 2.3 shows the list file .
Note: If you think this part is taxing , rush through these topics fast and come back
to it after you have learned more aspects of programming and are ready to do
programming using this assembler.
________________________________________________________________
Example 2.2
C:\masm6.14\BIN>ml tinym.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997. All rights reserved.
Assembling: tinym.asm
Example 2.3
--------------------------------------------------------------
Example 2.3 shows the listing corresponding to our tinym program.
( Only a part of the list file is shown here. See Appendix A for the additional details.)
A lot of information can be obtained just by looking at the list file .The list file is one of
the outputs obtained in the assembly process .
Examine Example 2.3
We see first the details of the version of MASM that is used.
The complete assembly language program along with the comments that we had typed in
, is seen next.. On the left hand side, we see the offsets within the code segment in which
the code is saved .These offsets have been generated by the assembler .We also find the
opcodes corresponding to each instruction.
For example in the first line which is
0100 B0 67 MOV AL,67H ;move 67H to AL
0100H is the offset in the code segment where the first instruction is stored ,and B0 67 is
the opcode of MOV AL,67H . If we add a directive .listall , the list file is changed as
shown in Example 2.4
------------------------------------------------------------------------------------------------------------
Example 2.4
.MODEL TINY
0000 .CODE
.STARTUP
0100 *@Startup:
.listall
C:\masm6.14\BIN>debug tinym.com
-r
AX=0000 BX=0000 CX=010C DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13AD ES=13AD SS=13BD CS=13BD IP=0100 NV UP EI PL NZ NA PO NC
13BD:0100 B067 MOV AL,67
-u
13BD:0100 B067 MOV AL,67
13BD:0102 B345 MOV BL,45
13BD:0104 02C3 ADD AL,BL
13BD:0106 8AD0 MOV DL,AL
13BD:0108 B44C MOV AH,4C
13BD:010A CD21 INT 21
-------------------------------------------------------------------------------------------------
The r command gives us the content of the registers, before the execution of the
instruction MOV AL,67H .
We find that the content of the CS register is CS=13BD.
On typing the ‘u’ command ,, we find that the logical address of the first instruction is
13BD:0100 . The value of CS need not be the same every time the program is loaded into
memory .This is because of the fact that object file generated by the assembler is
re-locatable.
--------------------------------------------------------------------
Example 2,6 shows a sequence of instructions that copy various data between 16 and 8
bit registers. The act of moving data from one register to another changes only the
destination register ,never the source.
-------------------------------------------------------------------------------------------------------
Example 2.7
Before we go on to using a two segment memory model ,we need to understand a few
directives of the assembler which define and describe different kinds of data.
Data which is used in a program can be bytes or words or of greater length .We have to
define data and assign labels to their corresponding addresses . Assigning labels eases
the programmer’s burden as he does not have to concern himself with numerical values
.The location counter in the assembler keeps on incrementing as labels are
encountered.
Defining data implies allocating space for data . Data is defined accordingly using
directives . Thus DB defines data byte ,while DW defines data word. This is the
traditional way of defining data . The newer versions of MASM (6.0 and higher)allow the
use of the directive BYTE for DB ,and WORD for DW ..
.MODEL TINY
0000 .CODE
0000 50 NUM1 DB 50H ;place 50H into NUM1
0001 210A NUM2 DW 210AH ;place 210AH into NUM2
0003 0789 NUM3 DW 0789H ;place 0789H into NUM3
.STARTUP
-------------------------------------------------------------------
The salient points of this program listing are
i) The word R on code lines with addresses says that the addresses are
re-locatable. This can be observed in the listing of Example 2.8 as well.
iii) In the data segment ,data locations have been given labels . Data will
be stored at offsets as shown in fig below (recollect the little endian
concept.
Fig 2.1 Data segment with labels and offsets corresponding to Example 2.9
Now let us see the same program listing with the .LISTALL directive inserted before
the startup directive .This will show the conversion of .STARTUP and .EXIT to a
number of instructions of 8086 . What is to be noted is that the startup directive
corresponds to instructions necessary to initialize the segment registers (except CS ,which
is done by the OS alone.)Compare this to Example 2-11. But if all this confuses you
,leave it aside for the time being and come back to it when you become more proficient in
assembly language programming,
-----------------------------------------------------------------------------------------------------------
Example 2-9b
0000 *@Startup:
0000 BA ---- R * mov dx, DGROUP
0003 8E DA * mov ds, dx
0005 8C D3 * mov bx, ss
0007 2B DA * sub bx, dx
0009 D1 E3 * shl bx, 001h
000B D1 E3 * shl bx, 001h
000D D1 E3 * shl bx, 001h
000F D1 E3 * shl bx, 001h
0011 FA * cli
0012 8E D2 * mov ss, dx
0014 03 E3 * add sp, bx
0016 FB * sti
---------------------------------------------------------------------------------------------------------
2.3.5 The DUP Directive
This directive is used to replicate a given number of characters. For example ,we may
want to fill up a number of locations in the data segment with the same word or byte .
NUMS DB 10 DUP(0) fills up with 0s the 10 byte locations starting with the label
NUMS.
STARS DB 5 DUP(‘#’) fills up 5 byte locations starting at location STARS,
with the ASCII value of the character # .
BLANK DB 10DUP(?) reserves 10 byte spaces starting from location with the label
BLANK ,but these are not initialized ,which implies that whatever data is there will
remain .We can overwrite these locations with new data when we choose to .
WRDS DW 4 DUP(FF0FH) fills up 4 word locations with the word FF0FH.
This directive allows us to equate names to constants .The assembler just replaces the
names by the values mentioned .Examples are
TEMP EQU 34
PRICE EQU 199
----------------------------------------------------------------------------------------------------------
Example 2.10
.MODEL SMALL
0000 .DATA
0000 45 ONE DB 45H
0001 00 TWO DB ?
0000 .CODE
.STARTUP
Example 2.10 shows the use of the EQU directive . From the list file, we see that the
label FACTR is replaced by 25H when the program is being executed.
.MODEL SMALL
.DATA
.EXIT
END
-----------------------------------------------------------------------------------------------------------
Example 2-12b
.MODEL SMALL
0000 .DATA
ORG 0010H
0010 20 40 NUMS DB 20H,40H
ORG 0030H
0030 8907 00FD NUMS1 DW 8907H,0FDH
0000 .CODE
.STARTUP
ORG 000AH
.EXIT
END
--------------------------------------------------------------------------------------------------------
Example 2-12 b is the list file corresponding to the program in Example 2-12a.
Compare it to any of the list files seen earlier . Now neither the data or code segments
start at the default offset of 0000. The data and code coming after the ORG statements
have offsets as specified in the ORG directive . It has thus left out a lot of space in both
the data and code segments.
2.3.7 Other Models
These are other models available to be used according to our requirements. See the table
below to get an idea of those models and their specifications.
There is another model called the flat model ,which uses 32 bit addressing and
unsegmented memory and is used in the protected mode.
From the above ,it is clear that all the models are not for 8086 alone,but are the features
of MASM which caters to the whole x86 family.
i) The data segment has been given a name DAT . The data within the segment
is enclosed between the SEGMENT and ENDS directives which are similar to
parentheses for a segment .
ii) Similarly ,the code segment is named COD and the contents of this segment
also have been enclosed between the same directives
iii) The DS register has been initialized by the first two instructions. DS cannot
use immediate addressing .Hence DAT, which corresponds to a number has
to be loaded to AX (or any other16 bit register)and then transferred to DS.
iv)
The value of DAT is loaded into DS . This means that DAT corresponds to
the base address of the data segment .But only when the loader loads this
program in memory ,will the actual base address (i.e. the upper 16 bits of the
base address) be loaded into the CS register. At this point ,the assembler just
recognizes the ‘reference’ to a segment named DAT.
iv) The CS register need not be initialized in the program .This is done by the
loader by default.
v) The program is terminated by using the DOS INT 21H with function
number 4CH
(DOS function calls will be discussed in detail later)
vi) ASSUME is a directive which relates each segment to its segment register,
by specifying the name .
However ,the simplified dot models will be used throughout this text book ,except when
special reasons call for the use of the full segment definition. In older versions of MASM
,before the advent of the .startup directive , segment registers had to be initialized with
the instructions as below .
MOV AX ,@DATA
MOV DS, AX
where .DATA is used at the start of the data segment .
Now let us summarize the general format for an assembly language line with an example
.
---------------------------------------------------------------------------------------------------------
Example 2-12
LABEL OPCODE OPERAND COMMENT
-------------------------------------------------------------------------------------------------------------------------------
An assembly language line has four fields namely ,label ,opcode ,operand and comment.
A label is positioned at the left of a line and is the symbol for the memory address which
stores that line of information .There are certain rules regarding labels that are allowed
under MASM.
i) All labels should begin with a letter or the special characters @,$ , _ and ? .
ii)A label can have 1 to 31 characters which may be digits ,letters in upper or lower
case or the special characters at (@),dollar($) , underscore (_) ,dot(.) or question mark(?)
iii)No reserved words of MASM may be used
iv)Each label must be unique .
The second field is the opcode or instruction field . The third is the operand field ,and the
last is the comment field which starts with a semicolon.
The usage of comments is advised for making programs more readable ..
Upto now ,we were discussing assemblers ,the best assemblers available and how to run
our programs using assemblers and other program development tools. Now ,let us go a bit
deeper into the core of the processor and investigate the process of how machine codes
have been designed . In doing so ,we will realize why the assembly process is a ‘one to
one’ process.
2.4.1 Manual Coding
Another idea to think of is the possibility of doing manual or hand coding, as it is called
–i.e. taking an assembly instruction ,looking up or finding out its machine code and
feeding it directly to the processor .Why not ? It looks quite feasible ,doesn’t it ?.Actually
it is , but very soon it becomes obvious that it is too cumbersome to think of
programming as fun.
During the time that the 8085 was a popular processor ,hand coding was the rule rather
than the exception .This was because 8085 had a limited instruction set and a look up
table was sufficient for hand coding . The scenario has changed with the 8086 , which
has more registers and more instructions –but the most notable fact is that it has many
,many more modes of addressing -which implies that each instruction can be expressed
in too many modes to facilitate the use of a look up a table . With the more advanced
of the x86 processors coming to the scene ,the difficulty has multiplied many fold .As
such ,we will conclude that using a standard and good assembler is the best solution .
However ,it will be very instructive and informative to understand how and why a
particular mnemonic has been converted to a particular code .
Let us understand the underlying principles of the Instruction set architecture of 8086 .
2.4.1 Instruction Set Architecture(ISA)
The following statement gives an almost appropriate definition of what is meant by ISA.
An instruction set, or instruction set architecture (ISA), is defined as the part of computer
architecture related to programming, including the native data types, instructions,
registers, addressing modes, memory architecture, interrupt and exception handling, and
external I/O. An ISA includes a specification of the set of opcodes (machine
language),i.e. the native commands implemented by a particular CPU design.
It must be noted that processors with the same ISAs need not have the same internal
designs(microarchitectures) –they only need to share a common instruction set .That is
why we find that AMD64 and P64 have the same ISA but they are not the same internally
. The ISA is an interface between a higher level language and the machine language .
2.4.2 Instruction set design of 8086
Now let us get to the 8086 processor ,which is a CISC processor .One feature of
CISC is that its instruction size varies –i.e. all instructions do not have the same size .
8086 has instruction size varying from one byte to five bytes. This makes the
processes of assembly, disassembly and instruction decoding complicated because the
instruction length needs to be calculated for each instruction. An instruction should have
the complete information for fetching and executing an instruction . It should thus have
the following information.
See the general format of the first three bytes of the instruction.
i)
PREFIXES
(OPTIONAL)
ii)
OPCODE D W
(6 bits) (1 (1
bit) bit)
iii)
MOD REG R/M
(2 bits) (3 bits) (3 bits)
PREFIX:
This is an optional byte and need to be used only to change the operation. e.g. segment
override prefix
FIRST BYTE:
This is considered to be the first byte of an instruction .
In this byte ,there is the operation code(opcode) which is 6 bytes long .This is the code
which defines the operation to be carried out. The other two bits are the D and W bits.
W (1 bit)- operand size .W=1 ,means word operand ;W=0 means byte operand.
D (1 bit) –Direction bit .D=1 means register is destination ;D=0 means register is source.
SECOND BYTE:
MOD (2 bit)- Register bits
REG (3 bit) –the identifying code of the register used .
R/M (3 bits) – Specifying a register or memory operand.
The MOD and R/M bits together specify the addressing mode of the instruction.
Note : All instructions need not have the W and D bits. In such cases, the size of the
operand is implicit ,and the direction is irrelevant .
2.4.3 Designing a code
The design of a code requires more information than has been presented so far.
We need the identifying codes of the registers, obviously .
Each 16 bit and 8 bit register has a unique code .These codes are given in Table 2.3 and
2.4 . In addition , a table showing the MOD and R/M bits corresponding to various
combinations of addressing modes is also given . Using these ,code design will be quite
simple. We also need the Intel manual for the opcodes and formats of instructions .This
is given in Appendix B.
SEGMENT CODE
REGS
ES 00
CS 01
SS 10
DS 11
MOD
R/M 00 01 10 11
W=0 W=1
000 (BX) +(SI) (BX) +(SI) + d8 (BX) +(SI) + d16 AL AX
001 (BX) +(DI) (BX) +(DI) + d8 (BX) +(DI) + d16 CL CX
010 (BP) +(SI) (BP) +(SI) + d8 (BP) +(SI) + d16 DL DX
011 (BP) +(DI) (BP) +(DI) + d8 (BP) +(DI) + d16 BL BX
100 (SI) (SI) + d8 (SI) + d16 AH SP
101 (DI) (DI) + d8 (DI) + d16 CH BP
110 d16 (direct address) (BP) + d8 (BP)+ d16 DH SI
111 (BX) (BX) + d8 (BX) + d16 BH DI
Table 2.5: Codes pertaining to addressing modes –MOD and R/M bit patterns
With the help of the above tables, let us try designing a code for a few simple
instructions .
i)MOV AX,BX
Coming to our task ,first look in the Intel manual for the opcode of MOV ,with the
specification of ‘Register /Memory to /from Register . It is seen to be 100010. This will
correspond to the first 6 bits of the first byte .
W=1,as the operand is a word.
The direction bit D has to be defined . In this case ,both the source and destination are
registers . Let us view it as a word being moved to AX .i. e, register AX is thus the
destination. Hence D=1 .Then we must use the code of AX in the register field of the
second byte . The first byte is
OPCODE D W
1 0 0 0 1 0 1 1
To design the second byte ,first look in Table 2.5 for the MOD and R/M bits. We find
that the MOD bits are 11 (last column of the table) for register addressing , the reg bits
are 011 for AX (from Table 2.3) and the R/M bits are treated as the ‘second register’
field .Here ,the second register is BX ,the code of which is 011 (Table 2.5,4th row). Thus
the second byte is
There is no other information needed in coding this instruction .This is a two byte
instruction with the code 8B C3 H .
OPCODE D W
0 0 0 0 0 0 1 0
iv)MOV DX,[23CDH]
OPCODE D W
1 0 0 0 1 0 1 1
The first byte is 8BH .The second byte has MOD =00 (Column 2 in Table 2.5 ) and
R/M bits are 110 (corresponding to direct addressing in Table 2.5).Thus the second byte
is 16H .This is followed by the direct address in little endian form.
The four byte machine code is 8B 16 CD 23 H
v)MOV [BX],DH
OPCODE D W
1 0 0 0 1 0 0 0
Fig 2.7a Byte 1
For the second byte ,MOD =00 corresponding to [BX]. The register bits are 110 for DH
,and R/M =111 from Table 2.5. Thus the instruction code is 88 37H.
By following the same procedure as discussed for the previous instructions, the code for
this is 8A 00 H.
OPCODE D W
0 0 0 0 0 0 1 1
The third byte of the instruction is 53H ,being the 8 bit displacement given in the
instruction. Thus the code is 03 40 53 H.
Segment override prefix
QUIZ
1. The translation from assembly code to machine code is a one to one
process ,but the translation from a high level language to machine code is
not so .Explain why.
2. List the steps involved in converting source code to executable code ,using
an assembler and other program development tools.
3. What are the roles of the linker and loader in the generation of the run
file?
4. How is a com file different from an exe file?
5. ‘The assembled code is re-locatable’. What is the implication of this
statement?
6. Why is that a .com program has the origin at 0100H?
7. Is MASM a high level assembler (HLA)? Comment.
8. How does a debugger help in the programming process?
9. What exactly does the .EXIT statement do?
10. List the rules regarding labels that can be used in MASM.
EXERCISE
1) Using the tiny model ,write a program that does the following.
a) Moves 1020H to AX,056H to BL and C76H to CX.
b) Copies the content of AX to DS ,the content of BH to AH ,and the content
of CX to DX.
2) Write a program to copy 25 to AL, ‘S’ to BL and ‘*’ to CL . Then move these
to AH,BH and CH respectively.
3) Using the small model, store the bytes 56H and FCH , and words 0978H and
CFD4H in the data segment.
4) Store three bytes and four words in the data segment. Copy one byte and one
Word of these to appropriate registers.
5) Using the small model , write a program which incorporates the following
addressing modes.
a) register mode
b) immediate mode
c) register indirect mode..
6) Write a program which adds a constant correction factor to a temperature that is
stored in the data segment .The corrected temperature should be stored in a new
location in memory.
7) Using the DUP directive allocate space for 10 data words, 5 double words and 2
quadwords in the data segment . Also fill the next 12 spaces with 0, the next 10
spaces with ‘$’ and the next space with ‘S’.
8) Using the full segment definition , write a program to add the content of two
words which are in memory , and store the sum in a third address.
9) Design the machine codes for the following instructions
i. XOR AL,AL
ii. AND AX,BX
iii. OR AL,BL
iv. MOV DS,AX
10) Find the machine codes for the instructions
i. SUB AL,[5676H]
ii. CMP AX, 0CDE2H
iii. CMP 34H[DI][BX],DL
iv ADD CX,[SI]
CHAPTER 3
PROGRAMMING CONCEPTS-II
OBJECTIVES
We are now in a position to feel that we have understood to a small extent ,the tools
needed for programming (in this case assembly programming) .But before getting down
to the actual coding process ,it might be worthwhile to spend some time trying to
understand the various approaches to be used in programming . These ideas apply to all
levels of programming -i.e. whether we use high level languages or assembly level
languages .
One common argument is whether programming is an art or a skill . To say that it is an
art implies that good programmers are born ,not made . This may be partly true ,because
it is seen that some people have a natural flair for programming .But this applies to
almost every ability we might have .Even if programming skill is an inborn ability , one
has to polish it and practice this craft to become good at it .There is no other way but
hard work to become an expert .
Coming to the other side of the argument that programming is a skill -then also ,the same
rules apply . The craft of programming has to learned with sound basics - we should
develop our thought processes to become good problem solvers . For every problem
,there is more than one way of solving it .Finding the optimal solution depends to a great
extent on how much we have learned to build castles in the air -where these castles are
the solutions to our problems .
Let us conclude that just anyone can become a reasonably good programmer , by getting
a good grasp of the basics and following it up with keen studying and continuous
practice.
There are various approaches to solve a programming problem .One is called the top
down approach. This approach tends to look at the problem as one whole functional
block .The solution to the problem is planned , after the whole problem is thoroughly
understood . This functional block is then broken down to smaller blocks ,whose
functions are to be defined along the way .
The other approach is the bottoms up approach . Here the basic functional blocks which
are available or can be clearly defined ,are joined together to get the bigger module .Thus
small function blocks are integrated to form the top level entity .
Both these approaches have their advantages and disadvantages. Depending on the type
of problem at hand , the methodology has to be decided.
In this book ,only the coding part is done -it is left to the student to visualize or write the
algorithm ,as he chooses .
The initial versions of DOS were very simple and resembled another operating system
called CP/M. Subsequent versions have became increasingly sophisticated as they
incorporated features of minicomputer operating systems. However, DOS is still a 16-bit
operating system and does not support multiple users or multitasking .
DOS is considered insufficient for many of the graphical operations that we use
our computers for ,but all Windows have a strong dependency on DOS . So newer and
newer versions of DOS are inbuilt in all Windows OS . DOS is considered to be a
software which can control hardware directly .It is this aspect of DOS which will prompt
us to DOS and BIOS interrupts in this book . A few basic commands of DOS are given
in Appendix A . They will help you to get started on MASM 6.14 .
We will start with three important function calls ,all of which are of interrupt type 21H
i)Read the keyboard with echo
MOV AH ,01
INT 21H
This call exits with the ASCII value of the key pressed ,being available in AL .
The key pressed is also echoed on the screen .
ii)Read Keyboard without echo
MOV AH ,08
INT 21H
This call exits with the ASCII value of the key pressed being available in AL .
The key pressed is not echoed on the screen
The instruction set of the 8086 is reasonably large .Besides, each instruction can be used
in various addressing modes making the whole set larger . Knowing all the instructions
,their format and their features is a key factor in becoming an expert programmer .
Let us list out the general features of instructions which will become clearer as they are
explicitly earned.
i)Most instructions have two operands i.e. the destination and the source .
These can be registers or memory operands .But both operands cannot be
memory operands. Some instructions can also use immediate data as the source
operand.
iii) Certain instructions have only one operand mentioned which is either the
destination or the source . These can be register or memory operands ,but not
immediate data.
iv) There are instructions which have no operands explicitly mentioned in the
instructions .In that case ,some register is implicit in the usage of these
instructions.
To learn the instruction set ,the approach will to be list instructions based on their
functionality , learn their format and function, and then write a few programs using them.
The list below ( Table 3.1)gives all the data transfer instructions of 8086 ,except the
input and output instructions ,which will be introduced in Chapter 5.
Table 3.1: List of 8086 data transfer instructions with format and function
A few of these instructions will be discussed in detail and used in programs here. The rest
will be used in later chapters.
Example 3-1
Example 3.1 shows the use of the MOV instruction in the direct mode of addressing .
This mode uses a memory location as one operand . Here two memory locations labeled
COSTP and SELLP have been defined in the data segment .COSTP has a data byte
stored in it ,but SELLP is only allocated space for later storage .The first MOV
instruction gets the data byte in COSTP into AL. After adding a pre-defined constant
PROFIT to it ,the sum in AL is stored in SELLP .Both MOV instructions use direct
addressing . At this stage , you need to remember that the assembler will do the
conversion of the labels to memory addresses.
Example 3.2
Example 3.2 shows the list file of a program which uses the MOV instruction in the
register relative addressing mode. Let us analyze the program The data segment is first
initialized with 5 bytes .The address of the first location is labeled as ARRAY ..DI is
used to address the element numbers .With DI=0 ,the first element is accessed ,and with
DI=5 ,the sixth element is accessed. Then the effective address (EA) is [ARRAY+DI] .
When DI=0, the instruction MOV AL,ARRAY[DI] ,transfers the data in the first memory
location to AL. To this , 07 is added. With DI=5, ARRAY[DI] becomes the effective
address of the sixth location in memory . Then MOV ARRAY[DI], AL stores the sum
in memory .
.The displacement for the relative mode is the value of the label ARRAY . From the list
file ,it is seen the offset corresponding to ARRAY is 0000..
Since ARRAY is the address of the first location in the data segment ,the data segment
will look like this ,after the program is executed. The sum will be in the location
ARRAY+5 .
EA CONTENT
ARRAY+5 17H
ARRAY+4 50H
ARRAY+3 40H
ARRAY+2 30H
ARRAY+1 20H
ARRAY +0 10H
Example 3.3
.MODEL SMALL ;select small model
0000 .DATA ;start data segment
0000 34 87 56 05 07 ARRAY DB 34H,87H,56H,05H,07H
;setup array
0000 .CODE ;start code segment
.STARTUP ;start program
Example 3.3 is the list file of a program that performs the same function as the previous
example ,but uses the based indexed mode of addressing .i.e. the effective address is the
sum of a base register and an index register . The instruction MOV BX, OFFSET
ARRAY causes the offset of ARRAY (which is 0000 as seen from the list file)to be
loaded into BX .Thus BX now is a pointer to the location labeled as ARRAY .But to
point to each element one by one ,another register DI is also used. With DI=0 ,the
instruction MOV AL, [BX+DI] causes the data in the first location to be copied to AL.
With DI=5 ,[BX+DI] accesses the sixth location in the data segment .
Example 3.4(a)
.MODEL SMALL
0000 .DATA
0000 48 45 4C 4C 4F 24 MESG1 DB "HELLO$"
0006 0A 0D 49 20 41 4D MESG2 DB 0AH,0DH,"I AM SAM$"
20 53 41 4D 24
0000 .CODE
.STARTUP
.EXIT
END
Example 3.4(b)
HELLO
I AM SAM
C:\masm6.14\BIN>
Now let us analyze the program that does this ,the listing of which is shown in Example
3.4(a).
In the data segment ,two messages are stored at offsets with labels MESG1 and MESG2
.The character strings are enclosed in double quotes and terminated by the $ sign .This
sign is mandatory to end a character string which is to be printed on the display device ..
Notice that the listing in Ex 3.4a shows the corresponding ASCII values for each
character of the string. There is an ASCII value for ‘space’ which comes between each
word of the second message-i.e. I AM SAM . It is seen to be ‘20H’ . Note that’24H’is the
ASCII value of $ and it is seen at the end of both strings.
On the second line , we also see two characters 0AH and 0DH .These are the ASCII
values for line feed(which advances the cursor to the next line) and carriage return (sets
cursor to the left position of screen).These are necessary for printing to occur on the
second line, starting from the left.
To print the first message ,DX points to the offset MESG1 This is done by using the LEA
instruction ,which loads the offset of MESG1 in DX.. Then the DOS function with
number 09 is called .Similarly for the second message ,DX should point to the offset of
MESG2.
The two messages as they are printed on the command window is seen in Example
3.4(b). The .EXIT command causes control to return to DOS prompt
0 ‘0’
1 ‘1’
2 ‘2’
3 ‘3’
4 ‘4’
5 ‘5’
6 ‘6’
7 ‘7’
8 ‘8’
9 ‘9’
Table ….: look up table for decimal to ASCII conversion.
See the above look up table which relates a decimal number to its ASCII value .
The ASCII ‘0’,’1’ ,’2’ etc have numerical values 30H,31H ,32H etc .Refer to the
ASCII table in Section 0-....
Example 3.5
The following program gets into the memory location starting from VALUES , the
ASCII values of 1,3,5,4,0….in that order. The look up table is stored in the array named
ASC.
.MODEL SMALL
.DATA
ASC DB '0','1','2','3','4','5','6','7'
NUM DB 1,3,5,4,0
VAL DB ?
.CODE
.STARTUP
LEA BX,ASC ; let BX point to offset of the table
LEA SI,NUM ; let SI point to NUM
LEA DI,VAL ;let DI point to VAL
MOV CX,5 ;CX=5
AGN: MOV AL,[SI] ; move the content of NUM to AL
XLAT ;translate
MOV [DI],AL ; move translated value from AL to VAL
INC SI ; increment the pointer to NUM
INC DI ;increment the pointer to VAL
LOOP AGN ;repeat until CX=0
.EXIT
END
The PUSH and POP instructions are relevant only for the stack segment .
So scroll down and read about the operation of the stack before attempting to
understand the PUSH and POP instructions.
The stack is a region of temporary storage .When a subprogram is called ,the register
contents of the main program at that point of time and the current value of IP and CS are
pushed onto the stack ,and retrieved on returning from the sub program .These things are
done automatically as part of a CALL instruction .Besides this ,a programmer can opt to
store in stack temporarily the values of some registers ,so that he can use those registers
for other computations . He can get back the previous values of these registers ,after he
completes his current computation . These operations are accomplished by the PUSH and
POP instructions.
3.2.3.1 OPERATION OF THE STACK
The 8086 ,as mentioned in Chapter 1 , uses a Last in First out(LIFO)stack . A stack is
an area of memory , in which the lowest address is the base address of the segment .The
upper 16 bits of this base address will be in the SS register .The highest address will be in
the SP register –or rather ,the SP will contain the offset of the highest address with
respect to the base address .This is why it is called the TOP of STACK . For example, if
the stack is defined from addresses 20000H to 203E8H , SS=2000H and
SP=03E8H,.This stack then has a size of 3E8H bytes or 1000 bytes.
In the 8086 ,only a word (16 bits ) can be pushed on to stack . A push thus causes two
memory locations to be accessed .
The operation of the stack is different from normal memory operations. It is a push-down
memory . i.e. For the value of SP mentioned above ,let us say ,the PUSH BX instruction
is to be executed . Let BX=CC99H. .Recollect that SP=03E8H.
The operation of pushing is as follows SP is first decremented by two . The new data
will be put in addresses (offsets ) 03E7H (SP-1) and 03E6H(SP-2), adhering to the
‘ little endian’ concept i.e. the higher byte of BX will be the higher address (03E7H) and
the lower byte in the lower address (03E6H).The new value of SP will be 03E6H . This
shows that new data to be pushed onto the stack will be pushed in the lower two
addresses with respect to the current stack top. Always ,the stack top (SP) has the offset
corresponding to the last byte that was pushed in .
BX
CC 99
CONTENTS ADDRESS OF
OF STACK STACK TOP
-- 03E8H SP BEFORE
THE PUSH
CC 03E7H
99 03E6H SP AFTER THE
PUSH
---- 03E5H
--- 03E4H
The reverse will be the case after a POP operation. Assume a POP CX operation at a
time when the SP value is 0312H . The content of the stack will be loaded into the CX
register. ,and SP is incremented by two.
CONTENTS ADDRESS OF
OF STACK STACK TOP
-- 0314H SP AFTER
THE POP
0E 0313H
67 0312H SP BEFORE
THE POP
---- 0311H
--- 03E10H
CX
0E 67
Example 3.5b
DW 200H (?)
STACKSEG ENDS
Example 3.5 shows two ways of defining a stack. Ex 3.5a uses the simplified memory
model and it specifies the size of the stack as 400 bytes .Ex 3.5 b uses the full segment
definition .200 data words are to be set aside as stack ,as per this definition. Here, both
definitions create stacks of the same size.
Example 3.6
Explain what is done in this program .Assume SP =0310H ,when the stack was initialized
.MODEL SMALL
.STACK 300H
.CODE
.STARTUP
MOV AX,4567H
MOV BX,0ACEH
PUSH AX
PUSH BX
POP AX
POP BX
.EXIT
END
Solution:
This program defines a stack of 300 bytes by the use of the stack directive
The program first loads two numbers in AX and BX. These numbers are pushed into
stack using the PUSH operations . Then comes the POP operations .What we need to
remember is that popping occurs in the reverse direction. What is pushed in last will be
popped out first .The last PUSH was PUSH BX ..It is obvious then that by the POP AX
operation ,the content of AX is replaced by the content of BX .Similarly the second POP
causes BX to get the content of AX. Thus this program exchanges the content of these
two registers , using the stack as a temporary data storage area. After execution the
contents of the registers are: BX=4567H and AX=0ACEH .
Note: Exchanging register contents is not really an application for the stack .We can
exchange these register contents easily by using the instruction XCHG AX,BX
But this program shows the relationship between POP and PUSH operations.
3.3 BRANCH INSTRUCTIONS
Branch instructions are very important ,because they carry on them the full power of a
computer . Branching can be conditional or unconditional .Unconditional branching
becomes necessary when looping back or forward is done infinitely ,like when we use
software to generate a square wave continuously .But most of the time , conditional
branching is what may be more important . Taking decisions based on the result of a
computation is what gives computers their power and versatility . Most instructions that
we will use subsequently become meaningful only when conditional branching is used.
In this chapter ,we will discuss only the jump and loop instructions .Other branching
instructions like call and return will be discussed in Chapter 4.
We will start with the jump instruction.
3.3.1 JMP -Jump
Usage: JMP destination
The destination is a memory location ,which can be expressed in different ways as will
be discussed presently .The jump destination is frequently referred to as ‘target’ as well.
A jump instruction breaks the normal sequence of program execution ,and takes control
to a different location in the code segment .
MOV…..
ADD,-----
JMP AGAIN
………..
--------
-
-
------------
AGAIN: ……..
OPCODE OFFSET
EXAMPLE 3.7
0000 B4 01 BEGIN: MOV AH,01
0002 CD 21 INT 21H
0004 8A D0 MOV DL,AL
0006 EB 07 JMP SHORT THERE
----------------------------------------
--------------,skipped instructions>---
----------------------------------------
000F 80 C2 02 THERE: ADD DL,02
0012 B4 02 MOV AH,02
0014 CD 21 INT 21H
0016 EB E8 JMP SHORT BEGIN
Example 3.7 shows how ‘short’ jump instructions pass control from one part of the
program to another.
0006 EB 07 JMP SHORT THERE
See the above line which is taken from the list file of the corresponding program. It
shows the address (0006) at which this instruction is stored .The opcode of this direct
JUMP instruction is EB and the ‘displacement’ is 07.
During the time that this instruction is decoded, the IP value is 0008 (address of the
next instruction) . At the beginning of the first pass ,the assembler reserves one byte
space in memory for the address of THERE. The assembler knows that only one byte is
required for THERE , because of the directive SHORT. .Otherwise it would have
reserved two bytes space ,as for a general near jump. If the address of the next
instruction (0008) is added to the sign extended displacement (0007) of the jump
instruction(this displacement is calculated by the assembler) ,the address of THERE is at
location 0008H + 0007H = 000FH . Thus control branches to location 000FH.
Now see the other direct jump instruction in the program
0016 EB E8 JMP SHORT BEGIN
The opcode is EB and the displacement is E8. JMP BEGIN is seen to be a backward
jump as BEGIN is a label appearing much before this instruction line.
The assembler calculates this displacement (which is negative) and add this displacement
to the value of IP at that point of the instruction sequence. Then it jumps to the
destination BEGIN. In this case the negative displacement is E8H .When JMP SHORT
BEGIN is executed ,the IP content will be 0018H . For jumping to BEGIN, the address
calculation is 0018+FFE8=0000 ,which is the address of the instruction with the BEGIN
label .Note that the negative number E8H ,when used in 16 bit calculations ,has to be
sign extended. So E8H is FFE8H in 16 bit sign extended form.
Now let us write and run a fun program using an unconditional jump instruction.
Example 3.8 is a simple program named UPLOW which uses DOS interrupts to enter a
character from a key board . If this character is an upper case alphabet ,it converts it to
lower case . The ASCII character table shows that a lower case character can be obtained
by adding 20H to the upper case code .i.e. The ASCII of ‘A’ is 41H and that of ‘ a’ is
61H ,and so on .
Example 3.8a
.MODEL TINY
.CODE
.STARTUP
START: MOV AH,01H ;AH=01
INT 21H ;enter a key
ADD AL,20H ;add 20H to it
MOV DL,AL ;move the sum to DL
MOV AH,02 ;AH=02
INT 21H ; use this function to display
MOV DL,0AH ;0AH is the ASCII for newline
MOV AH,02H ;display a new line
INT 21H
JMP START ;go to start to repeat
.EXIT
END
Example 3.8b
C:\masm6.14\BIN>UPLOW
Aa
Bb
Cc
Dd
Ee
Ff
Gg
Hh
^C
C:\masm6.14\BIN>
INT 21H with function number 01 is used to enter a key .Then 20H is added to the
ASCII value of the key ,and this is displayed in the same line . To get further displays on
separate lines ,the ASCII character 0AH is displayed .Remember that 0AH corresponds
to ‘newline’. The JMP START instruction is used to repeat this whole sequence
indefinitely .Since there is no stopping condition for this loop ,we have to use the ’ctrl c’
combination of the key board to terminate the program . The displayed output is shown
in example 3.8b ,Note that ,for any key input other than an upper case character ,the
output is some random character.
Example
JMP [SI] ;jump to the address which is stored in the memory location pointed
;by SI
If SI =0670H , the destination address is taken from the data segment at locations 0670H
and 0671H. IP is replaced by the value at these addresses.
Conditional jumps are the best part of the idea of control transfer .They change the
sequence of program execution based on the result of a computation which causes flag
bits to be set or reset .However ,there is one case (JCXZ) where a register content is
checked.
Note: All conditional jumps are short jumps
.
Usage : J(condition) destination
Table 3.1 contain the jumps used for unsigned data ,and those directly testing flags and
registers.
Note
i) Certain mnemonics are equivalent–For example ,JZ and JE mean the same as
they test the same flags. So also JC and JB , JP and JPE etc. More can be
identified from the list.
ii) JCXZ tests the CX register but other Jump instructions test conditional flags
only.
iii)There is another set of conditional jump instructions which are specially meant
for signed arithmetic .The list of those instructions and their usage will be dealt
with, in Chapter 4.
Defining an address as external and then using a jump to this address is also possible .
These ideas will be discussed in Chapter 5.
LOOP can be combined with other conditions ,to make it a conditional instruction.
LOOPNE/ LOOPNZ , LOOPE/LOOPZ etc can be used. These instructions test the zero
flag ,as well as the value of CX
Example 3-9 shows the use of an unconditional LOOP instruction.
In this program , three arrays have been declared in the data segment .Corresponding
bytes from the first two arrays are to be added and the sum is to be saved in the third
array . To point to the data items in the three arrays ,three registers are used . Data is
accessed using register indirect addressing .After one round of these actions ,the pointer
registers are incremented using the INC instruction .This instruction adds 1 to the
registers . This sequence of operations is repeated six times ,by using the LOOP
instruction .Once CX=0 ,the loop is exited ,and the program ends.
Example 3.9a
.MODEL SMALL
.DATA
NUMS1 DB 45H,67H,89H,65H,34H,23H
NUMS2 DB 09,09,12H,13H,08,02
NUMS3 DB 6 DUP(0)
.CODE
.STARTUP
.MODEL SMALL
.DATA
NUMS1 DB 45H,67H,89H,65H,34H,23H
NUMS2 DB 09,09,12H,13H,08,02
NUMS3 DB 6 DUP(0)
.CODE
.STARTUP
MOV CX,6
MOV BX,0
REPEA: MOV AL,NUMS1[BX] ;point to the first array
ADD AL, NUMS2[BX] ;add numbers of the second array
MOV NUMS3[BX] ,AL ;save in the third array
INC BX
LOOP REPEA ;repeat until CX-0
.EXIT
END
Now ,we will digress to the discussion of arithmetic instructions ,but you can see that
branch instructions in various forms will be continued to be used.
There are certain instructions which can be used to set/reset flags .This is very relevant
in the case of control flags like direction flag ,interrupt flag etc . But there is one
conditional flag which can be set /reset using instructions ,and that is the carry flag. The
relevant instructions for it are:
i) CLC -Clear carry
ii) STC -Set Carry
iii) CMC -Complement carry
Now we will see the format and usage of some of the important and commonly used
arithmetic instructions.
1)ADD -Add
Usage: ADD destination ,source
This instructions adds the destination and source and puts the sum in the destination. All
conditional flags get affected.
Examples
ADD AH,AL ;add AL to AH ,sum in AH
ADD AL, COSTP ;add the byte in COSTP to AL ,sum in AL
ADD BX,0987H ;add the number 987H to BX ,sum in BX
ADD CX ,[BX ] ;add the word in the location pointed by BX to CX
;sum in CX
2) ADC –Add with carry
Usage: ADC destination ,source
This instruction adds CF and source to the destination ,and puts the sum in the
destination. There are three operands ,of which the third is the carry . All conditional
flags get affected
Examples
ADC AH,0 ; AH=AH+0+CF
ADC [BX],AL ;add the byte pointed by BX with AL, and CF .
;put sum in the location pointed by BX
ADC AX,[BX][SI] ;add to AX , CF and the word with EA=BX+SI
;sum in AX
3)INC -Increment
Usage: INC destination
This instruction adds 1 to the destination . All conditional flags except the carry flag
get affected.
INC BX ;add 1 to the content of BX
INC [BX] ;add 1 to the content of the memory location pointed by BX
INC AH ;add 1 to the content of AH
Example 3.10
The following is a program which adds two bytes stored in memory . The sum of the
bytes is likely to be greater than FFH. Hence a word space needs to be allocated for the
sum.
.MODEL SMALL
.DATA
NUMS DB 95H ,0FCH ;store the two bytes
SUM DW ? ;allocate a word space for the sum
.CODE
.STARTUP
MOV AX,0 ;AX=0
CLC ;clear the carry flag
MOV AL,NUMS ;move the first number to AL
ADD AL,NUMS+1 ;add the second number to AL
ADC AH,0 ;add AH and CF and 0
MOV SUM,AX ;the sum in AX is moved to memory
.EXIT
END
First AX is loaded with 0.Observe how a byte addition causes a result to be a word .The
sum of two bytes which are added to AL is too large to fit in AL. Hence it generates a
carry .The next instruction adds CF ,0 and AH which contains a 0. So we find that the
carry bit is accommodated in AH .Thus AH-AL i.e. AX contains the sum which is a
word.
Example 3-11 adds 10 bytes stored in memory . The sum of these ten bytes will not fit
into a byte location .So a word location is allocated for the sum .It must also be noted
that the sum will definitely fit into a word location .[ If there are ten bytes ,the maximum
number possible for the sum of ten bytes is only 255 x10 =2550 ,which is less than
65,536 the maximum decimal number that a word can hold]
Example 3.11
.MODEL SMALL
.DATA
NUMS DB 245,178,190,167,56,178,250,89,150,235
SUM DW 0
.CODE
.STARTUP
CLC ;clear the carry flag
LEA BX,NUMS ;BX to point to the numbers
MOV CX,10 ;move the count to CX
MOV AX,0 ;make AX=0
REPEA: ADD AL,[BX] ;add the numbers ,sum in AX
ADC AH,0 ;add the carries into AH
INC BX ;increment the pointer
LOOP REPEA ;repeat if CX is not equal to 0
MOV SUM,AX ;store AX in SUM
.EXIT
END
Example 3.12 is an interesting program which adds two double words .A doubleword is
two words long .i.e 4 bytes long .
Example 3-12
.MODEL SMALL
.DATA
LONGNUM1 DD 0F8FC6768H ;first double word data
LONGNUM2 DD 0C6EF2109H ;second double word data
SUM DD 0 ;sum
SUMC DB 0 ;space for carry out
.CODE
.STARTUP
LEA SI,LONGNUM1 ;SI as pointer to first operand
LEA DI,LONGNUM2 ;DI as pointer to second operand
LEA BX,SUM ;DX as pointer to the sum
CLC ; clear carry
MOV CX,2 ; CX to count the number of words
REPEA: MOV AX,[SI] ;move to AX the first word
ADC AX,[DI] ;add the second operand and carry
MOV[BX],AX ;the lower word of result stored
INC SI ;increment SI twice to point to
INC SI ;next word operand
INC DI ; increment DI twice to point to
INC DI ;second word operand
INC BX ;increment BX twice to point to
INC BX ;second word of the sum
LOOP REPEA ;go back if CX is not 0
MOV DL,0 ;make DL=00
ADC DL,0 ;add the carry to DL
MOV SUMC,DL ;move the carry to SUMC
.EXIT
END
OFFSETS IN DATA
MEMORY
000C 01 SUMC
000B BF
000A EB
0009 88
0008 71 SUM
0007 C6
0006 EF
0005 21
0004 09 LONGNUM2
0003 F8
0002 FC
0001 67
0000 68
LONGNUM1
Table: 3.4 Relevant portion of the data segment after the execution of Example
3.12
3.4.2 SUBTRACTION
1)SUB -subtract
Usage : SUB destination ,source
This instructions subtracts the source from the destination .The result is in the
destination. All conditional flags are affected
Examples
SUB AX,BX ;subtract BX from AX
SUB AL,[BX] ;subtract the byte pointed by BX from AL
SUB COST[SI], CX ;subtract CX from the word with EA=COST+SI
SUB AX,8956H ;subtract 8596H from AX
SUB CL,BYTE PTR [SI] ; subtract from CL the byte pointed by SI
Example 13 .13
Explain what occurs on execution of the following instructions
MOV CL,0B5H
SUB CL,0FCH
Solution
B5 H 181 1011 0101 -
- FC H - 252 1111 1100
B9 H -71 1011 1001
This is a subtraction of a bigger number from a smaller number .Hence the carry flag is
set to indicate that a borrow has been used in the subtraction. The difference is -71
(decimal),which is represented as B9H which is the two’s complement representation of
decimal -71.. The carry flag is set in indicate that the subtraction has used a borrow .The
sign flag is set to indicate that the MSB of the result is 1 ,which ,in this case is interpreted
to be a negative number.
Now Let us use the SUB instruction in an interesting example .
Division is an operation which can be achieved by repeated subtraction .Many early
microprocessors (8085 ,for example) had no divide instruction. The 8086 ,however has a
divide instruction .But here ,let us use repeated subtraction to divide say ,100 by 9 . We
know that the quotient is 11 (0BH) and the remainder is 1 .
Example 3.14
.MODEL SMALL
.DATA
QUOTIENT DB 0
REMAINDER DB 0
.CODE
.STARTUP
MOV AL,100 ;copy dividend to AL
MOV CL,0 ;CL ,the quotient register=0
REPEA: SUB AL,9 ;subtract 9 from 100
JC OVER ;stop when CF =1
INC CL ; increment CL if CF is not set
JMP REPEA ;repeat subtraction if CF is not set
OVER: ADD AL,9 ;add 9 to AL to get remainder
MOV REMAINDER,AL ;store remainder
MOV QUOTIENT,CL ;store quotient
.EXIT
END
The SBB instruction can be used in instances when multibyte subtraction is done .For
example ,to subtract two double words or quad words, SBB will have to be used ,just as
ADC is used (Example 3.12)for multibyte addition.
We have seen so far, addition and subtraction operations using numbers in the
hexadecimal (essentially binary) format .But numbers are also represented in other
formats like BCD ,ASCII etc . There are special like instructions like DAA and DAS that
cater to BCD numbers ,and instructions like AAA, AAS etc which cater to ASCII
operations .In Section 4.4.2 , these instructions will be used in arithmetic calculations
using such numbers.
Consider the instruction CMP destination ,source .The condition of the flags will be as
shown in Table 3.5
If CF ZF
destination > source 0 0
destination < source 1 0
destination =source 0 1
It is thus obvious that following up a compare instruction with a JNC/JC or JNZ/JZ will
do the trick .
If the instruction is ,say CMP AL,BL the way to picture it is -
If AL> BL , CF is reset
if AL< BL ,CF is set
if AL= BL , ZF is set.
If two unsigned numbers are to be compared ,it will be clearer if we use JA/JB as the
mnemonic following the compare instruction ,instead of JC or JNC . This will make it
easier to comprehend the meaning of the result of the comparison ,.
If we want to compare AX and BX we could think of it as :
Jump to target if AX is above BX .The instructions to use will be
CMP AX.BX
JA Target
Similarly ,when testing for equality ,we could use JE/LNE instead of JZ/JNZ .Now ,let
us write a few programs illustrating the use of the compare instruction.
These tips are only for enhancing the readability of the program .
Example 3-15
Two unsigned words are stored in the data segment in locations WORD1 and WORD2
.The program compares the two numbers and displays a message stating which number is
bigger.
.MODEL SMALL
.DATA
WORD1 DW ------- ;store the first number
WORD2 DW ------ ;store the second number
MES1 DB "WORD1 IS BIGGER$" ;first message string
MES2 DB "WORD2 IS BIGGER$" ;second message string
.CODE
.STARTUP
This is a very simple program in which the WORD1 is brought into AX .It is then
compared with WORD2 . The jump (JB) instruction following the comparison, directs
control to the appropriate message . Recall that DOS function call with AH=09 will
display a string on the console.
Note : To run this program , actual data will have to be written in the data segment at
locations WORD1 and WORD2
.MODEL SMALL
.DATA
NUMS DB 56H,38H,09H,98H,99H,0C7H,07H,0BCH,0CH,0ECH
BIGGEST DB ? ;store the biggest number
.CODE
.STARTUP
Example 3.17 is another an interesting program ,which searches a character string for
the presence of a particular character ,and displays appropriate messages.
Example 3-17
.MODEL SMALL
.DATA
STRIN DB "HOWAREYOUMYBOY" ;string of characters
LEN DW 0EH
MESG1 DB 0AH,0DH,"CHARACTER FOUND$"
MESG2 DB 0AH,0DH,"CHARACTER NOT FOUND$"
.CODE
.STARTUP
LEA BX,STRIN ;BX to point to the string
MOV CX,LEN ;copy the string length to CX
.EXIT
END
A character string is a stored in location labeled STRIN .The length of this string is
specified in a word location named LEN . The character whose presence (or absence) we
want to check for , is entered through the keyboard using the DOS function call with
AH=01 . This character is compared with each of the characters in the string .Once
equality is found ,the comparing loop is exited and MES1 is displayed . Otherwise
MES2 is displayed ,after all the characters in the array have been compared .
Note that both the messages (MESG1 and MESG2) are preceded by the newline (0AH
) and carriage return (0DH )characters .This is to ensure that the display is obtained in a
different line ,after the input character (i.e. the one that is entered through the keyboard)
is echoed on the screen.
3.4.4 UNSIGNED MULTIPLICATION
MUL -Multiply
Usage :MUL source
This instruction multiplies a number in AL or AX by the source (where the source can be
a register or a memory location ,but not an immediate number).
All the conditional flags are affected ,but only the CF and ZF are defined as meaningful
for the result. The destination depends on the size of the operand.
There are two ways of performing multiplication .
i) Byte by byte
In this ,one of the operands must be in the AL register ,and the source can be a byte in
a register or memory location. The product (a word) will be in AX .
For example
MUL BL ;multiply BL by AL –product in AX
MUL BYTE PTR[SI] ;multiply the byte pointed by SI, by AL –product in AX
MUL BIG ;multiply the content of BIG by AL –product in AX
Flags Affected
For the multiply instruction ,all conditional flags are affected ,but only the carry (CF)
and overflow(OF) flags have any significance . They will be set or reset according to the
size of the product .Recall that a byte x byte multiplication can produce a result of a word
size .But if the operands are small, the product itself is only a byte, and needs only the
AL register for the product. .In that case ,both the carry and overflow flags are found
cleared ,but if the product occupies a word size ,both these flags are set (CF=1,OF=1).
This concept can be extended to the word x word multiplication as well .We can
summarize that if the product has a size equal to the size of the operands ,both these flags
are reset .i.e. CF=0,OF=0 .However ,if the product is large enough to occupy the registers
assigned for it, these flags are set i.e. CF=1,ZF=1. See this multiplication.
MOV AL 78H
MOV CL ,0F9H
MUL CL
This program segment will cause the product to be 74B8H .Thus the result of the
multiplication is AX =74B8H ,CF =1 and OF=1.
However if the program segment is
MOV AL,13H
MOV CL ,09H
MUL CL
The result is AX=00ABH ,CF=0 and OF=0 .
In example 3-18, two bytes stored in the data segment are multiplied .The result of
multiplication is available in AX ,which is then moved to the location PROD , a word
location .
Example 3.18
.MODEL SMALL
.DATA
MULT DB 0AH ;multiplier
MULP DB 0F6H ;multiplicand
PROD DW ? ;space allocated for product
.CODE
.STARTUP
MOV AL,MULP ;move the multiplicand to AL
MUL MULT ;multiply with the multiplicand
MOV PROD,AX ;product moved from AX to memory
.EXIT
END
Example 3-19
.MODEL SMALL
.DATA
FACT DW 0 ;space allocated for the factorial
.CODE
.STARTUP
MOV AH,01
INT 21H ;enter N from the keyboard
SUB AL,30H ;convert ASCII to binary
MOV AH,0 ;convert N in AL to a word in AX
MOV BX,AX ;move it to BX
MOV AX,1 ;AX=1 ,to start the iteration
CMP BX,0 ;compare BX (=N)to 0
JZ FINAL ;if N=0 ,jump to find 0!
REPEA: MUL BX ;for N not 0, multiply with AX
DEC BX ;decrement BX
CMP BX,0 ;compare with 0
JNE REPEA ;repeat if BX is not 0
FINAL: MOV FACT,AX ; AX=1 ,hence 0!=1
.EXIT
END
For division ,if the divisor is zero ,the quotient becomes undefined In attempting such a
division ,the 8086 will exit from this program and generate an interrupt .This state is said
to a ‘divide by zero error ‘.An interrupt generated by an error ,is termed an exception.
But division by zero is not the only condition to cause such an error . If the quotient
register is too small to accommodate the quotient ,then also this happens .For example ,if
the dividend is a large number and the divisor is very small ,such a condition is possible .
MOV AX,09876H
MOV CL,25H
DIV CL
The above program segment will give a quotient of 41E H ,which obviously cannot
be accommodated in AL . If this problem had been foreseen , the solution would be to
convert the dividend to a double word, and the divisor to a word. .Then the quotient will
be in AX ,which will be big enough for it ..When such an error occurs ,program
execution is aborted and the assembler displays the message ‘divide overflow error’.
EXAMPLE 3-20
.MODEL SMALL
.DATA
FIRST DB 89H
SECOND DB 0CAH
AVG DB ?
.CODE
.STARTUP
MOV AL,FIRST ;copy FIRST to AL
MOV AH,0 ;zero-extend
MOV BL,SECOND ;copy SECOND to BL
MOV BH,0 ;zero-extend
ADD AX,BX ;add the zero extended words
MOV CL,02 ;load divisor to CL
DIV CL ;divide AX by CL
MOV AVG,AL ;save quotient to AVG
.EXIT
END
Example 3-20 gives a complete program for finding the average of two bytes stored in
memory locations labeled FIRST and SECOND .These are copied to registers AL and BL
and zero extended to convert them to words. Addition of the numbers is then done as
words. This sum ,which is the dividend is in the AX register. The divisor 2 is copied to
the CL register. After division ,the quotient is available in AL and the remainder in AH
.For this problem ,only the quotient is important ,and this is saved to location AVG.
Now we will see a very useful and interesting application of the division operation.
This program converts a 16 bit hexadecimal number to decimal and displays the decimal
number on the console after converting it to ASCII form.
To see the logic of this program ,let us take a number ,say ,246 .
i) Divide this by 10 to get a quotient of 24 and a remainder of 6 .Push
the remainder on to the stack .
ii) Next ,divide the quotient again by 10 to get 2 as the quotient and 4 as
remainder .Push this remainder also on to the stack.
iii) One more division causes the quotient to be zero .This is the stopping
condition in the division loop..
iv) The number of division operations done is counted along with all these
steps. Now pop out the remainders from the stack .This will be in the
reverse order of the push operations .Thus 2 will be popped out first
and 6 last .This can be displayed in this order.
v) For displaying ,each digit must be converted to its corresponding
ASCII .This is done by adding 30H to each digit .
Now ,examine Example 3.21 the program that does the conversion of any 16 bit
hexadecimal number to decimal and then displays it. Here, the hexadecimal number is
placed in the data segment. Note that the division here is accomplished as a double word
by word division.
Example 3.21
.MODEL SMALL
.DATA
NUM DW 0CEF6H ;the hexadecimal number
COUNT DB 0 ;count of the number of divisions done
.CODE
.STARTUP
Example 3-22
Find the result and the state of the FLAGS CF,ZF and OF due to the following
instructions.
Given AX =008CH ,BX =345EH , CX=67EBH
II) AND BL,CL
III) OR AH,BH
IV) XOR AL,CH
V) TEST AH,BL
Solution:
i) AND BL,AL
ii) OR AH,BH
AH = 00 0000 0000
BH = 34H 0011 0100
34H 0011 0100
After this operation , AH=34H .CF=0,OF=0,ZF=0
i) TEST AH,BL
AH = 00 0000 0000
BL = 5EH 0101 1110
MASKING
There is a word ‘masking ‘ associated with the AND operation .Masking is used to select
the part of a word or a byte needed ,while making the unwanted bits to be zero
For example ,observe the value in AL after the following instructions are executed.
MOV AL, 78 H
AND AL ,0F H
This gives AL=07H in the AL register .The upper nibble of 78 H has been masked.
As another example ,see below where 16 bit data is ANDed with 0FFFH ,so as to mask
the upper 4 bits., This is a particular case when only 12 bits of this data are needed .
AND
To convert an ASCII number to a binary number ,mask the number with 0FH
MOV AH,01 ;get in the number through the keyboard
INT 21H
AND AL,0FH ;mask the upper nibble
OR
To convert an 8 bit binary number (from 0 to 9) to ASCII , OR it with 30H
MOV AL ,9 ;AL=09
OR AL,30H ;AL=39H
XOR
To clear a register ,use XOR
XOR BL,BL ;BL=0
TEST
To test whether a bit is set or not use the TEST instruction .
TEST BL,01H
This tests whether bit D0 of BL is 1 or not .If the bit under test is reset ,the AND
operation corresponding to the TEST operations causes ZF to be set.(ZF=1)
TEST CX,8000H
This tests whether D15 of CX is set or not . .The result of this that ZF=0 as D15=1.
3.5.1 SHIFT
i) Shift instructions do arithmetic or logical shifting
ii) They also shift right or left .
iii)Arithmetic shift is used for signed number operations ,while logical shift caters
to unsigned numbers.
iv)Shifting right causes a divide by 2 ,for each bit position shifted, while shifting
left corresponds to a multiplication by 2.
v)The count means the number of bit positions ,by which shifting is to be done.
vi)If the count >1, load it in CL ,otherwise use ‘1’ in the immediate mode.
Fig
Examples
Example 3-23shows a program segment for finding the weight of the modulo-2 sum of
two numbers.. Modulo-2 sum essentially is finding the positions in which the two
numbers are different – which is an XOR operation. The weight of a number is the
number of 1s in it .
In this problem , the modulo-2 sum is found by XORing the two numbers in AL and BL.
The result of this is then shifted left .The bit shifted out will be in the carry flag On
checking the carry bit ,if it is found to be 1, the register CH is incremented .Along with
this ,the register CL is decremented .CL contains the length of the operand . Here CL=8.
At the end of the program ,we get the ‘weight’ of the modulo-2 sum of AL and BL in
CH. For the numbers used in this program ,CH=6 is the result.
Example 3.23
.MODEL TINY
.CODE
.STARTUP
MOV AL,89H ;move first number to AL
MOV BL,0F7H ;move second number to BL
XOR AL,BL ;EX-OR the two numbers
MOV CL,8 ;CL=8
BACK: SHL AL,1 ;shift left AL once
JNC REPEA ;repeat if there is no carry
INC CH ;increment CH if there is a carry
REPEA: DEC CL ;decrement count
JNZ BACK ;repeat the shifting until CL=0
.EXIT
END
Fig
Examples
Example 3.24
Convert a packed BCD byte to two unpacked bytes.
Solution:
.MODEL TINY
.CODE
.STARTUP
MOV AL,95H ;copy the packed BCD byte to AL
MOV BL,AL ;copy the same byte to BL
AND AL,0FH ;mask the upper nibble of AL
AND BL,0F0H ;mask the lower nibble of BL
MOV CL,04 ;move a count of 4 into CL
SHR BL,CL ;shift BL right 4 times
.EXIT
END
Note : The operation and practical use of the SAR instruction will be discussed in
Section 4.7.4 ,where signed number arithmetic is dealt with .
Examples
ROL SI, CL ;rotate left the word in SI ,by the count in CL
ROL BYTE PTR [DI][BX],1 ;rotate left the byte pointed by EA=[DI+BX] once
.MODEL TINY
.CODE
.STARTUP
MOV BX,5634H
MOV CL,8
ROL BX,CL
MOV AL,0C6H
MOV CL,4
ROR AL,CL
.EXIT
END
In the first instance ,there is a 16 bit register BX which contains the value 5634H..By
using ROL ,and rotating 8 times the value in BX changes to 3456H. .Similarly with
AL=C6H , using ROR with a count of 4 ,Al will have the new value of 6C H. Thus we
can switch exchange nibbles or bytes using the rotate instructions with the appropriate
count in CL . Note that ,for this application ,it does not matter if ROL or ROR is used.
It is easy to check that Example 3-24 can be re-written replacing the SHR instruction
with either the ROL or ROR instruction.
Examples
RCL BL,CL ; rotate left through carry , BL ,by the count in CL
RCL CX,1 ; rotate left through carry the word in CX once
RCR - Rotate Through Carry Right
Usage: RCR dest, count
This instructions causes the right most bit (MSB) to enter the carry flag ,and the CF
enters through the left end(MSB) Thus ,due to one shift operation ,the LSB enters the CF
,and the CF gets into the MSB position.
Modifies Flags: CF OF
Examples
RCR BYTE PTR [SI] ,1 ;rotate right through carry ,the byte pointed by SI,once
RCR WORD PTR [DI][BX],CL ;rotate right through carry the word pointed by
EA=SI+BX ,by the count specified in CL
Example 3.26
Find the values in the destination for each line of this program segment.
STC
MOV AX,5485H
RCR AL,1
MOV CL,03
RCL AX,CL
MOV CL,05
ROR AX,CL
ROL AX,1
Solution:
STC CF=1
MOV AX,5485H AX=0101 0100 1000 0101
RCR AL,1 AL=1100 0010
MOV CL,03 CL=03
RCL AX,CL AX=1010 0110 0001 0101
MOV CL,05 CL=5
ROR AX,CL AX=1010 1101 0011 0000
ROL AX,1 AX=0101 1010 0110 0001
• BIOS and DOS function calls facilitate the use of input and output devices in
programming, making it interactive and more fun.
• The instruction set of 8086 can be divided into groups based on functionality.
• MOV is the most frequently used instruction and it is part of the data transfer
group.
• LEA is an instruction that copies the offset of a memory address to a 16 bit
register. It can be used to make the register be a pointer to an array of data items
in memory.
• PUSH and POP can be used only with the stack segment
• The SS register contains the upper 16 bits of the lowest (base) address in the stack
segment. SP contains the offset (with respect to the base address ) of the highest
address in the stack.
• Branch instructions cause the program sequence to change.
• JUMP is an important branch instruction which can be far or near ,and
unconditional or conditional.
• Jumps using the direct mode of addressing are re-locatable and ‘relative’.
• LOOP is a jump instruction which causes branching as well as decrementing a
count in CX. A LOOP instruction can be conditional ,as well.
• There are 20 arithmetic instructions for 8086 ,of which a few specifically cater to
signed numbers, a few to BCD numbers ,and a few to ASCII numbers.
• The ADC(Add with carry) and SBB (Subtract with borrow) instructions find use
in multibyte additions and subtractions.
• The INC and DEC instructions do not affect the carry flag.
• The CMP (Compare) instruction subtracts the source from the destination
and sets/resets flags ,but neither the source nor the destination is altered.
• MUL is the mnemonic for unsigned multiplication .For a byte by byte
multiplication ,one operand is implied to be in AL and the product in AX. For a
word by word multiplication , one operand is to be in AX and the product will be
in DX-AX.
• DIV is the mnemonic for unsigned division. For a word by byte division ,the
dividend is to be in AX .After division ,the quotient will in AL ,and the remainder
in AH .For a double word by word division ,the dividend is to be in DX-AX
.Then the quotient will be in AX and the remainder in DX.
• If the quotient is too large to fit in the register assigned for it , a ‘divide by zero’
error will be generated.
• The logical instructions of 8086 are AND,OR,XOR,NOT and TEST.
• The TEST instruction does logical ANDing ,but only the flags are affected.
• Shifting of a data can be done left or right ,and also by as many positions as
required.
• Arithmetic shifting is meaningful for signed numbers , while logical shifting
pertains to unsigned arithmetic.
• Rotation can be done left or right and can be direct or through the carry flag.
QUIZ
1. Distinguish between the top-down and bottoms-up approach in programming.
2. What is the use of DOS function calls in assembly programming?
3. How many operands do each of the following instructions have?
a. ADD ii) ADC iii) INC
4. Which conditional flag is not affected by the DEC instruction?
5. If AX=5600H and BX=0C07H ,find the contents of CX and DX after the
following three sets of instructions.
i) PUSH AX ii) PUSH AX iii) PUSH BX
PUSH BX PUSH BX PUSH AX
POP CX POP DX POP CX
POP DX POP CX POP DX
6. Why is the stack pointer called the TOP OF STACK?
7. Distinguish between a near and a far jump.
8. Specify two ways of specifying an unconditional jump in the indirect mode of
addressing.
9. Why does the far jump have five bytes of instruction length?
10. Name a conditional jump instruction that does not test a conditional flag.
11. What are the conditions under which a LOOPZ instruction exits looping?
12. What does the instruction STC do?
13. Can the ADD instruction use CS as a destination?
14. The instruction INC [SI] seems ambiguous .Why? How can the ambiguity be
corrected?
15. What does the instruction CMP AX,BX do?
16. What is the role of OF and CF in multiplication?
17. What happens if the DIV instruction is used for a dividend of 1938H and divisor
of 05?
18. What is meant by masking? Give an example of how it is used?
19. How can multiplication and division be achieved by shift operations?
20. What is the difference between the instructions RCL and ROL?
EXERCISE
1) Write a program to print on two separate lines ,the messages ‘Hi, folks’
and ‘I am Samantha’.
2) Enter a character through the keyboard without echoing it on the video monitor.
Compare it with a number stored in memory and display the message ‘EQUAL’
or ‘NOT EQUAL’ as the case may be.
3) Display each character of the word PRADO on a separate line.
4) Enter ‘N’ characters through the keyboard without echo. Save it in memory as an
array .Then display it as a character string.
5) Indicate what is wrong (if anything is wrong) with each of these instructions.
i. MOV CX,DL
ii. ADD DATA1,0978H
iii. MOV BYTE PTR [SI][BX],DX
iv. MOV 045FH,AX
v. MOV [DX],AL
vi. MOV [SI][CX],AX
vii. MOV DS,3453H
6) Write a program that adds two quad words and stores the result in memory.
7) Write a program that subtracts two double words and stores the result in memory.
8) Find the status of the CF and ZF flags after the execution of the following sets of
instructions.
i) MOV AX,9078H
CMP AX,0C089H
ii) XOR AL,AL
iii) MOV AL,29H
CMP AL,0
9) Find the biggest number in an array of i) bytes ii) words. Display the biggest
number in each case ,as a decimal number.
10) There are 10 unsigned bytes stored in memory .Arrange these bytes in i)
ascending and ii) descending order.
11) Find the average of 20 bytes stored in memory .Display the average as a decimal
number.
12) Add the sum of the first 20 natural numbers .Display the sum.
13) Write a program that counts the number of 1s in a binary number.
14) Write a program to add two N x N matrices.
15) Find the number of times a particular character is present in a character string
stored in memory.
CHAPTER 4
PROGRAMMING CONCEPTS-III
OBJECTIVES
Consider the case of 100(say) words or bytes in a particular memory area that is to be
moved to another memory area .This can very well be done using pointer registers and
looping using a counter . But, this whole process can be, sort of, automated with less
number of instructions ,if we use string instructions. Similarly, we may need to
compare two blocks of data for equality ,or search a data block for a particular data
.In all these cases ,string instructions make our task easier and our code shorter.
But before using these instructions, we have to include a few initialization steps in our
code.
Pre-requisites for using string instructions
i. Two segments are to be defined i.e. the data segment and the extra segment. This
means that the corresponding segment registers DS and ES have to be initialized
and used. The data segment is the source segment and the extra segment is the
destination segment.
ii. The DI register and SI registers should act as pointers to the data segment and
extra segment respectively.. This means that, initially, SI should contain the
address (offset) of the first location of the data segment .Similarly , DI should
contain the address(offset) of the first location of the extra segment.
iii. There is a control flag called the direction flag which is used exclusively for
string operations .Its purpose is that in string operations, if the flag is set, the
pointer registers get automatically decremented and if reset, the reverse occurs
.So whenever string instructions are being used, the direction flag (DF)should be
set or reset depending on the direction the addresses are to be modified after each
operation
iv. The counter CX should be loaded with the count of the number of operations
required.
Note In string instructions ‘SB’ stands for ‘string byte ‘ and ‘SW’ for ‘string word’
Table 4.1 List of instructions/prefixes used in string operations
.
4.1.1 The MOVS instruction
Example 4-1 shows the use of the string instruction MOVSB with the necessary pre-
requisites incorporated .Let us examine how this has been done.
The problem in hand is to transfer 10 bytes (the character ‘*’) from an area of memory
designated as DATA1 to an area named DATA2. Thus the source has to be the data
segment ,and the destination the extra segment. .How do we incorporate. the extra
segment?
If we use the full segment model ,we can define two segments i.e.-the data
segment(DAT) and the extra segment (EXTR) .See how it is done. When both the
segments registers are defined, the segments get defined. Then the program instructions
cause the data in the data segment to be copied to the extra segment. Example 4-1a uses
the full segment model for this.
Example 4-1a
DAT SEGMENT
DAT1 DB 10 DUP('*') ;store ‘*’ in 10 locations
DAT ENDS
EXTR SEGMENT
DAT2 DB 10 DUP(0) ;allocate 10 locations
EXTR ENDS
COD SEGMENT
ASSUME CS:COD,DS:DAT,ES:EXTR
MOV AX,DAT
MOV DS,AX ;initialize the DS register
MOV AX,EXTR
MOV ES,AX ;initialize the ES register
In Example 4-1a , two segments DAT and EXTR are defined ,and two segment
registers are initialized –i.e. DS and ES. The addresses SI and DI point to the source data
and destination data addresses and DF is cleared for auto incrementing the pointer
registers.
Now let us do the same program using the simplified model. The small model can have
only one data segment ,so how can we have an ‘extra segment’? The solution is to have
both data areas in the data segment itself ,but after the data segment is defined, copy
the value of DS into ES –this makes the assembler believe that the extra segment is
available-though it is the same as the data segment. In this example ,the destination data
area is chosen to be at org 0200H .This is done just to space the source and destination
data areas and is not mandatory. Thus the first pre-requisite of having both the data and
extra segments is satisfied.
Next, the SI register is made to point to the address DATA1 and the DI register to
DATA2 . Then the counter register is loaded with 10 ,the count of data transfer
operations required . The instruction CLD is used to clear the direction flag(DF). This
causes the value of SI and DI to be incremented after each move operation. Here the
data is in the form of bytes. Hence SI and DI are incremented only by one each time .The
string instruction used here is MOVSB (prefixed by REP).This causes the data in the
location pointed by SI to be moved to the location pointed by DI. After each such move
operation , two actions occur. One is that CX decrements by 1,and the other is that the
pointer registers are incremented. This sequence continues until CX=0. Example 4-1 a
and b illustrate these steps.
Example 4-1b
For the above program ,if the data involved is in the form of words, the only change
would be to replace the line REP MOVSB with REP MOVSW. In this case, CLD will
cause SI and DI to be incremented by 2
4.1.2 The CMPS instruction
Now ,let use the next string instruction –i.e. CMPSB/CMPSW .This instruction is for
string comparison , byte by byte or word by word as the case may be. The conditional
flags are modified according to the result of the comparison. String comparison has to be
accompanied by the use of the conditional REP prefix .Since string comparison checks
only for equality, the Zero flag is made use of automatically.
Example 4-2 compares two character strings (of length six) which are saved in the data
segment. One of the two messages are to be displayed depending on whether WRD2 is
the same or not the same as WRD1.
Example 4-2
.MODEL SMALL
.DATA
WRD1 DB "SAMSON" ;store the first word
WRD2 DB "SAMRON" ;store the second word
GUD DB "SAME$" ;store the message for equality
BAD DB "NOT SAME$" ;store the message for inequality
.CODE
.STARTUP
MOV AX,DS
MOV ES,AX ;Copy DS to ES
LEA SI,WRD1 ; point SI to source
LEA DI,WRD2 ;point DI to destination
MOV CX,6
Here SI and DI point to the two character strings ,CX stores the count and the direction
flag is cleared for auto incrementing the address pointers .REPE is used as the prefix for
comparing the string bytes. REPE CMPSB means that the corresponding string bytes of
the source and destination are compared as long as they are equal. Equality also implies
that the Zero flag is set .The comparison is stopped as soon as an inequality is
encountered. One of the following two conditions cause the comparison loop to be exited.
i)an inequality is seen –i.e. the Zero flag gets reset
ii)normal exiting when CX=0.
In the example, the two character strings are shown to be different, which causes the
message ‘NOT SAME’ to be printed .If WRD1 and WRD2 are the same, the message
‘SAME’ is printed.
Example 4-3
.MODEL SMALL
.DATA
LIST DB "ANBHTSHFGTRIUUJEHNPBGDVBTH"
PR DB "CHARACTER PRESENT$"
NOTP DB "CHARACTER NOT PRESENT$"
.CODE
.STARTUP
In the above, a variation (from the previous examples) has been used .DI has been made
to point to the last character of the string ,and the direction flag is set ,such that it auto
decrements after each scanning operation.
Refer to Example 3-17 , where the same problem has been done without using string
instructions.
4.1.4 The STOS and LODS instructions
i)STOS
The STO instruction is the mnemonic for ‘storing’ in memory . As such ,we need to
define a memory area in which ‘storing’ is to be done .This memory area is defined to
be the extra segment ,and it is addressed by DI as it is the destination segment. The
data to be stored is placed in the AL or AX register. An area in memory can be filled with
the required data with this instruction.
Example 4-4 illustrates the use of the STOS instruction. Here AX is loaded with 0001
,and this is moved to a location named AREA., which has been defined as a array of 50
words.. This array is filled with the word 0001 by the STOSB instruction ,using the
REP prefix and the counter CX initialized with 50. Since STOS is specified for the extra
segment , ES register has to be initialized .. Notice that , as done earlier ,a data segment
is first defined (by the .DATA directive) and the DS register content is then moved to
ES .Note also that the memory AREA is addressed using the DI register .SI register
cannot be used.
Example 4-4
It is obvious that the STOS instruction is used to fill up an area in memory with the same
data.
ii)LODS
The last of the string instructions is LODS. This is an instruction for ‘loading’ .Loading
always means the act of taking data from memory and putting it into a register .Here ,the
source memory is the data segment ,the pointer to it is SI.. The data segment is the source
segment and the destination register is the AL or AX register. There is no sense in using
the REP prefix for the LODS instruction as data can be loaded to AL/AX only once .
4.2 PROCEDURES
In high level languages (C ,C++ etc.)you might have come across ‘functions’ .A
function is a program which does a specific task .When this task is to be done repeatedly,
the function is used again and again .When a ‘main’ program considers this as a
subsidiary task ,the function(or sub routine) is ‘called’ whenever its service is required.
This is applicable to assembly language programming also.. Borrowing Intel’s
terminology , we shall call it a ‘procedure’.
The figure below shows two cases. One is the case of a main program calling many
different procedures .The second is a program calling the same procedure repeatedly.
........
CALL MULT
........
........
........
........
PROC MULT NEAR
........
Fig 4-2 Call .and
. . . Return
....
........
. . . . . . .in. a main program . Then the procedure MULT is
Fig 4-2 shows a CALL encountered
RET
taken up for execution. At the end of the procedure ,there is the RET instruction ,
executing which causes control to return to the main program .The instruction after the
CALL instruction will now be taken up.
Example 4-5
.MODEL SMALL
.DATA
NUMBERS DB 10 DUP(0) ;allocate space for squares
.CODE
.STARTUP
MOV CX,10 ;load count in CX
LEA SI,NUMBERS ;let SI point to NUMBERS
REPEA: MOV AH,01 ;key board input
INT 21H
SUB AL,30H ;convert from ASCII to binary
CALL SQUARE ;call the procedure for squaring
MOV [SI],AL ;store the square in memory
INC SI ;increment pointer
LOOP REPEA ;decrement CX and repeat if CX!=0
.EXIT ;return to DOS prompt
In the above, the square of the numbers has been calculated and saved in memory .It may
be interesting to display them on the monitor. .This can be done by referring to Example
3-21 .The program for converting any hexadecimal number to binary and displaying can
be made another procedure and called appropriately here.
4.2.2 CALL AND RETURN INSTRUCTIONS
Now that we have seen the general format and structure of a program with procedures, let
us examine the CALL instruction in greater detail. This instruction is obviously a
‘branch’ instruction because it causes control to move to a different address (called the
target or destination) ,which may be in the same or a different code segment.
The direct call is like a direct jump instruction ,and is three bytes long ..It is relative and
the destination can be -32,768 bytes to +32,767 bytes from the address of the instruction
following the call (this will be the current content of the IP). This means that the offset
can be a 16 bit signed number .When this call is executed, the new value of IP =old IP+
offset , where the second and third bytes of the instruction give the offset. The assembler
must calculate the offset during its first pass. Example 4-5 uses this type of the call
instruction –i.e. the direct near call.
Example
CALL NEW_WAY ; using the label, the assembler calculates the offset
CALL MULTI
ii) Indirect CALL
Usage: CALL reg16 ,CALL [reg16]
In this case, the destination is specified in a 16 bit register or in a memory location
pointed by a register. This is not a ‘relative’ call. The content of the referred register or
memory location is loaded into IP for using the procedure.
Example
CALL BX ;the procedure’s address is in BX
CALL WORD PTR[BX] ;the address of the procedure is in memory and
; is pointed by BX
EXAMPLE 4-6
.MODEL TINY ;select tiny model
.CODE ;start code segment
.STARTUP ;start program
LEA BX,ENT ;offset of ENT in BX
LEA SI,DISP ;offset of DISP in SI
CALL BX ;call the ENT procedure
ADD AL,20H ;upper case to lower case
MOV DL,AL ;transfer lower case to DL
CALL SI ;call the DISP procedure
.EXIT
.MODEL SMALL
.DATA
----------------
.CODE
.STARTUP
..
..
..
CALL MULTI
..
..
..
Now see Example 4-7b .The main program is not shown .What is illustrated is that it uses
registers DX and CX . These registers are used in the procedure MULTI as well. The
USES construct at the start of the procedure will be converted to PUSH DX and PUSH
CX when the procedure is called ,and to POP CX and POP DX when returning from the
procedure. This can be verified by ‘debugging’ the program or by using the directive
.listall . Then the list file will show the listing of the procedure as shown below. Note the
push and pop DX and CX which we have not written specifically in the program ,but
have been caused by the USES construct.
Example 4-7b
Example 4-8
.MODEL SMALL
.DATA
A DB 4 ; A=4
D DB 5 ; D=5
N DB 20 ;N=20
NTH DW ?
.CODE
.STARTUP
MOV BL,A ; copy A to BL
MOV BH,0 ;BX=0 to have A in BX as a word
MOV AL,D ;copy D to AL
MOV CL,N ;Copy N to CL
DEC CL ;decrement CL to get N-1
CALL NTH_TERM_AP ;call the procedure
MOV NTH,BX ; save the nth term in memory
.EXIT
.MODEL SMALL
.DATA
N WORD 0009H
NUMS BYTE 10H,08,89H,78H,67H,07,2,99H,0FEH
.CODE
.STARTUP
LEA BX,NUMS ;BX to point to the number array
MOV CX,N ;move the array size to CX
DEC CX ;CX must contain N-1
MOV SI,BX ;SI to have copy of starting address
AG: MOV DX,CX ;DX=N-1
STRT: CALL ONE_SET ;the procedure for pair-wise ordering
DEC DX ; keep count of the calling
JNZ STRT ;repeat until DX=0
MOV BX,SI ;put the starting address back in BX
LOOP AG ;loop back until CX=0
.EXIT
In the above program, nine numbers (bytes)are stored in the data segment and
sorted. Any number of bytes can be sorted this way , limited only by the maximum value
of N. For sorting in descending order, only one instruction in the procedure need be
changed. After sorting is done,if we write a procedure for converting hexadecimal
numbers to ASCII , the sorted numbers can be displayed as well.
There may be reasons where the stack, which is an area in memory can be used to store
data . The procedure can take data from the stack for its computation. Ex 4-10 is a simple
example which also elaborates how the register BP is used to access data in the stack.
Here four words which are in the data segment are pushed on to the stack by the main
program. The procedure accesses the data using the indexing features of BP .Remember
that BP is a register associated (by default) with the stack segment ,and that BP cannot
be used without an offset.
Example 4-10 calculates (A+B)—(E+D) where A,B,E and D are words stored in data
memory .The result is also to be saved in the data segment .
Note : We don’t use C as a label because ‘C’ is a reserved word in MASM.
Example 4-10
.MODEL SMALL
.STACK 100H
.DATA
A DW 0987H ;data values in the data segment
B DW 678H
E DW 0A5EH
D DW 0034H
RESULT DW ?
.CODE
.STARTUP
PUSH A ; push to stack
PUSH B
PUSH E
PUSH D
CALL COMPUTE ;call the procedure
MOV RESULT,AX ;save AX in memory
.EXIT
COMPUTE PROC NEAR
MOV BP,SP ; Copy SP to BP
MOV AX,[BP+10] ; move the top most stack value to AX
MOV BX,[BP+8] ;move next word in stack to BX
ADD AX,BX
MOV CX,[BP+6] ;move the next word to CX
MOV DX,[BP+4] ;move the next word to DX
ADD CX,DX
SUB AX,CX
RET 8 ;return and add 8 to SP
COMPUTE ENDP
END
Stack Stack
Address content
011F H 09
A
011EH 87
011DH 06 B
011CH 78
011BH 0A E
011AH 5E
0119H 00 D
0118H 34
Fig 4-5: Stack operation for the PUSH and RET 8 instructions
Refer to Fig 4-5.Let’s say that before the first PUSH ,the value of SP=0120H
This figure shows the content of the stack after the four PUSH operations .
The address of SP now is 0118H.
When the CALL is executed, SP=0116 H , because the content of IP is pushed on the
stack .
On returning from the procedure, SP again contains 0118H
RET 8 causes SP=SP+8 =0120H
It is important to be very clear about stack operations if you plan to use the stack .Any
wrong alteration of the stack can cause a system crash.
Stack overflow and underflow
SP can have a maximum value of FFFFH .For each PUSH operation, The SP value
decrements by 2 ,and in the limiting case ,it can go to SP=0000 .
Any PUSH operation beyond this will cause a 'stack overflow' .This creates a condition
when there is no space in the stack for new data.
Stack underflow is the other case when POP operations cause SP to have values beyond
the defined top of stack..
4.3 MACROS
The name of our assembler is ‘Macro assembler’. As such, we see that it is expected to be
able to handle an item called ‘macros’.
What is a macro? It is like an opcode –when used ,it executes .A macro when called by
name, executes the instructions that are listed under that name. Thus essentially , a
macro is a short hand notation for a number of instruction lines. It essentially makes
assembly language coding more readable and helps to avoid repetitive coding.
Usually , a macro, like a procedure, is defined for performing a specific function .But the
‘overheads’ involved in invoking a procedure are not incurred here. A procedure call
causes pushing and popping of addresses/ data in stack .A macro when invoked just
expands the code by putting in all the instructions corresponding to the called macro . It
does not have to ‘call’ and ‘return’. Thus, when we write our code with macros it may
look small ,but when assembling the code each macro is replaced by the full set of
instructions it consists of. Thus we can say that macros execute faster but the assembled
code takes more memory .This is because a procedure is written only once in memory
,but the macro statements are written as part of the code every time the macro is invoked.
When writing programs using macros, make sure the macros are defined before they are
used. Example 4-11 is a simple illustration of these principles.
Example 4-11
.MODEL TINY
.CODE
.STARTUP
ENTR MACRO ;define the macro for entering a key
MOV AH,01H
INT 21H
ENDM
When we run the above program , we get the pressed key displayed twice. Why?
Now let’s define and use a macro using parameters.
Example 4-12
.MODEL SMALL
.DATA
MESG1 DB "MY NAME IS ANTONY GONSALVES$"
MESG2 DB "I AM SAMANTHA$"
.CODE
.STARTUP
DISPLAY MACRO STRING ;define a macro for string display
MOV AH,09
LEA DX,STRING
INT 21H
MOV DL,0AH ;DL= ASCII data for newline
MOV AH,02
INT 21H
MOV DL,0DH ;DL=ASCII data for carriage return
MOV AH,02
INT 21H
ENDM
In the above program, the DISPLAY macro is first defined .Then the macro is invoked
twice with two different parameters MESG1 and MESG2. Note that the macro not only
displays a string ,but also has instructions for displaying newline and carriage return .This
ensures that after displaying one line, the cursor moves to the left side of the next line.
Thus in Example 4-12, the two messages are displayed on separate lines.
Example 4-13 shows such a case. Two macros have been defined –STAR and NEW. The
former contains a label AGAIN which has been declared as LOCAL.
This macro causes the character ‘*’ to be printed on a line N times . N is a parameter
whose value is passed to this macro, when called. The macro NEW just moves the
cursor to the left side of the next line. The output of Example 4-13 is as shown below.
*****
****
***
**
*
C:\masm6.14\BIN>
Note: Number format conversions have been discussed in detail in Chapter 0. If you
have doubts regarding such conversions, refer to Section 0.6 .
4.4.1PACKED BCD TO UNPACKED BCD CONVERSION
This has been done in Example 3-24 for a 2 digit packed BCD number. The exact method
of conversion is detailed there along with the example . We will now see how an eight
byte packed BCD is unpacked, converted to ASCII ,and displayed.
In Example 4-14, we see that an eight digit decimal number is represented in packed
BCD such that each decimal digit has a four bit representation. This is stored as a double
word.(four bytes). But when accessing it for processing ,one byte each of this double
word is moved to AL and processed.
Example 4-14
.MODEL SMALL
.DATA
BCD DD 67450823H ;store the 4 byte packed BCD number
ASC DB 8 DUP(?) ;allocate space for 8 byte ASCII number
.CODE
.STARTUP
LEA SI,BCD ;load the offset of BCD in SI
LEA BX,ASC ;load the offset of ASC in BX
MOV CX,4 ;count of BCD bytes
RPT: CALL BCD_TO ;call the conversion procedure
INC SI ; increment pointer
INC BX ; increment pointer
LOOP RPT ;continue until CX=0
;with the above loop, the conversion is
;done and only displaying is left
MOV CX,08 ;count of ASCII bytes
DEC BX ;to point to address of last ASCII digit
STRT: MOV DL,[BX] ;move ASCII value to DL for displaying
MOV AH,02 ;function to display a character
INT 21H
DEC BX ;point to next ASCII digit
LOOP STRT ;repeat until CX=0
.EXIT
Converting from ASCII to packed BCD is just a reverse of all these processes .
4.4.2 BCD CALCULATIONS
i) ADDITION
DAA –Decimal adjust AL after addition
Usage :DAA
For BCD ,none of the digits should have a value greater than 9 ,but we know that a
nibble can contain a value of 0FH .This causes problems when BCD numbers are added.
To sort out this problem ,there is a specific instruction DAA which stands for ‘Decimal
Adjust Accumulator’. This assumes that the sum of the BCD addition is in AL.
The need for this instruction is because direct addition of BCD numbers causes
errors ,and a correction is usually needed .The exact details of this and the correction
done is discussed in Section 0.7.2 .
Now see the following example which adds two multibyte BCD numbers ,to get the
result in BCD itself ..The two BCD numbers are stored as double words (DD)
Note that when they are stored thus, the lowest byte of the number will be in the lowest
address .Now BX and SI point to the BCD numbers to be added. For adding , only one
byte each of the numbers is accessed and added. The sum is stored in the byte location
SUM .Here too ,the LSB of the sum will be in the lowest address.
Example 4-15
.MODEL SMALL
.DATA
BCD1 DD 45679834H ;store the first BCD byte as a double word
BCD2 DD 93870989H ;store the second BCD byte as double word
SUM DB 5 DUP(0) ;allocate 5 byte memory for the sum
.CODE
.STARTUP
MOV AH,0 ;clear AH
CLC ;clear carry flag
LEA BX,BCD1 ;BX to point to first BCD byte
LEA SI,BCD2 ;SI to point to second BCD byte
LEA DI,SUM ;DI to point to SUM location
MOV CX,04 ;count for the number of bytes
RPT: MOV AL,[BX] ;copy the bytes of the first number to AL
MOV DL,[SI] ; copy the bytes of the first number to AL
ADC AL,DL ;add with carry AL and DL
DAA ;do the BCD correction
MOV [DI],AL ;store the result of each byte addition
INC BX ;increment pointer BX
INC SI ; increment pointer SI
INC DI ; increment pointer DI
LOOP RPT ;loop until CX=0
ADC AH,0 ;add carry ,AH and 0;sum in AH
MOV [DI],AH ;store AH in memory
.EXIT
END
On adding 4 BCD bytes ,there is a possibility of a carry ,which corresponds to the 5th
BCD byte of the sum. To preserve this final carry ,AH register is used as shown .The sum
of the given two numbers is 0139559823 .
i) SUBTRACTION
DAS –Decimal adjust AL after subtraction
Usage: DAS
For BCD subtraction ,there is an instruction DAS which operates on the data in AL The
use of this instruction is left to you .
In this case the lower nibble is not within A and F, and the AC flag is not set i.e.AF=0.
Then the instruction just resorts to making the upper nibble of AL to 0.
Thus if the AAA instruction is used after the addition ,
The sum becomes 09 i.e. 0000 1001.To convert it back to ASCII ,OR it with 30H
MOV AL,’5’ ;AL=35H
ADD AL,’4’ ;AL=35H+34H =69H
AAA ;AL=09H
OR AL,30H ;AL=39H,the ASCII value of 9
Case 2
Add ‘7’ and ‘6’ , get the sum in AL
37 H +
36 H
6D H
In this case , the rightmost nibble is greater than 9 i.e. within A and F-
–i.e.it is no longer representing a decimal digit. Hence 6 is added to the rightmost nibble
(like in BCD correction- The number 6 is added because that is the difference between
decimal and hexadecimal ) ,it also clears the upper nibble of AL, sets CF and AF ,and
adds 1 to AH.
Thus if the AAA instruction is used after the addition ,
The sum in AL is now 03 and AH=01 (if AH had been cleared earlier).
When the AAA instruction is used after this ,it makes the lower nibble 0003 and adds 1
to AH and also sets the carry flag.
MOV AL,'7' ;AL=37H
ADD AL,'6' ;AL=37H + 36H =6DH
AAA ;AL=03 ,AH=01 ,CF=1
OR AX,3030H ;AX=3133
The AX register will contain 3133H after this ,which is the ASCII value of 13.
These facts must be taken into consideration when adding multibyte ASCII numbers.
Case 3
Add ‘9’and ‘8’ , get the sum in AL
39H +
38H
71H
.
Here the rightmost nibble is not within A and F ,but AF=1 .
Hence 6 is added to the rightmost nibble and ,it also clears the upper nibble of AL, sets
CF and AF ,and adds 1 to AH.
Thus if the AAA instruction is used after the addition ,AL=7 ,AH=1 ,CF=1 and AF=1
In all cases, AAA clears the upper nibble of AL.
These facts must be taken into consideration when adding multibyte ASCII numbers.
4.5.2 ASCII SUBTRACTION
AAS – ASCII adjust AL after subtraction.
Usage :AAS
This functions similar to the AAA instruction ,After the subtraction of two ASCII
numbers ,AAS checks the lower nibble of AL .If this number has a value between A and
F ,or if the AF flag is set , 6 is subtracted from AL Also 1 is subtracted from AH and AF
and CF are set . In all cases, the upper nibble of AL is cleared.
Case 1
Subtract ‘5’ from ‘8’.
38H –
35 H
03
If AAS is used after this subtraction , the content AL of remains 03 itself .
Case 2
Subtract ‘8’ from ‘5’ .
35H –
38 H
FD H
After AAS ,,AH =FFH ,AL =07 .
Actually the answer should be -3 . but we get
. FF07 ,which is the ten’s complement
-10 +3 =7 .
Let us subtract ‘4’ from ‘15’ .We get the ASCII value of ‘11’ in the AX register with the
following program.
MOV AX,'15' ;AX=3135H
SUB AX,'4' ;AX=3101H
AAS ;AX=3101H
OR AX,3030H ;AX=3131H
Now ,it is up to you to find out what happens when ‘9’ is subtracted from ‘4’.
Also ,note that the instructions AAA and AAS can be used after adding or subtracting
unpacked BCD numbers as well,
. If the result of multiplication (MUL BL) is compared with the result of AAM ,it is
obvious that AAM accomplishes the conversion by dividing AX by 10.Following this, if
ORing with 3030H is done, the ASCII value of the product is available in AX. In the
program, this value is copied to CX, and displayed using the DISP macro. Note that the
most significant digit is displayed first.
Example 4-16
.MODEL TINY
.CODE
.STARTUP
DISP MACRO ;define macro for display
MOV AH,02
INT 21H
ENDM ;end of macro
MOV AL,'6' ;AL=36H
AND AL,0FH ;AL=06H
MOV BL,'9' ;BL=39H
AND BL,0FH ;BL=09H
MUL BL ;AX=0036H;decimal value is 54
AAM ;AX=0504H ,the unpacked BCD of 54
OR AX,3030H ; AX=3534H ,the ASCII value of 54
MOV CX,AX ;copy AX to CX
MOV DL,CH ;DL=CH to display the MSD
DISP ;call macro for display
MOV DL,CL ;DL=CL to display the LSD
DISP ;call macro for display
.EXIT
END
We have seen various number formats so far ,but by now it should be obvious that using
BCD and ASCII numbers for computation is cumbersome, especially when large
numbers are involved.. A more convenient format for all arithmetic calculations is the
binary format ,which is compactly written in hexadecimal. Another point is that, once the
computation is done the natural format for display is the ASCII format. For displaying
,we convert the binary number to unpacked BCD and then to ASCII .When entering data
through the keyboard also ,the ASCII number format is used.
All this implies the necessity of converting binary numbers to ASCII and vice versa.
Let us see how we convert a decimal number( say 6754) to binary form.
6754= 6 x1000 +7 x100 + 5 x 10 + 4 x1
i.e. 4 x1 =4 +
5 x10 =50 +
7 x 100=700 +
6 x 1000 =6000
6754
This is the algorithm to be used for conversion.. Let us write a program which does this.
Here the multiplicands 1,10,100 and 1000 are stored in the data segment. The binary
value also is finally stored in data memory .
Example 4-17
.MODEL SMALL
.DATA
ASCDAT DB '6754' ;ASCII value of 6754
MULTI DW 01,0AH,64H,3E8H
BINAR DW 0
.CODE
.STARTUP
DIG EQU 4 ;DIG specifies the number of digits
MOV CX,DIG ;move into CX the number of digits
MOV DI,DIG-1 ;DI to point to LSD of the ASCII no.
LEA SI,MULTI ;use SI to point to the multiplicands
RPT: MOV AL,ASCDAT[DI] ;bring first ASCII data to AL
AND AX,000FH ;mask AX
MOV BX,WORD PTR[SI] ; get the multiplicand to BX
MUL BX ;multiply AX by BX
ADD BINAR,AX ; add to binary value
DEC DI ; decrement byte pointer
INC SI ; increment word pointer
INC SI ; two increments required
LOOP RPT ;repeat until CX=0
.EXIT
END
Example 4-18
.MODEL TINY
.CODE
.STARTUP
MOV AL,+34
ADD AL,-23
.EXIT
END
For the above program in which +34(22H) and -23(E9H) are decimal numbers the
calculation in binary form is
0010 0010 +
1110 1001
1 0000 1011
Example 4-19
.MODEL TINY
.CODE
.STARTUP
MOV AL,-28 ; AL=E4H
ADD AL,-78 ;AL=B2H
.EXIT
END
In both the above problems ,the overflow flag is found cleared which indicates that the
signed number computation has produced the right result.
Now ,see the next program
Example 4-20
.MODEL TINY
.CODE
.STARTUP
MOV AL,+100
ADD AL,+75
.EXIT
END
In the above case ,the overflow flag will be set because there has been an overflow from
bit D6 to D7 ,and the sign bit is set .Even though two positive numbers have been added
,the sign bit indicates a negative sum . Thus AFH has to be thought of as a negative
number, which is not correct. The problem is that the sum is +175 which is too large to fit
in an 8 bit register. Since the overflow flag is set ,it indicates that the register AL has
overflowed the size allotted for signed operations in 8 bit format.
Understanding that the problem that has caused the error is one of inadequacy of the
size of the destination ,it can be solved by extending the bit size of the operands. This can
be done by sign extending a byte to a word and a word to a double word.
The 8086 has two specific instructions for it.
i) CBW convert byte to word
This instruction works on the data in AL. It copies the sign bit (D7) of AL to all the bits
of AH ,Thus the byte in AL is extended to a word in AX.
ii) CWD -convert word to double word
This instruction works on the data in AX. It copies the sign bit (D15) of AX to all the bits
of DX .Thus DX-AX is the new double word .
Let us now use these instructions to avoid overflow in the case of signed number
operations.
Example 4-21
.MODEL TINY
.CODE
.STARTUP
MOV AL,+100
CBW
ADD AX,+75
.EXIT
END
In this case ,the overflow flag is not set and hence the sum in AX is considered to be
correct. The sign bit also indicates a positive number..
Note: In a general case ,we are not sure if the result of adding/subtracting signed
numbers will exceed the register size allowed for it . To accommodate for a correction as
in Example 4-21 ,the instruction JO (Jump on overflow) can be used . The program can
be written in a way that after the addition or subtraction (as the case may be)operation,
the JO instruction tests the overflow flag and the CBW or CWD instruction is used only
if the overflow flag is found to be set. Then re-computation using the extended word or
double word can be done.
4 JL Jump if Less SF != OF
The list in Table 4.2 shows the conditions of the sign ,overflow and zero flags and
undoubtedly causes a certain amount of confusion. Let us test a program for the various
situations possible and check the condition of these flags.
Example 4-21
.MODEL TINY
.CODE
.STARTUP
MOV AL, +40
CMP AL,-60 ;after this ,OF=0,SF=0
CMP AL,+70 ;after this ,OF=0,SF=1
MOV BL,-20 ;BL=-20
CMP BL,-70 ; after this , OF=0,SF=0
CMP BL,-10 ; after this , OF=0,SF=1
CMP BL,+10 ; after this , OF=0,SF=1
CMP BL,-20 ; after this , OF=0,SF=0,ZF=1
.EXIT
END
The result of each comparison of the above program will help to clear the confusion .
i) When the destination is greater than the source , OF=SF
ii) When the destination is less than the source, OF !=SF
iii) When the destination is equal to the source ,ZF=1
Example 4-22 finds the smallest of three signed numbers and stores it in a memory
location. It compares the first number with the second number .If the first number is less
,it is compared with the third number .Otherwise the second number is compared with the
third number and finally the smallest number is identified and stored in memory .In this
case, the negative number -90 is found to be the smallest number.
Example 4-22
.MODEL SMALL
.DATA
NUMS DB +45,-76,-90
SMALLEST DB 0
.CODE
.STARTUP
LEA BX,NUMS ;use BX as pointer to the numbers
MOV AL,[BX] ;get the first number in AL
INC BX ;increment the pointer
CMP AL,[BX] ;compare the first number with the second
JLE NEXT ;if AL is less than or equal ,jump
MOV AL,[BX] ;otherwise ,the second no is loaded in AL
NEXT: INC BX ;increment the pointer
CMP AL,[BX] ;compare the second no with the third
JLE FINAL ;if the first no is less, jump
MOV AL,[BX] ;move the smallest number to AL
FINAL : MOV SMALLEST,AL ; store the smallest number in memory
.EXIT
END
4.7.2 SIGNED MULTIPLICATION AND DIVISION
There are two instructions to be used for signed multiplication and division .
i) IMUL
All that was discussed about the MUL instruction (Section 3.4.4)holds good in the case of
IMUL too .The only difference is that since the operands are signed, the product also is
signed.. Another point is that when the product is not large enough to use all the registers
allocated for it, the sign bit is copied to the upper register (AH or DX as the case may be)
and CF=OF=0.
ii) IDIV
The signed division operation is similar to unsigned division ,as discussed in Section
3.4.5 ,except that the operands and result are signed .Because of this, if the quotient
register is AL ,the value of the quotient is to be between -128 and +127 .If the quotient
register is to be AX, this value is to be between – 32,768 and +32,767. If this is violated,
a ‘divide overflow’ error message is displayed on the screen.
Now let us use the signed multiply and divide instructions in a program.
Let us compute (XY—Z2 )/ L and store the quotient alone. The values of X, Y ,Z and
L are put in the data segment as shown in the program .
For multiplication ,a procedure named MULT is defined and used.
Example 4-23
.MODEL SMALL
.DATA
X DB -6
Y DB 25
Z DB 19
L DB -9
QUO DB ?
.CODE
.STARTUP
In this program , positive as well as negative numbers are the operands .Let us make a
few observations.
i) With the values of X,Y , Z and L as given in the program , the product is -511
(FE01H) and the quotient is +56 ,which is found in AL as 38H.
ii) If the divisor is now changed to +9 ,the quotient is -56 ,which is found in AL
as C8H .
iii) If the divisor is changed to -3, we get a message divide overflow error and
the program execution is terminated .This is because the quotient (+170) is
too large to fit in AL .Remember that the largest positive number that AL can
hold is +127 only.
This is the same as shift left logical (SHL) .As shifting left does not involve any sign bit
,logical and arithmetic shifting are the same. The two mnemonics(SAL and SHL) mean
the same and can be used interchangeably .
Example 4-24a
.MODEL SMALL
.DATA
FIRST DB "THE VALUE IS WITHIN 0 AND 5$"
SECOND DB "THE VALUE IS WITHIN 6 AND 9$"
OTHER DB "NOT A NUMBER$"
.CODE
.STARTUP
Let us see what exactly these high level language constructs are .Obviously ,they get
converted to assembly language statements .We use the .LISTALL directive to find out.
Example 4-24b shows the listing for the code segment alone up to the .ENDIF statement.
We find the equivalent assembly code for the high level language constructs ,which in
this case are compare and jump instructions.
Example 4-24b
Example 4-25
.MODEL SMALL
.DATA
DAT DB 'ABCDEFGHIJKLMNOPQRSTUVWXYZ$'
MES1 DB 0DH,0AH,'REVERSED STRING: $'
DAT1 DB 27 DUP(?)
.CODE
.STARTUP
MOV BX,OFFSET DAT+25 ;address end of DAT
MOV SI,OFFSET DAT1 ;address DAT1
MOV CX,26 ;load count in CX
.WHILE CX!=0 ;loop while CX!=0
MOV AL,BYTE PTR[BX] ;move byte to AL
MOV BYTE PTR[SI],AL ;save AL in DAT1
INC SI
DEC BX
DEC CX
.ENDW ;end of while loop
MOV BYTE PTR[SI+1],'$' ;append string with $
MOV DX,OFFSET MES1
MOV AH,09
INT 21H
MOV DX,OFFSET DAT1 ;display the reversed string
MOV AH,09
INT 21H
.EXIT
END
For both the above problems ,the high level language constructs are written indented to
the left. This is done only to improve the readability of the program.
The above problem can be worked out using the REPEAT ….UNTIL construct. The
loop is repeated until CX==0 .CX becoming zero is the stopping condition for the loop.
There is also a .UNTILCXZ construct available that can be used in this example .The
.UNTILCXZ instruction uses the CX register as a counter to repeat a loop a fixed number
of times. This example can be rewritten using a .REPEAT .....UNTILCXZ loop also .
Now let us see a case of nested loops. Example 4-26 shows the case of an IF loop nested
within a repeat-until loop. In this , a character is to be searched within a string and the
number of occurances of that character in the string is to be found out. To make the
program inter-active for the user, messages asking for the character and the string are
displayed using DOS INT 21H function 9. Both the character and the string are entered
through the keyboard. The string is terminated when the ‘enter’ key is pressed. This
corresponds to the ‘carriage return’ character 0DH and this is the termination condition
of the repeat ..until loop.
Example 4.26
.MODEL SMALL
.DATA
.CODE
.STARTUP
The output of the Example 4-26 for a character ‘t’ and string entered through the
keyboard is shown below..
Now that we have seen the use of the high level language constructs ,it is obvious that
many of our previous programs can be re-written using these constructs .
xvi) Explain the action of the following instructions-AAM ,AAS,AAD and AAM.
xvii) Write down the steps required for the following conversions.
a. binary to ASCII
b. ASCII to binary
xviii) When is the CWD instruction used?
xix) Is it justified to call MASM A high level assembler?
EXERCISE
1. Write a program to move 100 words from ‘FROM’ to ‘TO’ which are ,two
areas in the data segment separated by a 200 byte space.
2. Write a program to scan the name SAHABUDDIN and replace S by B and B by
H.
3. Store a password in memory .Enter another password through the keyboard and
verify if it matches the stored password. The password entered should not be
displayed .as such, but each letter should be displayed as ‘*’.
4. Write a string program for an application which requires the pointer registers to
be auto decremented.
5. Write a program to search for a word in a block of N words.
CHAPTER 6
OBJECTIVES
In this chapter you will learn
• The differences between 8086 and 8088.
• The pin functions of 8086 in the minimum and maximum modes.
• The use of address latches and data buffers in an 8086 based system.
• The concept of machine cycles and the associated bus timings of 8086.
• The reason for using the bus controller IC is used in a maximum mode system.
• How an 8086 is used in the maximum mode.
• To calculate instruction timing and thus create delay loops.
Upto now, we were dealing exclusively with the programming aspects of 8086 . It is time
now to take a look into the hardware aspects of the processor.
6.1 PIN CONFIGURATION
Now ,we will try to understand the processor in terms of its hardware ,which include the
pin configuration ,functions of the pins , memory bus cycles , connections to other chips
,modes of operations and so on .
Fig 6.1 a) 8088 CPU pin diagram b) 8086 CPU pin diagram
It was mentioned in Chapter 0 that the first PC was built with the 8088 processor. This
processor is in most ways exactly the same as the 8086
The minor differences are as listed
i) 8088 has an external data bus of 8 bits ,while 8086 has a 16 bit data bus.
ii) The instruction queue of 8088 is 4 bytes ,while 8086 has a 6 byte queue.
iii) For 8088, pin NO: 28 (in minimum mode)is an M / IO pin ,while for
8086 ,the polarity of the pin is M / IO .
iv) Pin No 34 is SSO for 8088 , while it is BHE / S 7 for 8086.
The pin diagrams of both these processors are given in Fig 6-1. We see that both these
processors are packaged as 40 pin DIP (dual-in-line package).
It is also seen that some pins have dual functions ,and this corresponds to two modes of
operation –i.e. minimum and maximum modes. The minimum mode is used when the
8086/8088 is used in uniprocessor systems ,and the maximum mode is when the system
is a multi processor system ,in which the 8086/8088 is one of the processors.
Now ,neither 8086 nor 8088 is used in PCs ,because of the advent of the advanced
processors of the same family .However ,we will discuss the hardware aspects of 8086
,because it is the basic processor in the x86 family ,and the features of the advanced
processors are upward extensions of the 8086 features. As such ,understanding the
features of 8086 is very fundamental to the understanding of the advanced processors of
the x86 family.
It was mentioned earlier that 8086 has a 16 bit data bus and a 20 bit address bus .Let us
locate the address and data pins, in the pin diagram. We see a set of 16 pins designated as
AD –i.e.AD0-AD15 .These are the address/data pins .The notation ‘AD’ means that
these pins are used for address as well as data. They are multiplexed for data and address
,which means that at a particular time they carry address ,and at other times they carry
data. The reason for having multiplexed pins was the necessity to reduce the number of
pins of the chip, and this restriction was more stringent in the early days of
microprocessor development..Remember that 8086 has only a total pin count of 40.
But, since the data bus and address bus have to be separate and because the
‘address’ has to be available at all times , the address-data lines have to be ‘de-
multiplexed ’.i.e. separated .This is done by the use of latches ,also availing the timing
features of bus cycles
6.1.2 De-multiplexing the address/data bus
Let us assume that memory is to be read or written to ..Recollect the sequence of
steps for reading and writing memory (Section 0..7) The first step in (say) reading
memory is ‘placing the address on the address bus’. This is ,in practice ,the first part of
the bus cycle . Next, the data is to appear on the data bus , which means that the bus
must be made free to carry data.. For de-multiplexing ,during the time that the address is
on the address /data bus ,it is latched on to a ‘latch IC’ whose clock is the ALE (Address
Latch Enable) signal supplied by 8086 .The signal ALE goes high and functions as a
clock for a latch which is used to save the address values, because the address has to be
available throughout the bus cycle .Once the address is latched on to the latches ,it can
be removed from the AD lines ,and then these lines are free to carry data.
8
L
0 A16/S3 –A19/S6 A
T
8 C
A0-A19
6 H
AD0 –AD15
DATA BUS
D0-D15
Fig 6-3 Block diagram of the de-multiplexed address /data bus
But this accounts for only 16 address lines .8086 has a 20 bit address .In the pin diagram
, pin numbers 35 to 38 also cater to address ,and they have the designations A16 to A19
.But they are also multiplexed with status signals ,and they also have to be de-
multiplexed. Besides these, there is one more pin which carries address information ,and
that is the active low line BHE (Bus High Enable)which is multiplexed by the status
signal S7 . The function of this signal is explained in Chapter 7.Now, with all this
information included, the de-multiplexed block diagram is as shown in Fig 6-3.
1D G 1Q
2D 2Q
3D 3Q
4D 4Q
74LS373
5D 5Q
6D 6Q
7D 7Q
8D OC 8Q
BHE / S 7
BHE
OC A19
A19/S6
A18/S5 A18
A17/S4 ‘373 A17
A16/S3 A16
G
A15
A14
A13
A12
A11
A10
Address Bus
A9
A8
A7
A6
A5
A4
A3
A2
A1
A0
G G
ALE ‘373 ‘373
OC
OC
Fig 6-5 The buffered and de-multiplexed address bus
Fig 6-5 shows the complete diagram of the 8086 ,connected to three latch chips .The
outputs of the latches constitute the address bus.
6.1.4.2 The Data Bus
In Fig 6.5, the data lines D0 -D15 are shown available directly from the pins AD0 to
AD15 . But in practice, these lines don’t have sufficient current driving capability for
many (more than 8 to 10) TTL loads and the capacity to drive capacitive loads .Most
memory devices are MOS chips which have capacitive inputs ,which have to be
charged/discharged when logic level changes occur . If the pin fan out is high ,the
processor cannot supply the necessary current for this .Hence buffers/line drivers which
raise the current driving capability of these pins are used. Moreover , these lines also
have to be isolated when not in use .Hence the buffers have to be tri-state buffers .
The data lines are bidirectional –the direction depending on whether the data is being
read/written. A popular octal bidirectional tri-state buffer is the 74LS245 . For 16 data
lines, two such ICs are needed. Two pins of the of the 8086 act as enable and direction
pins to be connected to the buffer . The DT / R line is high for data write and low for
data read .This pin of 8086 is connected to the DIR (Direction) pin of the buffer .This
fixes up the direction of data transfer .Another pin DEN of the processor is connected to
the Enable line ( G ))of the buffer .Only if this pin is at low level ,will the output lines of
the buffer be enabled ,otherwise the lines are tri-stated ).
A1 G B1
A2 B2
A3 B3
A4 74LS245 B4
A5 B5
A6 B6
A7 B7
A8 DIR B8
Fig 6-7 Pin diagram of the octal tri-state bi-directional buffer 74LS245
The bus cycle timing will ensure the activation of these signals at the appropriate time
for data transfer. We will soon get to the discussion of bus cycles .
M / IO
RD 1A2 1Y2 RD
WR 1A3 1Y3 WR
AD15 A4 B4 D12
AD14 A5 B5 D12
Address bus
AD13 A6 B6 D10
AD12 A7 B7 D9
AD11 A8 B8
D8
AD10 G DIR
AD9
6.1.5 Memory and I/O
The signals RD, WR and M / IO have to be used suitably to get the memory read/write
signals MEMRD and MEMWR and the I/O read/write signals IORD and IOWR . See
the circuit diagram in Fig 6-9. With this, I/O and memory have separate lines for access
.Generation of these control signals may be done with any logic gates .
RD
IORD
MEMRD
o
M / I0
o MEMWR
WR
IOWR
The clock generator IC performs a few more functions than just supplying the clock
frequency to the processor . We will discuss only a few basic functions that are in use
for a minimum mode system. See the connection diagram in Fig 6-11
Note that the crystal is connected between the X1 and X2 pins
X1 X2
8284A
READY
RDY
RESET
RES
CLK
CLK RESET
READY
8086
6.1.8 RESET
An active low RES signal from the control bus is sent to the 8284 which synchronizes it
with the trailing edge of the clock . Most systems include a line that goes to all system
components and possibly controlled by an operator push button (or just after power
on),which causes a low signal on the RES pin of the clock generator . The reset circuitry
of 8284A makes sure that the timing requirements of reset of 8086 are met –i.e. the
RESET line of 8086 must remain high for at least 4 clock periods. The 8086 will
terminate operations on the high-going edge of RESET and will remain dormant as long
as RESET is HIGH. A high on the RESET pin of the processor causes all system
components to be reset ,and inside it, the instruction queue , PSW,DS,SS ,ES and IP are
cleared. CS gets a value of FFFFH and with IP=0000, the first instruction will be
executed from the location FFFF0H . But normally ,this address will be found in a read-
only section of memory , and the code here will be a jump instruction to a program for
loading the operating system .Such a program is called a bootstrap loader.
6.1.9 POWER ON RESET
Fig :6-12 ‘Power On Reset ‘circuit
. When the power is first switched on ,the 8086 should be reset .This is called power on
reset and Fig 6-12 shows the ‘power on reset ‘circuit .Observe the circuitry at the pin
RES .
There is an RC circuit ,and a push button switch too .Assume the push button switch to
be in the open condition now. When the 8086 is first switched on ,power is applied .Then
the capacitor in the circuit starts charging as shown in Fig 6-13 . Thus Vc increases and
the voltage at the RES increases .Inside the 8284 chip ,this RES is the input to a
Schmitt trigger as shown in Fig 6-14 .
Fig 6-13 Charging of the capacitor and generation of reset pulse.
Fig 6-14 The generation of the reset pulse using a Schmitt trigger (inside the 8284
IC)
Till the voltage VC rises to VH, ,the Schmitt trigger output is low .Once it crosses the
value of VH ,it goes low . The high pulse from the Schmitt trigger output is the RESET
pulse to be applied to the 8086 .Note ,in Fig 6-14 ,that it is applied to the D input of a flip
flop and hence it appears at the flip flop output ,in synchronism with the clock .
It is specified that the 8086 reset pulse should be high and should have a period of
at least 4 clock cycles .How is that ensured .? Simply by designing the time constant of
the RC circuit .Let’s try a simple calculation.
The Schmitt trigger switches to low, once its input crosses 1.7V (as per its specifications
).The equation for the charging of the capacitor is
Vc =Vcc[ 1-exp(-t/RC)] .
If Vc is to be at least 1 V ,t=50usecs and Vcc =4.5V ,then RC=188usecs.
So if C=10uF ,then R=18K .
These are close to the values we have used in Fig 6-12 .
By this we are designing a reset pulse of 50 usecs, which is higher than the minimum
requirements of the reset pulse for the clock frequency of 5 MHz shown in Fig 6-12 .
The reset output of the clock generator can be used for resetting other devices in the
system along with the 8086 reset .
Why the push button switch? To start a new reset cycle – if the switch is pressed ,the
capacitor discharges ,and to re-start ,the whole process should start again.
When using a crystal for frequency generation ,the F/ C pin of the 8284 IC, must be
grounded . If this pin is high ,the EF1 pin should be connected to an external oscillator
.and a pin CSYNC is used to provide synchronization in this case ,Since we are using a
crystal in our circuit, the pins F/ C and CSYNC are grounded in Fig 6-12.
6.2 CLOCK
It has been mentioned that all activities on the system bus is synchronized by the system
clock which provides the basic timing . Now ,what are the activities on the bus?
Let us list it out
i) Reading from memory or / IO
ii) Writing to memory /IO
These are obviously two fundamental bus activities .Depending on the type of activity
,different control signals are activated . Any read or write cycle is called a bus cycle .For
8086 ,a bus cycle takes 4 T states, where one T state is defined as the ‘period’ of the
clock .If the clock frequency is 10 MHz ,one T state= 0.1µsecs or 100 nsecs. A bus cycle
is also called a machine cycle .During a machine cycle ,a specific operation –say, reading
or writing is accomplished .So, we can have the following basic machine cycles,
i) Memory Read
ii) Memory Write
iii) I/O Read
iv) I/O Write
There is also the ‘Interrupt acknowledge machine cycle’ ,but that will be discussed in
Chapter 8.
6.2.1 READ MACHINE CYCLE
In chapter 0.2.5,the the steps involved in a typical read machine cycle were listed ,and
let us recollect it.
i) Place on the address bus ,the address of the location whose content is to be
read .This action is performed by the processor.
ii) Assert the read control signal which is part of the control bus
iii) Wait until the content of the addressed location appears on the data bus
iv) Transfer the data on the data bus to the processor
v) De-activate the read control signal .The read operation is over and the address
on the address bus is not relevant anymore.
T1 T2 T3 T4
CLK
M / IO
ALE
DT / R
RD
o o
DEN
READY
i. In T1, the address of the location to be accessed is placed on the lines AD0 to
AD15 andA16/S3 to A19/S6 . BHE is high or low depending on the type of
data(byte/word) to be accessed.
ii. In T1 ,the ALE signal goes high for one clock cycle ,and on its trailing edge ,the
address information is latched and available at the output of the latches.
iii. If it is a ‘memory read’ ,the M / IO signal is high from T1 to T4 .If it is ‘I/O read’
,this signal is low. Also the DT / R is low from T1 to T4 indicating that data is to
be ‘received’ by the processor(since it is a read cycle).
iv. In T2 , the address information is removed from AD0 to AD15 and the bus is tri-
stated(floated). The address information is removed from A16/S3 to A19/S6 and
BHE / S 7 also, and these lines now carry status information.
v. In T2 , the RD signal (which is the READ control signal) is made low . The line
shown to the data bus is indicative of a ‘cause –effect’ relationship –here when
the READ control signal goes low, the output lines of memory come out of the
‘float ‘ state.
vi. For a normal read cycle ,valid data from appears on the data lines after the period
defined as the ‘access time’ of the memory/IO.
vii. In T2 , DEN goes low to enable the data bus buffer outputs .
viii. At the end of T2 ,the READY signal is sampled and if it is high , the bus cycle
proceeds normally .This is the case shown in this diagram.
ix. The clock cycle T3 is to allow ‘access time’ for bringing data from memory/IO
and put it on the data bus. Thus, after the data appears on the data bus ,it is
transferred to the 8086 (to the relevant registers)at the end of T3 .
x. In T4, all the bus signals are de-activated in preparation for the next bus
cycle..This machine cycle ends with T4 , and the next machine cycle is scheduled
to start .
6.2.2 WAIT CYCLES
If the access time for a device is longer than can be permitted by the timing of 8086,
extra clock cycles termed ‘wait states’ have to be inserted in the bus cycle .This is
applicable for reading and writing ..
Fig 6-16 Sampling the READY signal and inserting wait states
The arrangement for this is to sample the READY line at the end of T2 If the READY
signal is found low ,an extra cycle is inserted into the bus cycle ,between T2 and T3,
which is designated as TW . All signals on the bus remain unchanged during this extra
wait state (TW).In the middle of TW , once again the READY signal is checked .If it now
at logic 1, the machine cycle will be T3 -otherwise another wait state will be inserted.
Thus bus cycles can be lengthened to accommodate slower devices in the systems . In
such cases ,a ‘wait state generator’ is connected to the READY pin of the clock
generator to control the READY pin.
.
X1 X2
CLK
8284A CLK 8
CLOCK READY
RES READY 0
GENERATOR RESET
RESET 8
6
RDY
WAIT STATE
GENERATOR
Example 6.1
What is the duration of the bus-cycle in a 8086 based microcomputer, if the clock
frequency is 12 MHz and the three wait states are inserted?
Solution:
The period of a 12 MHz microprocessor is, T = 1/f = 83 ns.
Thus, the duration of the bus-cycle, without any wait-states
is given by; Tbus-cycle = 4* T = 4 * 83 ns = 332 ns.
Duration of the wait-states is, TW= 3* T = 249 ns.
So, extended bus-cycle Tbus-cycle + TW = 581 ns.
T1 T2 T3 T4
CLK
Address/
Status Address Status
Address/
Data Address Data to be written
WR
The word ‘interrupt’ implies that something that is being done is to be temporarily stalled
,so as to take up another activity . This is the case for a processor too .When the
processor is performing some activity ,an external device can ask for attention by placing
a signal on an ‘interrupt request’ line. Then the processor stops whatever it was doing and
attends to the request of the interrupting device When the processor gets an interrupt
request on the INTR line ,it responds by lowering the INTA (Interrupt Acknowledge
line) and enters an Interrupt acknowledge machine cycle.
NMI(Non Maskable Interrupt) is another line on which an external device can place an
interrupt request. .One difference between the two is that, for an interrupt request on the
INTR line to be acknowledged and taken up ,the interrupt flag should be set (IF=1),while
this is not necessary for a request on the INTR line. The details of interrupt processing
will be discussed in Chapter 8 .(We will also discuss why the RESET pin can also be
called an interrupt pin).
6.3.2 DMA
DMA stands for ‘direct memory access’ .So far, we have seen the processor
communicating with memory or I/O i.e. the communication always involved the
processor .Is it possible for data in memory to be sent directly to I/O or vice versa without
involving the processor? For example, we might need to print a large chunk of data which
is in memory .This data can be sent to the output device (printer) from memory. In this
case ,there is no necessity to involve the processor in the data transfer .This is called
‘direct memory access’ . But since the processor is in the system ,it must be isolated
from this process. That means that ,when DMA is to take place ,the connection of the
processor to memory and I/O is blocked –i.e the buses of the processor are to be tri-stated
(in the high impedance state), leaving the path open for data to be transferred directly
between I/O and memory .
MEMORY
S
Y
S
T
8086 E
FLOAT M
B
U
S
I/O
HOLD HLDA
Fig 6-19 Concept of Direct Memory Access
Fig 6-19 shows the typical scenario . Data transfer occurs between the memory and I/O
,in either direction .The processor cannot do any bus related operation ,as its buses are in
the float state . This whole process is managed and controlled by a DMA controller
,which we will discuss in detail in Chapter 11. . At this point, what is needed to be
understood is that when DMA operation is to be done, the DMA controller places a DMA
request on the HOLD pin of 8086.This is essentially a request for permission take
control of the system bus. Since DMA is a high priority service, the processor stops
whatever it is doing and acknowledges the request by raising the HLDA (hold
acknowledge) line and DMA is initiated. Fig 6-20 ..shows the timing associated with the
HOLD and HLDA lines.
DMA
T4 or T1 cycles
CLK
HOLD
HLDA
Hold Execution
Execution State resumes
Suspends
The HOLD pin is sampled at every rising edge of the clock ,and the acknowledge signal
is activated at the end of the current machine cycle –i.e. at T4 .In this state ,the address
,data and control pins of the 8086 are in the high impedance state ,and this continues until
the HOLD request is taken away by the requesting device.
6.3.3 TEST : This pin can be used as an alternative to interrupts. But usually ,this pin is
not used in the minimum mode of operation where 8086 operates in a single processor
environment . In the maximum mode, when an arithmetic coprocessor is also present in
the system , this pin is needed to synchronize the activities of both the processors .The
two processors normally can execute separate instructions ,but if the 8086 needs a result
from the co-processor before it can proceed further ,it executes a WAIT instruction
,which forces the TEST to be high . This is because this pin is connected to the BUSY
pin of the coprocessor ,which is high when the coprocessor is executing instructions Then
the 8086 goes into an idle state where it simply waits until the TEST becomes low (this
happens when the required result is obtained from the processor).After that ,the 8086 can
continue its normal sequence of operations. If the TEST pin is low ,the WAIT
instruction is a NOP for 8086. The communication between the 8086 and the co-
processor is discussed in detail in Chapter 13.
6.3.5 BUS HIGH ENABLE( BHE )
This active low signal is used to activate the higher order memory bank ,the details of
which is discussed in Chapter 7..
Power Supply
VCC GND
Address/data bus
Interrupt
Interface TEST
NMI
ALE
RESET
8086 BHE / S7
M / IO Memory/IO
DMA HOLD DT / R Control
Interface
HLDA RD
VCC WR
DEN
Mode MN / MX READY
Select
CLK
Clock
Now that all the signals in the minimum mode have been discussed ,the minimum mode
configuration can be observed and understood from Fig 6-21 .
6.3.6 HALT MACHINE CYCLE
Another machine cycle is the HALT machine cycle .The processor enters this machine
cycle in response to a HLT (Halt) instruction. In the minimum mode ,the processor issues
an ALE signal with no other control signals .In the maximum mode, the processor signals
cause the bus controller (will be discussed in Section 6.4) to generate the ALE signal
.Since no other signals are generated ,the bus cycles are halted .To bring the processor out
of this state, an interrupt or a RESET signal must be issued
Now let us see how the lost signals of the processor are generated by the bus controller
The bus controller 8288 was designed by Intel to be compatible with the 8086 .
6.4.1 BUS CONTROLLER
In the maximum mode configuration, pin no.s 26 ,27 and 28 are designated as S 0 , S 1
and S 2 .On these pins ,the 8086 outputs a code which specify the type of bus cycle to be
initiated . The listing of the codes , corresponding machine cycles and the important
control signal generated by the 8288 is shown in Table 6-1
The connection diagram between the 8086 and 8288 is shown in Fig 6-21
Note that the clock generator applies the same clock to both the ICs .Thus ,according to
the code on S 0 , S 1 and S 2 ,the appropriate signals are generated and can be used to
read/write memory or I/O , and provide the necessary signals for latches and transceivers
etc .
Note that the bus controller generates the read and write control signals for memory and
I/O ,the ALE signal for the latches ,the DT / R and DEN signals for the transceivers and
the INTA signal for acknowledging interrupts etc
There are a few more pins for the bus controller IC ,but we shall not discuss those
,because such maximum mode systems have become obsolete. But to complete our
understanding of the 8086 ,let us see the rest of the maximum mode pins.
Fig 6-23 :Pin diagram of the bus controller IC 8288
8284
CLOCK
GENERATOR
CLK MRDC
MWTC
CLK 8
S0 2 IORC
S0
8086
S1 8 IOWC
S1
S2
8 DT/ R
S2 DEN
ALE
6.4.1REQUEST/GRANT pins.
Pin numbers 30 and 31 are designated as RQ / GT . These are special pins ,in the sense
that they are bidirectional .As the name implies ,they cater to ‘request’ and ‘grant’. Each
of these pins act similar to the HOLD and HLDA pins .In the minimum mode , HOLD
is for bus request ,and HLDA is for bus grant .In the maximum mode ,one pin performs
both functions .Here the request/grant pins are more likely to be used for communicating
with other processors, than for DMA operations .Remember that this mode is used in
multiprocessor configurations-i.e. systems in which there are other processors besides the
8086. .In multiprocessing systems ,resources like memory and I/O are likely to be shared
and the system bus is common to all processors .When one processor is using the bus
,another processor may request the use of the bus and the current bus master may
relinquish the bus (subject to conditions). Thus ,requests from another processor will be
placed on these lines and the grant signal is also placed on these pins, but in the opposite
direction ,obviously .That is how these pins are ‘bidirectional’.
T4or T1
CLK
RG / GT
.There are timings associated with requests and grants, and are shown in Fig 6-25.
Three pulses are needed to complete a request/grant/release of the bus. The RQ / GT pins
are examined at the rising edge of each clock pulse .If a request (low logic for one T
state) is detected ,and if the conditions are conducive for granting the request, the grant
pulse appears on the same pin immediately following the next T4 or T1 state. This allows
the requesting processor to use the bus .When this processor is ready to return the bus
after use, it sends the release pulse on the same line..Incidentally there are two such pins
and in a maximum mode system , if bus requests are received on both the pins ,the pin
RQ 0 / GT 0 will get the priority. This can happen if there are two other processors in the
system besides the 8086 .
6.4.3 LOCK This is an output signal from the 8086 .If this processor wants to prevent
any other bus masters from accessing the bus, this signal will be asserted low .The 8086
can use an instruction with a LOCK prefix to cause the hardware LOCK signal to be
asserted. For example if the 8086 wants to retain the bus until a string transfer is
completed fully ,it can use the instruction (say) LOCK REP MOVSB. So the processor
does not have to relinquish the bus after one bus cycle ,as may be the case if the LOCK
prefix is not used.
We have discussed all the maximum mode pins of 8086 and the diagram with all the
control signals is shown in Fig 6.26
CLK
.
Vcc GND CLK AEN
Interrupt S0 MRDC
Interface CLK MWTC
S1
INTR S2 8288 IORC
S0 Bus
TEST IOWC
Controller
NMI S1
INTA
RESET S2 DEN
DT / R
ALE
ALE
8086 DT / R
DEN
MIN / MX AD0 - AD15 -
A16/ S3 - A19/S1
LOCK
BHE
RD
READY
QS1 ,QS 0
Local bus
RQ1 / GT1 RQ0 / GT0 control
If for example we fetch an ADD instruction which is a 2-byte instruction, the first 8-bits
specify this operation..The other bits identify the source and destination operands of our
instruction. The ADD instruction may have a memory operand or an immediate operand.
If a memory address or immediate operand is specified, there will be optional bytes
following the instruction which will have to be fetched. There may also be the
requirement of having to calculate the operand address. The BIU will separate the
operation type from the operands and fetch any operands from memory if required, and
after that , execution in done.
In the execution phase,, any instruction involving loading from or storing into memory
,will cause a read or write cycle to occur. An instruction which moves data between two
registers, or must execute a more complicated instruction with operands available within
the processor, is done in the Arithmetic and Logic Unit (ALU) alone.
Now ,let us look at the ’instruction fetch cycle ‘ once again .The instruction is in memory
and fetching it requires a ‘memory read’ cycle which takes 4 T states . But in 8086,
‘fetch and execute ‘are overlapped –i.e .instructions are fetched during the execution
phase of an earlier instruction , and thus the current instruction is normally available in
the instruction queue when it is ready for execution. The result is that, in most cases, the
time normally required to fetch instructions "disappears", because the Execution unit
(EU) executes instructions that have already been pre-fetched by the BIU. Thus the
instruction cycle time does not include the time required to fetch the instruction .But for
instructions which have memory operands not specified directly ,there is a time involved
in calculating the ‘effective address’. Take the case of an addressing mode like ,say,
based indexed mode or relative based indexed mode .The BIU takes time to do the
address calculation ,and this time is also included in the instruction cycle.
Another factor for an extra delay to occur is , if an operand which is a word whose
address is aligned with an odd address has to be accessed ,In this case ,one more memory
read/write cycle may be incurred ,in comparison to a word which is at an even address.(
the mechanics of this is discussed in Chapter 7)..This is not taken into account in the
ensuing calculations .
Now look at some execution times. in Table 6-2. In the first three instructions, all the
operands are register operands ,and the execution time is just the time required to perform
the operation in the ALU. Note that MUL requires quite a lot of time compared to ADD,
because it is a complex instruction involving many steps. The fourth instruction is in the
immediate addressing mode .
In the fifth instruction , there is a memory operand and the execution time is given as
9+EA ,where EA is the time for calculating the effective address .The EA values for
different addressing modes is given in Table 6-3 .
For instruction 6 and 7 ,the execution times are 16/4 and 17/5 .These instructions are
conditional branch instructions .The first figure gives the execution time if the branch is
taken and the second number corresponds to the case when branching is exited. When a
branch is taken ,the instruction queue is to be cleared and reloaded. That is why more
time is expended when a ‘branch’ is taken ,in contrast to the case of execution in the
normal sequence.
In short ,if all such information is provided ,we will be in a position to calculate the
‘instruction cycle’ of an instruction ,and note that the time for ‘instruction fetch ’doe not
need to be included herein The instruction cycle times for each instruction is included
along with the instruction set ,in Appendix ……
Now that we have the means to calculate the time required to execute any instruction of
the processor ,let us use the execution time for an interesting application .We see that a
certain amount of time or rather a ‘delay’ is associated with the execution of an
instruction .Thus instruction execution gives us a means of generating a delay .See the
instructions below.
MOV CX, 100 4 cycles
HERE: LOOP HERE 17/5 cycles
How many clock cycles are necessary for executing the above program segment? The
MOV instruction takes 4 clocks .The LOOP instruction takes 17 cycles for repeating ,and
5 when exiting the loop .Thus totally we have 4+(17 x100) -12 =1692 cycles .( 12 is
subtracted ,because the last execution of the LOOP instruction takes 5 cycles rather than
17 )In a system with a clock of 12 MHz , one clock period is 0.083 µ secs, which makes
for a total delay of 140 µ sec s delay . Also note that most of the delay is incurred in the
repetition of the LOOP instruction .Thus the delay caused by the peripheral instruction
(MOV CX,100 ) may be neglected.
This idea may be used to fix up the value of N so as to get a desired value of delay .To
increase the delay,we can use the NOP (No operation ) instruction which has no operands
and no function except to execute within 3 cycles. The NOP instruction is usually used
to reserve space in programs ,for instructions which may needed to be added later.
Here we use it here to create a delay –any number of NOP instructions may be added.
Example 6-2
Write a program to create a delay of 1 msec.
Solution
As most of the delay occurs within the loop ,the total cycles of delay
is =[(3+17) x N]-12.
Total delay time =1 msec =20N x 0.083 usecs
1m sec
For 1 msec delay ,the value of N = = 602 or 25AH
(20 x0.083µ sec s)
This value of N if inserted into the program will give us a delay of 1 msec. There is a
small % of error in this ,because of having to leave out peripheral instructions ,but for
most situations this can be neglected, as the delay that we get is anyway only
approximate.
The maximum value that can be loaded into a register is FFFFH , and if larger delays
are needed, a nested delay loop can be used as shown below .
Example 6-3
Write a delay loop with appropriate values of the count to get a delay of 1 second .
Solution:
The program is
Instructions Cycles
MOV BX,N1 4
THERE: MOV CX,N2 4
HERE: LOOP HERE 17
DEC BX 2
JNZ THERE 16
The inner loop is that which corresponds to the LOOP instruction .It repeats N2 times
,which is the count in the CX register . The LOOP instruction plus a few overheads(
caused by the instructions MOV CX,N2 and DEC BX ) repeat N1 times ,which is the
count of the outer loop .
.Thus a large delay can be obtained .To calculate N1 and N2 , the best way would be to
make N2=FFFFH and then get a corresponding value for N1 .
For a total delay of 1 second,
Total cycles = N1 x [17x N2 +4+2+16]=N1 [22+17 N2]
If N2 =FFFFH i.e. 65,535 the calculation comes to
1114117 x N1 x .083 =1000 000 usecs
92471.711 xN1 =1000000
N1 =10.81
Thus a value of 11 (0BH) can be used in the place of N1 in the program.
Note : No values are subtracted to take into account the decreased number of cycles when
a loop is exited .Thus ,this calculation is only approximate ,but the error is negligible .
The above program assumes that a program named DELAY_1MS has been written and
used as a procedure along with this . This procedure is like the programs in Examples 6-2
or 6-3 ,with a count which gives a delay of 1 msecs.
If a CRO is connected to any pin of the data bus of the output port ,a continuous square
wave can be observed.
This is an application of software delay ,but in practical cases, we are more likely to use
dedicated timer chips for generating square waves of any frequency ,which is more easy
to program and gives more accurate delays.
In Chapter 5, Example 5-2 gets 20 temperature values from 20 input ports .Usually there
should be a time delay between each measurement ,and a software delay can be used
there .It is only necessary to write a procedure for a specific delay and call this delay
between each measurement.
• 8088 and 8086 processors are similar in most ways, except for three differences.
• There are two modes of operation for the 8086-which are designated as minimum
mode and maximum mode.
• When in the minimum mode, the 8086 functions in a uniprocessor environment.
• The address bus of the processor is multiplexed with the data bus ,and hence to
use these buses separately ,de-multiplexing must be done.
• The address bus ,data bus and control bus must be buffered to increase the driving
capability of the buses.
• An external clock generator is used to provide the clock and to synchronize the
reset and ready signals with the clock
• A machine cycle is a bus activity during which a specific operation is performed.
• Reading and writing of memory and I/O are important bus cycles
• A typical read/write machine cycle of 8086 is 4 T states long.
• Wait states are inserted into machine cycles to accomodate slow memory or
peripherals
• In the minimum mode ,all control signals are obtained from the processor.
• In the maximum mode, an external bus controller IC generates the important
control signals
• In the maximum mode ,there will be other processors in the system
• There are pins on the 8086 on which other processors can request the service of
the system bus.
• It is important to know how many cycles are expended for the execution of each
instruction.
• The delay caused by instruction execution can be used to create delay loops
QUIZ
1. List out the differences between 8086 and 8088.
2. How is the mode of operation of the processor selected and what is the
purpose of each mode?
3. Why are buffers/line drivers used in the address ,data and control lines?
4. What is the specification of the clock to be applied to the CLK pin of the
processor?
5. What is the specification of the RESET signal to be given to the
processor?
6. At what address does the 8086 wake up?
7. Why are RESET and READY signals applied to the clock generator IC?
8. When DT / R is high ,what is indicated?
9. What is the difference between the 74LS244 and 74LS245 ICs?
10. When is the READY pin sampled?
11. What is the difference in the interrupt signals INTR and NMI ?
12. What are the functions of the HOLD and HLDA pins?
13. When is the HOLD pin sampled?
14. How many wait cycles can be inserted in a bus cycle?
15. How can the processor be taken out of the HALT state?
16. What is ALE expected to do? What is the duration of ALE?
17. What is the purpose of the LOCK pin ?
18. Which are the control signals which are not available from the 8086 in the
maximum mode?
19. Why is it that the time for fetching an instruction is not included in the
instruction cycle time of an instruction?
20. What is the use of the NOP instruction?
EXERCISE
1) Draw the de-multiplexed and buffered data bus, address bus and
the read/write control signals with latches and buffers connected.
2) Draw a logic diagram using NAND gates to get the signals IORD ,
IOWR , MEMRD and MEMWR from the three processor signals
M / IO , RD and WR .
4) Redraw the above bus cycle with two wait states included.
5) Explain with relevant timing ,the operation of the RQ / GT pins.
6) In a typical write machine cycle, list out the activities occurring
during each of the four T states.
7) Write a program for getting a delay of 100 msecs.
8) Write a program to generate a square wave at the LSB of the data
bus of an output port with address F767H.
9) In a pressure monitoring environment ,10 pressure sensors have
been connected. The pressure from these sensors have to be read at
intervals of 5 msecs. Write a program to read the sensor values
from input ports having address 0FF0H to 0FF9H
CHAPTER 7
Introduction
In the previous chapter we took a peep into the hardware structure of 8086 and a few
ICs which have to be used along with it. Now we will delve a bit deeper- the focus of this
chapter will be on the way memory and I/O are connected to the processor.
Let us start with memory . Whenever we talked about reading or writing from memory ,
an address was referred to . Assuming that memory is byte organized , it is thought that
every location in memory which can store a byte of data ,has a unique address .The total
address space of 8086 is from 00000 to FFFFFH . How is it that each location gets a
unique address ?
Another issue is that the total address space of 1 MB is not obtained as a single chip of
RAM –also it is not entirely RAM . ROM is also part of memory and a number of RAM
and ROM chips together constitute the total memory i.e. the total address space .
Also, when the processor is used , its address space is partitioned such that certain
address ranges are standardized for certain specific applications .Thus the IBM PC has a
standard memory map –if it is not standardized ,incompatibility issues are likely to arise.
This chapter attempts to find answers to all these doubts and queries.
The assumption here is that memory space and I/O space are disjoint (Refer Chapter 5) –
i.e. the scheme of I/O interfacing used is ‘peripheral or Isolated I/O’. This allows memory
to use the full 1 MB of space for itself.
A0
D0
RAM
DM-1
AN-1
WE CS OE
MEMRD
MEMWR
SELECT
Coming to the other pins of the chips , the active low signals WE is to be connected to the
MEMWR signal from the processor side .Only if this pin is activated ,can the write
operation be done in the addressed location . WE enables the input tri-state buffers of the
data lines of the RAM.
For reading ,the pin MEMRD (from the processor side) is to be connected to the OE
(output enable) pin of the memory chip for reading .When this pin is low, the output
lines are activated ,otherwise they remain tri-stated. Thus OE is meant to enable the
output tri state buffers of the RAM.
Some RAM devices have only a single pin for reading or writing, which is R/ W .This is
fine, as it is obvious that only one of the activities (read or write ) can occur at a time.
The pin CS (chip select) or CE ( chip enable or S (Select)) is the pin which enables the
memory chip .No activity is possible if this pin is inactivated , as the chip remains turned
off . How does this pin get activated? It will be activated only if the address placed on the
address bus of the processor is one of the addresses in the address range of this chip
.When this condition is satisfied, a select pulse is obtained from the ‘address decoder’
output and the memory chip is turned ON for reading or writing. It is during a memory
read or write cycle that the select pulse is obtained i.e. when reading or writing to the
particular chip is required and the address is placed on the address bus .
A ROM chip is similar except that it does not have the WE pin, as it can only be read
from . The ROMs that are used are usually EPROM ,which have a pin to enable the chip
to be programmed .( PGM ).
Why active low control signals?
You might have noticed that most of the control signals discussed here and in the
previous
chapters are active low. This is a TTL concept .The stray capacitances of the control pins
can get charged from noise voltages ,and this may cause the signals to cross the threshold
of ‘high’ level as defined for TTL –thus if the control signals are active high ,it may
cause wrong triggering . On the other hand , an active low signal trigger will happen
only when the line is pulled low deliberately by the controller.
Example 7-1
How many address and data lines are needed for the memory chips with the following
organization?
i) 256 x4
ii) 512 x 8
iii) 1K x16
iv) 32K x 8
v) 128K x 8
Solution
i) 256 x 4 means, it has 4 data lines , and 256 memory locations ,each of width 4
bits.
256 =28 .Thus 8 address lines are needed.
ii) 512 x 8
This chip has 8 data lines .
512 =29 . Thus ,it has 9 address lines.
iii) 1K x 16 .
This chip has 16 data lines .
1K =1024 =210 .It has 10 address lines to access each of the 16 bit data words.
iv) 32K x 8
It has 8 data lines.
32K = 25 x 210 =215 .
It has 15 address lines
ii) 128 K x 8
It has 8 data lines.
128 K = 27 x 210 =217 .
It has 17 address lines .
A0
Address Bus of
A0 - A10
A10 MEMORY
8086
A11 2K x 8
A19
CS
SELECT
o
Example 7-2
i)Find out the range of addresses that the memory chip in Fig 7.2 contains
ii) Repeat the same when the logic of the NAND gate is changed as in Fig 7.3
A19 o
A18 o
A17 o
A16 o
A15 o o SELECT
A14 o
A13 o
A12 0
A11 0
The address lines from A10 to A0 can vary from 000 0000 0000 to 111 1111 1111. The
address lines A19 to A11 must always be 1111 1111 1 for this chip to be selected.
As such ,the lower and upper range of the addresses in the chip are seen as
A1 A1 A1 A1 A1 A1 A1 A1 A1 A1 A A A A A A A A A A
9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Thus the address range of the chip is FF800H to FFFFFH .i.e.2048 bytes i.e. 2K bytes.
If the logic on the NAND gate is modified as in fig 7.3 ,the address range will be
to
Example 7-3
Design an address decoder using OR logic for a 32K x 8 RAM .Find the address space of
this memory chip.
Solution:
Since it is a 32K RAM , it has 15 address lines . Thus the lower 15 lines of the address
bus of the processor is connected to the address lines of the RAM .The upper 5 lines A15
– A19 are used for address decoding . Here the lines are given as input to the OR gate .
When the address on the address bus corresponds to A15 to A19 being all at logic 0 , the
decoder gives the SELECT pulse to the CS pin of the memory chip and thus it is
selected.
32K x 8
A0 D0
ADDRESS BUS RAM DATA BUS
A14 D15
CS
A19 SELECT
A15
+5V
o
A0 A0 PGM A0 A0
EPROM RAM
8Kx8 D0 D0 8Kx8 D0 D0
A12 A12
A12 D7 A12 D7
D7 D7
OE CS OE WE CS
A19 o
A18 o o
A19
A17 o
SELECT
A o
18 A16
o o o
A17 A15
SELECT
o o
A14
A16
A15
A13 o
A14 o
A13
Fig 7-5 a EPROM with address decoder Fig 7-5 b RAM with address
decoder
Solution:
Both the memory chips are of 8K capacity .Hence they have 13 address lines .The
remaining 7 lines of the processor system bus are used for address decoding.
i) In Fig 7-5 a, for the EPROM chip to be selected ,the values of A19 to A13 are
to be
1111 101 .Thus the lowest and highest addresses in the chip are
ii) In Fig 7-5 b, for the RAM chip to be selected , the values of A19 to A13 are
0001 111. Thus the lowest and highest addresses in the chip are
and
0001 1111 1111 1111 1111
Inputs
Enable Select Outputs
*
G1 G2 C B A Y0 Y1 Y2 Y3 Y4 Y5 Y6 Y7
X H X X X H H H H H H H H
L X X X X H H H H H H H H
H L L L L L H H H H H H H
H L L L H H L H H H H H H
H L L H L H H L H H H H H
H L L H H H H H L H H H H
H L H L L H H H H L H H H
H L H L H H H H H H L H H
H L H H L H H H H H H L H
H L H H H H H H H H H H L
Fig 7.6b Decoding Table of 74LS138
Note: G2* =G2A and G2B
The output lines are active low and depending on the selection inputs , one output line
alone will be active . To enable the decoder chip ,it must be ensured that G2 A and
G2 B
are at logic level 0 and G1 is at logic 1. The decoding table of the chip is shown in Fig
7.6b
Consider that a 1K x 8 RAM chip uses a 3 to 8 decoder for memory decoding .The RAM
has 10 address lines .Thus the remaining 10 lines of the processor address bus can be
used for decoding . Let us calculate its address space .
Consider a hardware connection as shown in Fig 7-7
A10
A11
A12
1 1 0
A13
A B C CS
A14 o G2A 7 Y3 o
4
L RAM
A15 S
o G2B
A16 1 1K x 8
3
A17
G1 8
A18
A19
A0-A9
In this ,the address linesA0 to A9 ,from the address bus of the processor are directly
connected to the address lines of RAM .The logic on these lines can vary from 00 0000
0000 to 11 1111 1111 .
The remaining 10 address lines of the processor are used for address decoding .
Thus we see them connected to the pins of the decoder .To enable the decoder ) G 2 A and
G 2 B must be 0,and G1 must be 1. Also ,to enable Y3 , CBA must be 011 .
Thus the logic on the lines from A19 to A10 should be
1111 1111 10 .
Thus the upper and lower range of the address is as shown in the table .
A1 A1 A1 A1 A1 A1 A1 A1 A1 A1 A A A A A A A A A A
9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1
Subtracting FF800H from FFBFFH ,we get 3FFH which is 1023 which means that there
are 1023 +1 addresses on this chip..
Thus there are 1024 (1K)byte locations in the chip.
Example 7.5
The figure shows a RAM and a ROM connected to an address decoder ,which is a 3 to 8
decoder.
Find the address space of each of these chips.
CS
SELECT
A19 G1 Y1 o
G2A
G2B Y4 o
A0 – A15 RAM
64K x 8
SELECT
CS
Fig 7.8 Address decoding of a RAM and ROM using a 3 to 8 decoder
Solution: Only 4 lines of the address bus are to be used for address decoding, as the
remaining 16 lines are to be connected to each of the 64K memory chips .
For the ROM ,A19 to A16 are to be 1001 ( A19 =1,CBA=001).
.Thus the address range of this chip is
90000H to 9FFFFH. Similarly for the RAM , A19 to A16 are to be 1100
(A`9=1,CBA=100) . Thus the address range of this chip is
C0000H to CFFFFH.
A0 -A13
16K x 8 16K x 8
RAM ROM
CS CS
o
A14
Fig 7.8 Partial address decoding
See Fig 7.8. Here two 16 KB memory chips are decoded using just the condition of the
A14 line .For the RAM ,A14 should be 0 and for ROM A14 should be 1 for the respective
chips to be enabled .The address lines A15 to A19 are don’t cares ,which means that each
of these chips can be accessed using a number of different addresses which will map to
the same physical location. In effect ,this causes ‘foldback memory ‘ or ‘multiple-mapped
memory’ to exist . Suppose we try to read from the lowest RAM location .We use the
physical address 00000 .But the same data can be read using an address F8000H or
E0000H .In fact 32 different addresses can be used to read this data .This is because 5
bits of the address i.e. A15 to A19 ,are don’t cares ,232 =32 .
Thus this is the amount of foldback memory that exists.
Does this cause any problem? Not really ,but if the memory of the system needs to be
expanded ,the hardware will have to be modified .As a rule ,partial address decoding is
perfectly safe for a small system which does not need any memory expansion.
Example 7.6
Draw a decoding circuit using partial decoding for a RAM and EPROM each of size 8K
x 8 .For decoding use only the address lines A13 and A14 . What is the size of its
foldback memory?
A0 -A12
8K x 8 8K x 8
RAM EPROM
CS CS
A14 o o
A13 o
Fig 7.9 Partial address decoding using two address lines
Solution:
For the RAM ,it is mandatory to have only the address lines A13 and A14 to be 00. The
addresses can vary as shown below. Since 5 address lines are don’t cares ,there are 32
different addresses with which each location can be accessed.
A1 A1 A1 A1 A1 A1 A1 A1 A1 A1 A A A A A A A A A A
9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
X X X X X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
X X X X X 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
For the EPROM ,it is only mandatory to have the address lines A13 and A14 to be 11.
The addresses can vary as shown below. Since 5 address lines are don’t cares ,there are
32 different addresses with which each location can be accessed.
A1 A1 A1 A1 A1 A1 A1 A1 A1 A1 A A A A A A A A A A
9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
X X X X X 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
X X X X X 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
MEMORY-1 MEMORY-2
Address Bus
Address Bus
D0 –D7 D8 –D15
Fig 7.10: 16 bit memory realized using two 8 bit memory chips
We know that for a word transfer , two byte locations must be accessed –i.e. two
addresses are actually needed
Keeping these concepts in mind, let us see how memory is organized for 8086 .A 16 bit
data can be obtained by accessing two memory chips in parallel each having 8 bits each
of the word. See Fig 7.10 . This is the way memory is organized in 8086 .A 16 bit word is
obtained as the concatenation of two bytes in two ‘memory banks’. i..e. memory is
organized as two banks –one, the upper bank or high bank with the data corresponding to
the upper byte D8-D15 , and the other ,the lower bank or low bank which has the data
lines D0-D7. The upper bank is also called the odd memory bank,because it has the odd
addresses mapped to it –in the same way ,the lower bank is called the even bank .Let’s
how this works out .
00003 00002
00001 00000
See Fig 7.11 .Remember that a 16 bit word has one byte with an odd address and one
byte with an even address .If a byte alone is to be accessed ,it may be in the odd bank or
even bank .Thus for reading /writing a byte ,only one of the banks is to be accessed
,but to access a word ,both memory banks have to be accessed .In the second case, the
byte in the given logical address and the byte in the next address are accessed .For
instance ,if the instruction is MOV AX ,[0002] the logical addresses involved are 0002
and 0003 –one byte in the even bank ,and one byte in the odd bank . For an instruction
MOV AX,[0001] the addresses are 0001 and 0002 –again ,both banks have to be
accessed to get the complete word.
How are the two banks differentiated.? An even address implies that the LSB of its
address i.e. A0 is low . Thus , A0 can be used to enable the even bank .To enable the high
(odd) bank ,8086 generates a signal BHE (Bus High Enable)which goes low
whenever the addressed data is in the high (odd) bank.
In Fig 7.12, see how these two signals are used along with the address decoder output ,to
enable the two banks independently . BHE is enabled if and only if the address on the
address bus is for accessing a byte in the high bank. That is why it is designated ‘BUS
HIGH ‘ enable.
A0 is frequently called ( BLE} (Bus Low Enable)because it used to enable the low bank.
.
Fig No 7.12 ,shows a case when two 32 K x 8 RAMs form the16 bit memory. . Thus
the total memory is 64K x 8 bytes. A 32 K RAM needs 15 address lines . Of the 20
address bits from the address bus of the processor , A0 is used to enable the even bank..
A1 to A15 are connected to the address pins A0 to A14 of each RAM chip. The rest of
the address lines are used for address decoding . A0 and BHE are used for selecting
each of the banks separately
A0
BHE
A16-A19
Address
Decoder
CS CS
RAM RAM
32K x 8 32K x 8
Address Bus A1-A15
LOW HIGH
(EVEN) (ODD)
BANK BANK
D0 –D7 D8 –15
Fig 7.12 Memory banks with the respective bank enable signals ,along with the
decoder
This is why it was mentioned (Section 6.6) that for accessing a word operand from an
odd location ,an extra machine cycle machine time has to be added to the instruction
cycle time. Thus it is advantageous (in terms of speed) to align all words at even
addresses. Many optimized assemblers do this automatically .Otherwise, there is a
directive named EVEN which does this.
Take the case when the data segment has 7 bytes and a word .The bytes are stored in
location 0000 to 0006 .Obviously ,the word will be aligned at the next address, which is
odd. If the EVEN directive is used ,the location counter is incremented by 1 ,and the
word is stored at location 0008 .This is illustrated in the following data segment
definition.
.DATA
NUMS DB 7 DUP(05)
EVEN ;increments location counter to 0008
WDR DW 2345H
But this obviously wastes one byte space, and memory is fragmented But this situation
cannot be avoided if speed is to be optimized.
7.3.2 WHY MEMORY BANKS ?
Thus we see that arranging memory to be in two memory banks causes an additional
burden. Then why is the memory system designed this way ?
What is being attempted is that ,when a byte alone is to be accessed ,only the bank in
which that byte resides , should be enabled –the other bank should remain disabled .
What is the problem if both the banks are enabled for any and every access?
The answer is that there won’t be any problem in case of a read operation , but it may
cause havoc for a write operation .See the following cases.
MOV AL,[0000]
Note the above instruction which initiates a read cycle. .It is the case of reading a byte
.Only the even (low) bank need be enabled to read .But even if the high bank is also
enabled ,it doesn’t matter .The data present on the lines D8 to D15 will not be used by
the processor .It takes into AL , only the data on lines D0 to D7 and ignores the data on the
upper bus.
BHE HWR
MEMWR
A 0 ( BLE} LWR
Fig 7.13 Using separate write strobes
We can use the logic shown in Fig7.13 . When there is the necessity to write a byte to
the high order (upper)bank , only the HWR (High write ) signal will be enabled –
similarly for writing a byte to the lower bank ,only that bank will be enabled ,but for
writing a word , both banks get enabled . Only the write control signal is taken into
consideration- reading unwanted data is not a problem as the processor takes from the
data bus ,only the byte that it needs.
In this scheme, the LWR and HWR signals are applied to the WE pins of the
respective memory banks only . Separate bank select signals from the address decoding
circuitry is not used. .
A0000H to 128K VDR Video display RAM with portions allocated for
BFFFFH video buffers and other video applications
C0000H to 192K ROM Memory expansion area for ROM
EFFFFH
F0000H to 64K ROM BIOS ROM , hard disk and other peripherals’
FFFFFH ROM
Input Ports
8 Data in
0
8 Data bus
Data out
6
Output Ports
FFFFH
FFH
Fixed port addressing
0000 00
Fig 7.15 Schemes of port addressing
Output
Device
Data Bus
IOW System
Data Bus
Example 7.5
Design a system with 8LEDs connected to the lower 8 bits of the data bus of the 8086
data bus.
The LEDs must switch ON and OFF with a delay of 1 second between each switching
action.
Solution:
Fig 7-17 shows the hardware setup .
Q0
7
D0 4
D0 L
Data Bus of 8086 S 8 LEDs
D7 3
D7 7
3 R
G Q7
OC
A0 o
A1 o
A2 o
A3 Select
A4
A5
A6
A7
o
IOW
i) The diagram with the address decoder ,the select pulse and the IOW signal is
shown .The address decoder gives a high select pulse . This is ANDed with
the inverted IOW pulse, and applied to the G pin of the octal transparent
latch 74LS373 (Refer Section 6.1.4).The G pin needs a high level pulse to
latch the data on its D inputs to its Q outputs . OC must be strapped to ground
to enable the output pins of the latch.
ii) Current limiting resistors are connected to the cathodes of the LEDs
iii) The address decoder specifies an 8 bit address of F8H for the port.
iv) The program for switching the LEDs ON and OFF is
Input ports use the IN instruction to give data to the processor which is received in the
accumulator of the processor (AL or AX) . An input port requires a tri-state buffer along
with it to ensure that is isolated from the bus when it is not selected. A simple switch can
be an input port , and the alphanumeric keyboard used in PCs is also an input port.
Fig 7.18 shows a basic input port .
System Data
Bus
SELECT TSB
Address E
Address Bus Decoder
Input
Device
IOR
ii) To read from the input device ,the IORD signal must be active
iii) When the above two conditions are true, the corresponding tri-state buffer gets
enabled and data is transferred to the processor.
Taking into consideration all these requirements ,let us design a system with two sets of
switches acting as two input ports. The tri-state buffer 74LS244 is used. This has two
active low enable inputs 1OE and 2OE .When both of them are low ,data at the A inputs
appear at the at the Y outputs. But if the enable pins are high ,the output is tri-stated.
1OE 1 20 VCC
1A0 2 19 2OE
2Y0 3 18 1Y0
1A1 4 17 2A0
2Y1 5 16 1Y1
74LS244
1A2 6 15 2A1
2Y2 7 14 1Y2
1A3 8 13 2A2
2Y3 9 12 1Y3
10 11 2A3
GND
Fig 7.19 Functional pin diagram of the octal tri-state buffer 74LS244
Example 7.6
Find the addresses of the two input devices shown in the figure and write a program to
read data from each of these ports and move it to some other registers.
Solution:
i) Two sets of 8 switches S0 to S7 are connected to the data bus through two
tri-statebuse and this setup functions as two input ports .
ii) The address of the ports are 8 bit ,as only address lines A0 to A7 have been
considered for decoding ,The address of the first port is 7EH and that of the
second is 3EH.
iii) The address on the address bus decides which of the input ports get selected .
The tri-state buffer ensures that the port which is not selected is isolated from the bus.
iv) The program for inputting data from each of these ports is
Thus after reading both ports , the switch settings are available in BL and CL.
This data can be used for processing and/or display
TSB-1
S0
7 Y0
4 D0-D7
Y7
L
S
2
S7 4
4
Port-1
o 1OE 2OE
SELECT
o
IOR
o
TSB-2
S0 Y0
7
4
Y7
L
S
2
S7 4
Port-2 4
1OE 2OE
A0 o
A1
A2 SELECT
A3 o
A4
A5
A6 o IOR
A7 o
Fig 7.20 Two sets of switches acting as two 8 bit input ports
0005
0003 0004
0001 0002
D15 D8 D7 D0
Most of the I/O ports used for PCs have 8 bits wide data , but there are 16 bit wide ports
like video and analog to digital conversion ports etc . If a port is .by definition , a 16 bit
wide port ,use of separate bank select signals are not needed-the port can be selected
directly by the address decoding and the I/O read/write signals. That is because data is
always read or written as a word, and byte access is not necessary.
QUIZ
1. How many address lines does a 256K memory chip need?
2. Name the important pins in a typical RAM and explain the function of
each.
3. Find the numbers of two standard ROM chips and two EPROM chips ,
along with the way the chips are organized(how many address and data
lines).
4. Why are active low signals preferred for use as control signals?
5. What is meant by the term ‘address decoding’ with respect to memory as
well as I/O devices?
6. Why is memory organized in ‘banks’?
7. Why is the address line A0 also designated as BLE ?
8. Why are separate write strobes used for memory banks ,but not read
strobes?
9. Whenever LEDs are connected to the data output lines ,it is preceded by
latches .Why?
10. What is the range of address space for I/O in an 8086 based system?
EXERCISE
1. Design an address decoder for two RAM chips and two ROM chips each
organized as
1K x 8 chips. Use gates for the decoding circuitry and specify the address range
of each chip.
2. Repeat the above problem using the chip 74LS159 (2 to 4 decoder) as address
decoders.
3. Using a 3 x 8 decoder , design the decoding setup for eight RAM chips each of
size
8Kx 8 .Indicate the address space of each chip.
4. For the following (Fig 7.22a and b)decoding circuitry , find the address range of
the
memory addresses .
A19 O
A18
A17
A16 O
A15
A14 SELECT
A13
Fig 7-22 a
A19 O
A18 O
O
A17
A16 O
SELECT
A15 O
A14 O
Fig 7-22 b
5. .Design a memory system using partial address decoding for 4 RAM chips of size
1K x 8 each .
6. Draw a diagram using memory banks for a 16 K x 16 memory . Draw the decoding
circuitry ,and use write strobes for enabling the WE pins of the banks .
7. Design a I/O system in which there are two switches connected as an input port .
The status of these switches are to be read and fed to corresponding LEDs connected
at
the output side. Draw the hardware diagram for this and write a program to do this .
The address of the ports should be designed .
8. .Design an I/O system in which an input counter counts from 0 to 9 ,then goes back to
0
and then repeats .this sequence.. This count has to be displayed on a seven segment
LED.
Design the setup with input and output ports.
CHAPTER 8
Introduction
The word ‘interrupt’ brings to our mind many situations .It could be that we are at
home doing something rather important and interesting .and suddenly the doorbell rings .
There is no option but to go to the door and attend to the person who has interrupted us –
it may be that the visitor needs only a few minutes of our time –but it may also happen
that the visitor needs us to go out and do some errand for him .In either case ,the task we
were at ,is suspended temporarily We spend some time attending to the task requested by
the visitor after which we get back to the suspended task . This is a real life situation, and
if you think it over, it happens quite frequently –which means that ,being social beings
,none of us ever gets to do any of our tasks uninterruptedly
.
This is exactly the case with the processor too . Since the processor is connected to
various peripherals and also because it has various programs stored in its memory ,there
is very little chance of it being able to perform a single task without interruption .
Besides, performing a task only when ‘requested’ of it is actually the most efficient way
of using the capabilities of the processor.
Let us look at it this way . The keyboard is a peripheral connected to the processor –this
means that the processor expects a key to be pressed at any time .One option is for the
processor to do ‘polling’ .i.e. the processor continually keeps checking if a key has been
pressed .When a key press is detected ,the processor goes on to the task of identifying
the key . But if the processor is ‘polling ‘ for a key press ,it is obviously waiting and
doing nothing else . This is just a waste of processor time .A better way of organizing the
setup would be to let the processor do some task, and ‘interrupt’ it only when a key is
pressed . When a ‘key press’ interrupt is received, the processor performs the ‘interrupt
service routine’(ISR) which is the process of identifying the key that was pressed . This,
in essence ,is the philosophy of interrupts-and it helps utilize the processor with
maximum efficiency .
8.1 INTERRUPTS OF 8086: Now that we have accepted that interrupts are a part of
the normal working of a processor, let us see the interrupt processing mechanism of
8086.
8086 has hardware interrupts ,software interrupts and error generated interrupts .For the
three cases mentioned here ,the interrupting mechanism is different ,but the way the
processor responds is similar .So let us first have a look at the way 8086 responds to any
interrupt .The point to note is that ,after an interrupt request is processed, the processor
has to come back to its previous task which was left unfinished .Thus , an interrupt is not
very different from a CALL instruction in its philosophy ,but there are some differences
in the way it is handled and processed.
8.1.1 INTERRUPT RESPONSE OF 8086
After every instruction cycle ,the processor checks if any interrupt is awaiting service. If
it finds an interrupt request, and decide to acknowledge and service it , the response is
the following sequence of steps.
i) The flag register is pushed on to the stack.
ii) The interrupt flag is disabled (IF=0).
iii) The trap flag is disabled (TF=0).
iv) The CS register is pushed on to the stack.
v) The IP register is pushed on to the stack.
vi) Control is transferred to the location in which the corresponding ‘Interrupt
Service Routine’(ISR) is stored. This in effect ,would be a far jump.
vii) The program corresponding to the ISR is executed .The last instruction in the
ISR will be IRET .
viii) Then IP is popped off the stack.
ix) CS is popped off the stack.
x) The Flag register is popped off the stack.
xi) Control returns to the point at which it had left off.
Steps i to vi constitute the actions before getting to the interrupt service routine ,and
steps viii to xi occur on returning from the interrupt.
MAIN PROGRAM PUSHF
INT CLEAR IF ISR
CLEAR TF
PUSH CS
PUSH IP
CET ISR
POP IP
POP CS
POPF IRET
000B
CS
0008 IP INT 2 VECTOR
CS
0004 IP INT 1 VECTOR
CS
0000 IP INT 0 VECTOR
CS HIGH
CS LOW
IP HIGH
IP LOW
byte
Each interrupt vector is numbered from 0 to 255 ,and as such , each interrupt is also
numbered in the same way .These numbers thus turn out to be what can be called as the
‘type numbers’ of the interrupts. Thus there are interrupts designated as Int 0, Int 1,Int
2……Int 255 . As is obvious from Fig 8-2a, these numbers show the position of the
corresponding interrupt vector in the interrupt vector table . Thus the vector of INT 0 ,is
the 1st entry in the IVT , that of INT 1 is the second entry … that of INT 6 is the 7th
entry and so on. Since each interrupt vector is 4 bytes long ,the memory location
corresponding to the vector of INT n is obtained as n x 4.i.e. for INT 1 ,the address of
the first byte of its vector is 1x4 =0004 ,for INT 0 it is 0000 etc as can be verified from
Fig 8.2a. In fact ,this is how the 8086 reacts to an interrupt with a type number n. It
multiplies the type number by 4 ,gets the address of its interrupt vector ,loads IP
and CS with these new values and starts executing the ISR it has thus located .
Example 8-1
Find the address (in the IVT) of the interrupt vector of INT 61H . Find the physical
address of the ISR corresponding to this interrupt if the vector is 0F00:9872 .
Solution:
The type number of the interrupt is 61H =97 in decimal.
The address of the interrupt vector is 97 x 4 =388 =184H.
Thus the interrupt vector is to be stored in the IVT in location 0000:0184 onwards.
For the ISR the CS value is 0F00H and the IP value is 9872H . The
part of the interrupt vector table which has these vectors stored is shown.
0FH
0186H 00
98H
0184H 72H
Address
Now ,how does the interrupt vector table get these entries?
It was mentioned in Chapter 6 that after’ power on’ ,the processor wakes up at the
address FFFF0H .This is the entry point to BIOS in ROM.(Ref Table 7.3)
BIOS contains a set of routines that provide device support. These routines check
,verify and initialize various devices ,and also establish two data areas, one of which is
the interrupt vector table from locations 00000 to 003FEH.
How is an interrupt different from a ‘call’?
An interrupt seems to perform actions similar to a far CALL instruction .It differs in a
few points ,however. An interrupt causes the flag register to be pushed on the stack in
addition to CS and IP values . It clears the trap and interrupt flags .The action of clearing
the flags is not associated with a CALL ,but is part of the automatic response due to an
interrupt. Also ,note that the ISR ends with IRET rather than a RET instruction. The
IRET instruction ensures that the flag register is also popped off the stack ,rather than just
the CS and IP.
We have used software interrupts in the form of the DOS function INT 21H . DOS has a
number of functions to access input/output devices, and we can use these functions by
knowing the interrupt type number .The absolute address of the function need not be
known to us .
Besides DOS interrupts, there is another set of functions for I/O access and this is
supplied by the system BIOS. BIOS stands for ‘Basic Input output System ’.A collection
of routines are available here too ,to access I/O .These functions are accessed by
interrupts with type numbers specified for a particular peripheral .Refer Table 8.2 .
Thus DOS and BIOS interrupts belong to the class of software interrupts and will be
discussed in greater detail soon.
T1 T2 T3 T4 T1 T2 T3 T4
ALE
INTA
FLOAT
AD0 –AD15 Type No.
Now let us see the sequence of actions following an interrupt request on this line.
On being interrupted ,the processor goes into an ‘interrupt acknowledge’ state which
causes two ‘interrupt acknowledge machine’ cycles to occur.
During the first machine cycle, the address /data buses are floated and the INTA
(interrupt acknowledge) pin goes low in T2 and remains low till T4 . This is an indication
for the interrupting device that its interrupt request has been accepted by the processor .In
the second machine cycle, once again the line INTA is low from T2 to T4 ,and then the
interrupting device places the ‘type number’ of the interrupt on the lower data lines (D0 to
D7 ),which comes out of the float condition. The processor accepts this number
,multiplies it by 4 ,and accesses the interrupt vector table at that location.
In Fig 8.3, the ALE signal is also shown .Actually ,this signal has no role in the
INTA cycle, but is included just to show that in the first T state of any machine cycle ,
the ALE signal is activated.
D00
D 1
7
8 D11
D 0
4 0
0 D22
D
D L 0
8 D33
D S 0
6 D44 0
D5 2
D65
D 0
4
D76
D 4 1
OE OE
INTA
Fig 8.4 shows a simple circuit which is capable of sending a ‘type number ‘ to the
processor .Consider an interrupting device interrupting the 8086 on its INTR pin. The
processor responds by lowering the INTA line. This signal is connected to the enable
pins of a tristate buffer (74LS244). Eight switches are connected to the input pins of the
TSB .When a switch is closed ,the line is grounded corresponding to a ‘0’ level on the
pin. When a switch is open ,it gets Vcc on it and hence is in the ‘1’ state .In the figure
,the switch configuration corresponds to 1000 0001 or 81H. This is because the first and
last switches are open ,and the rest are closed .When the enable pins (OE) of the TSB are
low (i.e. when INTA becomes low),the data at the input of the TSB is transferred to the
output of the TSB chip.
During the first Interrupt acknowledge machine cycle ,the address /data lines of the
processor are floated . This ‘interrupt acknowledge ‘machine cycle signals the
interrupting device that its interrupt request has been acknowledged ,and that it is to get
ready .In this cycle even though the type number appears on the output of the TSB , the
bus of the 8086 is in the high impedance state and so nothing happens. But during the
second machine cycle ,when the INTA line is lowered ,the lines D0 to D7 of the processor
are enabled , and the switch settings of the TSB are transferred to the data lines D0 to D7
of the 8086 ,constituting the type number . Thus it is understood that this INTR interrupt
is now vectored to type 81H.
Thus INTR is an interrupt pin with which an interrupt can be funneled to any location in
the IVT and thus facilitates flexibility for use in any application . It is also possible that
more than one device can place interrupt requests on this line ,and each of them may
have to be associated with different type numbers. All these necessitate the need of extra
hardware to manage the possible interrupts arriving on this pin from various peripherals
.This is done by a peripheral chip named Programmble Interrupt Controller (PIC ) 8259
,the details of which will be presented in Chapter 10.
The INTR pin along with the PIC is used in PCs for managing various hardware devices.
C:\>debug
-dffff:0000
FFFF:0000 EA 5B E0 00 F0 30 36 2F-32 30 2F 30 35 00 FC 00 .[...06/20/05...
The first 5 bytes constitute the assembly code for the jump instruction i.e. JMP (EA)
F000:E05B. The next eight bytes were originally called the "RELEASE MARKER" by
IBM, as they always contain the release date of the code (which is 20th June 2005 for this
PC). See the ASCII code of this marker
30 36 2F 32 30 2F 30 35 i.e. 06/20/05.
1. Why are the interrupt and trap flags cleared as part of the interrupt response?
2. How many hardware and software interrupts can the 8086 support?
3. Explain the terms ‘Interrupt service routine’ and interrupt vector.
4. The hardware interrupt INTR is called a non –vectored interrupt .Why?
5. List the interrupts reserved by Intel.
6. Explain the sequence of actions that occur in an interrupt acknowledge cycle.
7. What happens if a number of interrupts occur at the same time?
8. DOS interrupts be used in TSR programs?
EXERCISE
1. Find the address in the IVT for the interrupts of types 102H and 200H.