Microproccessor NOTES
Microproccessor NOTES
Microproccessor NOTES
15CS44
MODULE – 2
A AND L INSTRUCTIONS & INT 21H AND INT 10H PROGRAMMING
ARITHMETIC & LOGIC INSTRUCTIONS AND PROGRAMS
INTRUCTIONS SET DESCRIPTION:
UNSIGNED ADDITION AND SUBTRACTION:
Unsigned numbers are defined as data in which all the bits are used to represent data and no bits are set
aside for the positive or negative sign. This means that the operand can be between 00 and FFH (0 to 255
decimal) for 8-bit data, and between 0000 and FFFFH (0 to 65535 decimal) for 16-bit data.
The instructions ADD and ADC are used to add two operands. The destination operand can be a
register or in memory. The source operand can be a register, in memory, or immediate.
Remember that memory-to-memory operations are never allowed in x86 Assembly language.
The instruction could change any of the ZF, SF, AF, CF, or PF bits of the flag register, depending
on the operands involved. The overflow flag is used only in signed number operations.
1
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
With addition, two cases will be discussed:
CASE1: Addition of Individual Byte and Word Data:
Program 3-1a
These numbers are converted to hex by the assembler as follows: 125 = 7DH, 235 = 0EBH, 197 = 0C5H,
91 = 5BH, 48 = 30H. This program uses AH to accumulate carries as the operands are added to AL
register. Three iterations of the loop are shown below:
1. In the first iteration of the loop, 7DH is added to AL with CF = 0 and AH = 00. CX = 04 and ZF
= 0.
2. In the second iteration of the loop, EBH is added to AL, which results in AL = 68H and CF = 1.
Since a carry occurred, AH is incremented. CX = 03 and ZF = 0.
3. In the third iteration, C5H is added to AL, which makes AL = 2DH. Again a carry occurred, so
AH is incremented again. CX = 02 and ZF = 0.
This process continues until CX = 00 and the zero flag becomes 1, which will cause JNZ to fall through.
Then the result will be saved in the word-sized memory set aside in the data segment.
2
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Although this program works correctly, due to pipelining it is strongly recommended that the
following lines of the program be replaced:
The instruction "JNC OVER" has to empty the queue of pipelined instructions and fetch the instructions
from the OVER target every time the carry is zero (CF = 0). Hence, the "ADC AH, 00" instruction is
much more efficient.
The addition of many word operands works the same way. Register AX (or CX, DX, or BX) could be
used as the accumulator and BX (or any general-purpose 16-bit register) for keeping the carries. Program
3-1b is the same as Program 3-1a, rewritten for word addition.
Program 3-1b
3
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
CASE2: Addition of Multiword Numbers:
Program 3-2
o Assume, a program is needed that will add the total Indian budget for the last 100 years or the
mass of all the planets in the solar system.
o In cases like this, the numbers being added could be up to 8 bytes wide or even more. Since
registers are only 16 bits wide (2 bytes), it is the job of the programmer to write the code to break
down these large numbers into smaller chunks to be processed by the CPU.
o If a 16-bit register is used and the operand is 8 bytes wide, that would take a total of four
iterations. However, if an 8-bit register is used, the same operands would require eight iterations.
In writing this program, the first thing to be decided was the directive used for coding the data in
the data segment. DQ was chosen since it can represent data as large as 8 bytes wide.
In the addition of multibyte (or multiword) numbers, the ADC instruction is always used since the
carry must be added to the next-higher byte (or word) in the next iteration. Before executing
4
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
ADC, the carry flag must be cleared (CF = 0) so that in the first iteration, the carry would not be
added. Clearing the carry flag is achieved by the CLC (clear carry) instruction.
Three pointers have been used: SI for DATA1, DI for DATA2, and BX for DATA3 where the
result is saved.
There is a new instruction in that program, "LOOP xxxx", which replaces the often used "DEC
CX" and "JNZ xxxx".
When "LOOP xxxx" is executed, CX is decremented automatically, and if CX is not 0, the microprocessor
will jump to target address xxxx. If CX is 0, the next instruction (the one below "LOOP xxxx") is
executed.
The x86 uses internal adder circuitry to perform the subtraction command. Hence, the 2's complement
method is used by the microprocessor to perform the subtraction. The steps involved is –
1. Take the 2's complement of the subtrahend (source operand)
2. Add it to the minuend (destination operand)
3. Invert the carry.
These three steps are performed for every SUB instruction by the internal hardware of the x86 CPU. It is
after these three steps that the result is obtained and the flags are set. The following example illustrates
the three steps:
After the execution of SUB, if CF = 0, the result is positive; if CF = 1, the result is negative and
the destination has the 2's complement of the result.
5
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o Normally, the result is left in 2's complement, but the NOT and INC instructions can be used to
change it. The NOT instruction performs the 1’s complement of the operand; then the operand is
incremented to get the 2's complement; as shown in the following example:
The PTR (pointer) data directive is used to specify the size of the operand when it differs from the defined
size. In above Example; "WORD PTR" tells the assembler to use a word operand, even though the data is
defined as a double word.
6
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
UNSIGNED MULTIPLICATION AND DIVISION:
One of the major changes from the 8080/85 microprocessor to the 8086 was inclusion of instructions for
multiplication and division. The use of registers AX, AL, AH, and DX is necessary.
byte x byte: In byte-by-byte multiplication, one of the operands must be in the AL register and the
second operand can be either in a register or in memory. After the multiplication, the result is in AX.
In the program above, 25H is multiplied by 65H and the result is saved in word-sized memory named
RESULT. Here, the register addressing mode is used.
The next three examples show the register, direct, and register indirect addressing modes.
In the register addressing mode example, any 8-bit register could have been used in place BL.
Similarly, in the register indirect example, BX or DI could have been used as pointers.
If the register indirect addressing mode is used, the operand size must be specified with the help
of the PTR pseudo-instruction. In the absence of the "BYTE PTR" directive in the example above,
7
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
the assembler could not figure out if it should use a byte or word operand pointed at by SI. This
confusion may cause an error.
word x word: In word-by-word multiplication, one operand must be in AX and the second operand can
be in a register or memory. After the multiplication, registers DX and AX will contain the result. Since
word-by-word multiplication can produce a 32-bit result, DX will hold the higher word and AX the lower
word.
word x byte: This is similar to word-by-word multiplication, except that AL-contains the byte operand
and AH must be set to zero.
8-bit AL Q: AL 16-bit AX Q: AX
8-bit BL R: AH 16-bit BX R: DX
8
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
16-bit AX Q: AL 32-bit DA AX Q: AX
8-bit BL R: AH 16-bit BX R: DX
In divide, there could be cases where the CPU cannot perform the division. In these cases an interrupt is
activated. This is referred to as an exception. In following situations, the microprocessor cannot handle
the division and must call an interrupt:
1. If the denominator is zero (dividing any number by 00)
2. If the quotient is too large for the assigned register.
In the IBM PC and compatibles, if either of these cases happens, the PC will display the "divide error"
message.
byte/byte: In dividing a byte by a byte, the numerator must be in the AL register and AH must be set to
zero. The denominator cannot be immediate but can be in a register or memory. After the DIV instruction
is performed, the quotient is in AL and the remainder is in AH.
word/word: In this case, the numerator is in AX and DX must be cleared. The denominator can be in a
register or memory. After the DIV; AX will have the quotient and the remainder will be in DX.
9
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
word/byte: Here, the numerator is in AX and the denominator can be in a register or memory. After the
DIV instruction, AL will contain the quotient, and AH will contain the remainder. The maximum quotient
is FFH.
The following program divides AX = 2055 by CL = 100. Then AL = 14H (20 decimal) is the quotient and
AH = 37H (55 decimal) is the remainder.
Double-word/word: The numerator is in DX and AX, with the most significant word in DX and the least
significant word in AX. The denominator can be in a register or in memory. After the DIV instruction; the
quotient will be in AX, and the remainder in DX. The maximum quotient is FFFFH.
In the program above, the contents of DX: AX are divided by a word-sized data value, 10000.
The 8088/86 automatically uses DX: AX as the numerator anytime the denominator is a word in
size.
Notice in the example above that DATAl is defined as DD but fetched into a word-size register
with the help of WORD PTR. In the absence of WORD PTR, the assembler will generate an
error.
Table: Unsigned Division Summary
1
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
LOGIC INSTRUCTIONS:
Here, the logic instructions AND, OR, XOR, SHIFT, and COMPARE are discussed with examples.
Inputs Output
AND A B A AND B
0 0 0
This instruction will perform a logical AND on the operands and 0 1 0
place the result in the destination. The destination operand can be a 1 0 0
register or memory. The source operand can be a register, memory, 1 1 1
or immediate.
AND will automatically change the CF and OF to zero, and PF,
ZF, and SF are set according to the result. The rest of the flags are
either undecided or unaffected.
AND can be used to mask certain bits of the operand. The task of clearing a bit in a binary
number is called masking. It can also be used to test for a zero operand.
The above code will AND DH with itself, and set ZF =1, if the result is zero. This makes the CPU
to fetch from the target address XXXX. Otherwise, the instruction below JZ is executed. AND
can thus be used to test if a register contains zero.
Inputs Output
OR A B A OR B
0 0 0
The destination and source operands are ORed and the result is 0 1 1
placed in the destination. 1 0 1
The destination operand can be a register or in memory. The 1 1 1
source operand can be a register, memory, or immediate.
OR will automatically change the CF and OF to zero, and PF, ZF,
1
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
and SF are set according to the result. The rest of the flags are either undecided or unaffected.
The OR instruction can be used to test for a zero operand. For example, "OR BL, 0"will OR the
register BL with 0 and make ZF = 1, if BL is zero. "OR BL, BL" will achieve the same result.
OR can also be used to set certain bits of an operand to 1.
Inputs Output
XOR
A B A XOR B
0 0 0
The XOR instruction will eXclusive-OR the operands and place the
0 1 1
result in the destination. XOR sets the result bits to 1 if they are
1 0 1
not equal; otherwise, they are reset to 0.
1 1 0
The destination operand can be a register or in memory. The
source operand can be a register, memory, or immediate.
OR will automatically change the CF and OF to zero, and PF, ZF,
and SF are set according to the result. The rest of the flags are either undecided or unaffected.
1
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
XOR can be used to see if two registers have the same value. "XOR BX, CX" will make ZF = 1, if
both registers have the same value, and if they do, the result (0000) is saved in BX, the
destination.
XOR can also be used to toggle (invert/compliment) bits of an operand. For example, to toggle bit
2 of register AL:
This would cause bit 2 of AL to change to the opposite value; all other bits would remain
unchanged.
SHIFT
o Shift instructions shift the contents of a register or memory location right or left.
o The number of times (or bits) that the operand is shifted can be specified directly if it is once
only, or through the CL register if it is more than once.
o There are two kinds of shifts:
Logical – for unsigned operands
Arithmetic – signed operands.
SHR: This is the logical shift right. The operand is shifted right bit by bit, and for every shift the LSB
(least significant bit) will go to the carry flag (CF) and the MSB (most significant bit) is filled with 0.
SHR does affect the OF, SF, PF, and ZF flags.
The operand to be shifted can be in a register or in memory, but immediate addressing mode is
not allowed for shift instructions. For example, "SHR 25, CL" will cause the assembler to give an
error.
Eg:
SHR BH, CL R/M Cy
CL 02H
[Type text]
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Cy 1 0
[Type text]
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
If the operand is to be shifted once only, this is specified in the SHR instruction itself rather than
placing 1 in the CL. This saves coding of one instruction:
SHL: Shift left is also a logical shift. It is the reverse of SHR. After every shift the LSB is filled with 0
and the MSB goes to CF.
SHL does affect the OF, SF, PF, and ZF flags.
The operand to be shifted can be in a register or in memory, but immediate addressing mode is
not allowed for shift instructions. For example, "SHL 25, CL" will cause the assembler to give an
error.
Eg:
SHL BH, CL Cy R/M
0
[Type text]
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The CMP instruction compares two operands and changes the flags according to the result of the
comparison. The operands themselves remain unchanged.
The destination operand can be in a register or in memory and the source operand can be in a
register, memory, or immediate.
The compare instruction is really a SUBtraction, except that the values of the operands do not
change.
The flags are changed according to the execution of SUB. Although all the flags (CF, AF, SF, PF,
ZF, and OF flags) are affected, the only ones of interest are ZF and CF.
It must be emphasized that in CMP instructions, the operands are unaffected regardless of the
result of the comparison. Only the flags are affected.
Table: Flag Settings for Compare Instruction
Compare Operands CF ZF Remark
destination > source 0 0 destination – source; results CF = 0 & ZF = 0
destination = source 0 1 destination – source; results CF = 0 & ZF = 1
destination < source 1 0 destination – source; results CF = 1 & ZF = 0
In the program above, AX is greater than the contents of memory location DATA1 (0CCCCH >
235FH); therefore, CF = 0 and JNC (jump no carry) will go to target OVER.
VEMANA
15
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
In the above code, BX is smaller than CX (7888H < 9FFFH), which sets CF = 1, making "JNC
NEXT" fall through so that "ADD BX, 4000H" is executed.
In the example above, CX and BX still have their original values (CX = 9FFFH and BX =7888H)
after the execution of "CMP BX, CX".
Notice that CF is always checked for cases of greater or smaller than, but for equal, ZF must be
used.
The above program sample has a variable named TEMP, which is being checked to see if it has
reached 99.
In the following Program the CMP instruction is used to search for the highest byte in a series of 5 bytes
defined in the data segment.
The instruction "CMP AL, [BX]" works as follows ([BX] is the contents of the memory location
pointed at by register BX).
• If AL < [BX], then CF = 1 and [BX] becomes the basis of the new comparison.
• If AL > [BX], then CF = 0 and AL is the larger of the two values and remains the basis of
comparison.
Although JC (jump carry) and JNC (jump no carry) check the carry flag and can be used after a
compare instruction, it is recommended that JA (jump above) and JB (jump below) be used
because,
• The assemblers will unassembled JC as JB, and JNC as JA.
The below Program searches through five data items to find the highest grade.
The program has a variable called "Highest" that holds the highest grade found so far. One by
one, the grades are compared to Highest. If any of them is higher, that value is placed in Highest.
This continues until all data items are checked. A REPEAT-UNTIL structure was chosen in the
program design.
The program uses register AL to hold the highest grade found so far. AL is given the initial value
of 0. A loop is used to compare each of the 5 bytes with the value in AL.
VEMANA
16
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
If AL contains a higher value, the loop continues to check the next byte. If AL is smaller than the
byte being checked, the contents of AL are replaced by that byte and the loop continues.
Program 3-3
NOTE:
There is a relationship between the pattem of lowercase and uppercase letters, as shown below for A and
a:
A 0100 0001 41H
a 0110 0001 61H
The only bit that changes is d5. To change from lowercase to uppercase , d5 must be masked.
Note that small and capital letters in ASCII have the following values:
VEMANA
17
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The following Program uses the CMP instruction to determine if an ASCII character is uppercase or
lowercase.
The following Program first detects if the letter is in lowercase, and if it is, it is ANDed wit h
1101 1111B = DFH. Otherwise, it is simply left alone.
To determine if it is a lowercase letter, it is compared with 61H and 7AH to see if it is in the
range a to z. Anything above or below this range should be left alone.
In the following Program, 20H could have been subtracted from the lowercase letters instead of ANDing
with 1101 1111B.
VEMANA
18
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Program 3-4
Digit BCD
0 0000
BCD AND ASCII CONVERSION:
1 0001
o BCD (binary coded decimal) is needed because we use the digits 0 to 9 for
numbers in everyday life. Binary representation of 0 to 9 is called BCD. 2 0010
o In computer literature, one encounters two terms for BCD numbers: (1) unpacked 3 0011
VEMANA
19
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Packed BCD:
o In the case of packed BCD, a single byte has two BCD numbers in it, one in the lower 4 bits and
one in the upper 4 bits.
• For example, "0101 1001" is packed BCD for 59.
o It takes only 1 byte of memory to store the packed BCD operands. This is one reason to use
packed BCD since it is twice as efficient in storing data.
ASCII Numbers:
o In ASCII keyboards, when key "0" is activated, for example, "011 0000" (30H) is provided to the
computer. In the same way, 31H (011 0001) is provided for key "1", and so on, as shown in the
following list:
It must be noted that, although ASCII is standard in many countries, BCD numbers have universal
application. So, the data conversion from ASCII to BCD and vice versa should be studied.
20
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
• The Program 5-3c uses the based addressing mode (BX+ASC is used as a pointer.
Program 3-5a
Program 3-5b
Program 3-5c
ASCII to Packed BCD Conversion:
To convert ASCII to packed BCD, it is first converted to unpacked BCD (to get rid of the 3) and then
combined to make packed BCD.
For example, for 9 and 5 the keyboard gives 39 and 35, respectively. The goal is to produce 95H or"1001 0101",
which is called packed BCD. This process is illustrated in detail below:
VEMANA
21
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
After this conversion, the packed BCD numbers are processed and the result will be in packed BCD
format. There are special instructions, such as DAA and DAS, which require that the data be in packed
BCD form and give the result in packed BCD.
• For the result to be displayed on the monitor or be printed by the printer, it must be in ASCII
format. Conversion from packed BCD to ASCII is discussed next.
• After learning bow to convert ASCII to BCD, the application of BCD numbers is the next step.
• There are two instructions that deal specifically with BCD numbers: DAA and DAS.
Adding them gives 0011 1111B (3FH), which is not BCD! A BCD number can- only have digits from
0000 to 1001 (or 0 to 9). The result above should have been 17+ 28 = 45 (0100 0101).
To correct this problem, the programmer must add 6 (0110) to the low digit: 3F + 06 = 45H.
The same problem could have happened in the upper digit (for example, in 52H + 87H = D9H).
Again to solve this problem, 6 must be added to the upper digit (D9H + 60H = 139H), to ensure
that the result is BCD (52 + 87 = 139).
VEMANA
22
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
DAA
The DAA (decimal adjust for addition) instruction in x86 microprocessors is provided exactly for the
purpose of correcting the problem associated with BCD addition. DAA will add 6 to the lower nibble or
higher nibble if needed; otherwise, it will leave the result alone.
The following example will clarify these points:
After the program is executed, the DATA3 field will contain 72H (47 + 25 =72).
Note that DAA works only on AL. In other words, while the source can be an operand of any
addressing mode, the destination must be AL in order for DAA to work.
It needs to be emphasized that DAA must be used after the addition of BCD operands and that
BCD operands can never have any digit greater than 9. In other words, no A-F digit is allowed.
It is also important to note that DAA works only after an ADD instruction; it will not work after
the INC instruction.
In reality there is no other use for the AF (auxiliary flag) except for BCD addition and correction. For
example, adding 29H and 18H will result in 41H, which is incorrect as far as BCD is concerned.
See the following code:
The above example shows that 6 is added to the upper nibble due to the fact it is greater than 9.
VEMANA
23
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
VEMANA
24
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
MOV AL, 36H ; (AL)= 36H
ADD AL, 42H ; (AL) = 78H
DAA ; (AL) = 78H Just treat it as decimal with CF = 0
78H
+00H In this case, DAA same as ADD AL, 00H
=78H
The following Program demonstrates the use of DAA after addition of multibyte packed BCD numbers.
VEMANA
25
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Program 3-6
VEMANA
26
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
BCD Subtraction and Correction:
The problem associated with the addition of packed BCD numbers also shows up in subtraction. Again,
there is an instruction (DAS) specifically designed to solve the problem.
Therefore, when subtracting packed BCD (single-byte or multibyte) operands, the DAS instruction is put
after the SUB or SBB instruction. AL must be used as the destination register to make DAS work.
Summary of DAS Action:
1. If after a SUB or SBB instruction the lower nibble is greater than 9, or if AF = 1 , subtract 0110
from the lower 4 bits.
2. If the upper nibble is greater than 9, or CF = 1, subtract 0110 from the upper nibble.
Due to the widespread use of BCD numbers, a specific data directive, DT, has been created. DT can be
used to represent BCD numbers from 0 to 1020 – 1 (that is, twenty 9s).
Assume that the following operands represent the budget, the expenses, and the balance, which is the
budget minus the expenses.
VEMANA
27
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
More Examples:
1: Subtract decimal numbers 45 and 38.
VEMANA
28
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
ROTATE INSTRUCTIONS:
In many applications there is a need to perform a bitwise rotation of an operand. The rotation instructions
ROR, ROL and RCR, RCL are designed specifically for that purpose. They allow a program to rotate an
operand right or left.
o In rotate instructions, the operand can be in a register or memory. If the number of times an
operand is to be rotated is more than 1, this is indicated by CL. This is similar to the shift
instructions.
o There are two types of rotations. One is a simple rotation of the bits of the operand, and the other
is a rotation through the carry.
If the operand is to be rotated once, the 1 is coded, but if it is to be rotated more than once, register CL is
used to hold the number of times it is to be rotated.
Eg:
ROR BH, 1 R/M Cy
VEMANA
29
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
ROL (rotate left)
In rotate left, as bits are shifted from right to left they exit the left end (MSB) and enter the right end
(LSB). In addition, every bit that leaves the MSB is copied to the carry flag. In other words, in ROL the
MSB is moved to the LSB and is also copied to CF, as shown in the diagram.
If the operand is to be rotated once, the 1 is coded. Otherwise, the number of times it is to be rotated is in
CL. Eg:
ROL BH, CL Cy R/M
The following Program shows an application of the rotation instruction. The maximum count in Program
will be 8 since the program is counting the number of 1s in a byte of data. If the operand is a 16-bit word,
the number of 1s can go as high as 16.
Program 3-7
VEMANA
30
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The Program is similar to the previous one, rewritten for a word-sized operand. It also provides the count
in BCD format instead of hex. Reminder: AL is used to make a BCD counter because the because, the
DAA instruction works only on AL.
Program 3-8
If the operand is to be rotated once, the 1 is coded, but if it is to be rotated more than once, the register CL
holds the number of times.
Eg:
RCR BH, 1 R/M Cy
VEMANA
31
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
If the operand is to be rotated once, the 1 is coded, but if it is to be rotated more than once, register CL
holds the number of times.
Eg:
RCL BH, CL Cy R/M
VEMANA
32
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
INTERRUPTS IN x86 PC
8088/86 INTERRUPTS
o An interrupt is an external event that informs the CPU that a device needs its service. In 8088/86,
there are 256 interrupts: INT 00, INT 01, . . . , INT FF (sometimes called TYPEs).
o When an interrupt is executed, the microprocessor automatically saves the flag register (FR), the
instruction pointer (IP), and the code segment register (CS) on the stack; and goes to a fixed
memory location.
o In x86 PCs, the memory locations to which an interrupt goes is always four times the value of the
interrupt number. For example, INT 03 will go to address 0000CH (4 * 3 = 12 = 0CH). The
following Table is a partial list of the interrupt vector table.
Table: Interrupt Vector
33
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Processing Interrupts:
When the 8088/86 processes any interrupt (software or hardware), it goes through the following steps:
1. The flag register (FR) is pushed onto the stack and SP is decremented by 2, since FR is a 2-byte
register.
VEMANA
34
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
2. IF (interrupt enable flag) and TF (trap flag) are both cleared (IF = 0 and TF = 0). This masks
(causes the system to ignore) interrupt requests from the INTR pin and disables single stepping
while the CPU is executing the interrupt service routine.
3. The current CS is pushed onto the stack and SP is decremented by 2.
4. The current IP is pushed onto the stack and SP is decremented by 2.
5. The INT number (type) is multiplied by 4 to get the physical address of the location within the
vector table to fetch the CS and IP of the interrupt service routine.
6. From the new CS: IP, the CPU starts to fetch and execute instructions belonging to the ISR
program.
7. The last instruction of the interrupt service routine must be IRET, to get IP, CS, and FR back
from the stack and make the CPU run the code where it left off.
The following Figure summarizes these steps in diagram form.
Categories of Interrupts:
INT nn is a 2-byte instruction where the first byte is for the opcode and the second byte is the interrupt
number. We can have a maximum of 256 (INT 00 INT FFH) interrupts. Of these 256 interrupts, some are
used for software interrupts and some are for hardware interrupts.
1. Hardware Interrupts:
o There are three pins in the x86 that are associated with hardware interrupts. They are INTR
(interrupt request), NMI (non-maskable interrupt), and INTA (interrupt acknowledge).
o INTR is an input signal into the CPU, which can be masked (ignored) and unmasked through the
use of instructions CLI (clear interrupt flag) and STI (set interrupt flag).
VEMANA
35
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o If IF = 0 (in flag register), all hardware interrupt requests through INTR are ignored. This has no
effect on interrupts coming from the NMI pin. The instruction CLI (clear interrupt flag) will make
IF = 0.
o To allow interrupt request through the INTR pin, this flag must be set to one (IF = 1). The STI
(set interrupt flag) instruction can be used to set IF to 1.
o NMI, which is also an input signal into the CPU, cannot be masked and unmasked using
instructions CLI and STI; and for this reason it is called a non-maskable interrupt.
o INTR and NMI are activated externally by putting 5V on the pins of NMI and INTR of the x86
microprocessor.
o When either of these interrupts is activated, the x86 finishes the instruction that it is executing,
pushes FR and the CS: IP of the next instruction onto the stack, then jumps to a fixed location in
the interrupt vector table and fetches the CS: IP for the interrupt service routine (ISR) associated
with that interrupt.
o At the end of the ISR, the IRET instruction causes the CPU to get (pop) back its original FR and
CS: IP from the stack, thereby forcing the CPU to continue at the instruction where it left off
when the interrupt came in.
• Intel has embedded "INT 02" into the x86 microprocessor to be used only for NMI.
• Whenever the NMI pin is activated, the CPU will go to memory location 00008 to get the address
(CS: IP) of the interrupt service routine (ISR) associated with NMI.
• Memory locations 00008, 00009, 0000A, and 0000B contain the 4 bytes of CS: IP of the ISR
belonging to NMI.
• The 8259 programmable interrupt controller (PIC) chip can be connected to INTR to expand the
number of hardware interrupts to 64.
VEMANA
36
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
2. Software Interrupts:
o If an ISR is called upon as a result of the execution of an x86 instruction such as "INT nn", it is
referred to as software interrupt, since it was invoked from software, not from external hardware.
o Examples of such interrupts are DOS "INT 21H" function calls and video interrupts "INT 10H".
o These interrupts can be invoked in the sequence of code just like any other x86 instruction.
o Many of the interrupts in this category are used by the MS DOS operating system and IBM BIOS
to perform essential tasks that every computer must provide to the system and the user.
o Within this group of interrupts there are also some predefined functions associated with some of
the interrupts. They are "INT 00" (divide error), "INT 01" (single step), "INT 03" (breakpoint),
and "INT 04" (signed number overflow). Each is described below.
o The rest of the interrupts from "INT 05" to "INT FF" can be used to implement either software or
hardware interrupts.
INT 0 is also invoked if the quotient is too large to fit into the assigned register when executing a
DIV instruction. Look at the following case:
VEMANA
37
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
INT 03 (breakpoint)
To allow implementation of breakpoints in software engineering, Intel has set aside INT 03.
VEMANA
38
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
In single-step mode, one can inspect the CPU and system memory after the execution of each
instruction, a breakpoint is used to examine the CPU and memory after the execution of a group
of instructions.
INT 3 is a 1-byte instruction; where as all other “INT nn” instructions are 2-byte instructions.
Suppose that the data in the above program was DATA1 = +64 and DATA2 = +17. In that case,
OF would become 0; the INTO is not executed and acts simply as a NOP (no operation)
instruction.
VEMANA
39
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
INT 21H & INT 10H PROGRAMMING
The INT instruction has the following format:
Interrupts are numbered 00 to FF; this gives a total of 256 interrupts in x86 microprocessors. Of these 256
interrupts, two of them are the most widely used: INT 10H and INT 21H.
VEMANA
40
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
INT 10H Function 06H: Clearing the Screen
To clear the screen before displaying data; the following registers must contain certain values before INT
10H is called: AH = 06, AL = 00, BH = 07, CX = 0000, DH = 24, and DL= 79. The code will look like
this:
To clear the screen, the top left and bottom right values are used for start and stop points in order
to scroll up the entire screen. It is more efficient coding to clear the screen by combining some of
the lines above as follows:
VEMANA
41
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
After execution of the program above, registers DH and DL will have the current row and column
positions, and CX provides information about the shape of the cursor.
The reason that page 00 was chosen is that the video memory could contain more than one page
of data, depending on the video board installed on the PC.
In text mode, page 00 is chosen for the currently viewed page.
VEMANA
42
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The attribute provides information to the video circuitry, such as color and intensity of the
character (foreground) and the background.
The attribute byte for each character on the monochrome monitor is limited. The following Fig
shows bit definitions of the monochrome attribute byte.
VEMANA
43
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
From the bit definition, it can be seen that, the background can take eight different colors by combining
the prime colors red, blue, and green. The foreground can be any of 16 different colors by combining red,
blue, green, and intensity.
VEMANA
44
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The following Program shows the use of the attribute byte in CGA mode.
VEMANA
45
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o Hence, with a fixed amount of video RAM, the number of supported colors decreases as the
resolution increases.
Table: The 16 Possible Colors
I R G B Color I R G B Color
0 0 0 0 Black 1 0 0 0 Gray
0 0 0 1 Blue 1 0 0 1 Light Blue
0 0 1 0 Green 1 0 1 0 Light Green
0 0 1 1 Cyan 1 0 1 1 Light Cyan
0 1 0 0 Red 1 1 0 0 Light Red
0 1 0 1 Magenta 1 1 0 1 Light Magenta
0 1 1 0 Brown 1 1 1 0 Yellow
0 1 1 1 White 1 1 1 1 High Intensity White
VEMANA
46
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
INT 21H Option 09: Outputting a String of Data to the Monitor
INT 21H can be used to send a set of ASCII data to the monitor. To do that, the following
registers must be set: AH = 09 and DX = the offset address of the ASCII data to be displayed.
The address in the DX register is an offset address and DS is assumed to be the data segment.
INT 21H option 09 will display the ASCII data string pointed at by DX until it encounters the
dollar sign "$".
In the absence of encountering a dollar sign, DOS function call 09 will continue to display any
garbage that it can find in subsequent memory locations until it finds "$".
VEMANA
47
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Program 4-1
INT 21H Option 0AH: Inputting a String of Data from the Keyboard
Option 0AH of INT 21H provides a means by which one can get data from the keyboard and
store it in a predefined area of memory in the data segment.
To do this; the register options are: AH = 0AH and DX = offset address at which the string of
data is stored.
This is commonly referred to as a buffer area.
VEMANA
48
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
DOS requires that a buffer area be defined in the data segment and the first byte specifies the size
of the buffer. DOS will put the number of characters that came in through the keyboard in the
second byte and the keyed-in data is placed in the buffer starting at the third byte.
For example, the following program will accept up to six characters from the keyboard, including
the return (carriage return) key. Six locations were reserved for the buffer and filled with FFH.
The following shows portions of the data segment and code segment:
When this program is executed, the computer waits for the information to come in from the
keyboard.
When the data comes in, the IBM PC will not exit the INT 21H routine until it encounters the
return key.
Assuming the data that was entered through the keyboard was "USA" <RETURN>, the contents
of memory locations starting at offset 0010H would look like this:
The 0AH option of INT 21H accepts the string of data from the keyboard and echoes (displays) it
on the screen as it is keyed in.
49
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o In the absence of CR the string would be displayed wherever the cursor happened to be.
o In the case of CR and no LF, the string would be displayed on the same line after it had been
returned to the beginning of the line.
VEMANA
50
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Program 4-2
o The Program 4-3 prompts the user to type in a name. The name can have a maximum of eight
letters.
o After the name is typed in, the program gets the length of the name and prints it to the screen.
VEMANA
51
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Program 4-3
VEMANA
52
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o Program 4-4 demonstrates many of the functions described:
VEMANA
53
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Program 4-4
*********
*********
VEMANA
54