Download as pdf or txt
Download as pdf or txt
You are on page 1of 92

ARITHMETIC INSTRUCTION

 This group of instructions provides the 8086 with its basic integer
math skills.
 Addition, subtraction, multiplication, and division can all be
performed on different sizes and types of numbers.
 Addition Instructions: includes
 ADD: Addition
 ADC: Addition with carry
 INC: Increment (Add 1)
 Subtraction Instructions: This group of instructions consist of the
following group of instructions.
 SUB: Subtraction
 SBB: Subtraction with borrow
 DEC: Decrement
 NEG: 2’s Complement of a number
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
1
ADDITION OF NUMBERS
ADD destination , source
 Destination operand = destination operand + source operand
 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 80x86
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.
 Example :- If AX and BX contain l234H and 2345H, respectively, what is
the result of ADD AX, BX?
 Solution: Considering ADD AX, BX, adding 1234H to 2345H gives
3579H, which replaces the contents of AX, BX still contains 2345H.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


2
ADDITION OF NUMBERS
 EXAMPLE 1:- Show how the flag register is affected by:
MOV AL, 0F5H
ADD AL, 0BH
 SOLUTION:
F5H 1111 0101
+ 0BH + 0000 1011
100H 0000 0000
 After the addition, the AL register (destination) contains 00 and the
flags are as follows:
CF = 1 since there is a carry out from D7
SF = 0 the status of D7 of the result
PF = 1 the number of 1’s zero (zero is an even number)
AF = 1 there is a carry from D3 to D4
ZF = 1 the result of the action is zero (for the 8 bits)

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


3
ARITHMETIC INSTRUCTION
ADC Destination, Source (Add Bytes, or Words).
 The operation of ADC is similar to ADD; however, in addition to the source
operand the processor also adds the contents of the carry flag.
 The source may be an immediate number, a register, or a memory location.
 The destination may also be a register or a memory location.
 The source and the destination in an instruction cannot both be memory
locations. The source and the destination must be of the same type.
 All Flags are affected: AF, CF, OF, PF, SF, ZF.
 Example:- If AX and BX contain l234H and 2345H, respectively,
what is the result of ADC AX, BX? If CF=1
 Solution:
 AX, BX, and the carry flag are added together to get 357AH, which
replaces the contents of register AX.
 The carry flag is then cleared, because 357AH fits into a 16-bit register.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
4
CONT…

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


5
INC INSTRUCTION
INC Destination
 The INC instruction adds 1 to a specified register or to a memory location.
 AF, OF, PF, SF, and ZF are affected (updated) by this instruction.
 Note that the carry flag (CF) is not affected.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


6
Addition of individual byte and word data
CASE 1: Addition of individual byte and word data
 To calculate the total sum of any number of operands, the carry flag
should be checked after the addition of each operand.
 Program 3-la uses AH to accumulate carries as the operands are added
to AL.
 Example:- Write a program to calculate the total sum of 5 bytes of
data. Each byte represents the daily wages of a worker. This person
does not make more than $255 (FFH) a day. The decimal data is as
follows: 125,235, 197, 91, and 48.
 Solution:
 The result will be saved in the word-sized memory set aside in
the data segment.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


7
Cont…..

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


8
Addition of individual byte and word data
 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.
 Although this program works correctly, it is strongly recommended
that the following lines of the program be replaced:

 Actually, ADC AH, 00H, means AH = AH + 00H + CF


 SEE the following example:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


9
Addition of individual byte and word data
.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


10
Cont…
 The addition of many word operands works the same way.
 Register AX (or CX or DX or BX) could be used as the accumulator
and BX (or any general-purpose 16-bit register) for keeping the
carries.
 Program 3-lb is the same as Program 3-la, rewritten for word addition.
 Example: Write a program to calculate the total sum of five words of
data. Each data value represents the yearly wages of a worker. This
person does not make more than $65,535 (FFFFH) a year. The
decimal data is as follows: 27345, 28521, 29533, 30105, and 32375.
 Solution:
 The result saved in two words in the memory

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


11
Cont…

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


12
Subtraction of Unsigned Numbers
 Subtraction Instructions:
 This group of instructions consist of the following group of
instructions.
 SUB: Subtraction
 SBB: Subtraction with borrow
 DEC: Decrement
 NEG: 2’s Complement of a number

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


13
Subtraction of Unsigned Numbers
SUB destination, source
 destination = destination - source
 In subtraction, the 80x86 use the 2’s complement method.
 Although every CPU contains adder circuitry, it would be too
cumbersome (and take too many transistors) to design separate
subtractor circuitry.
 For this reason, the 80x86 uses internal adder circuitry to perform the
subtraction command.
 One can summarize the steps of the hardware of the CPU in executing
the SUB instruction for unsigned numbers, as follows.
 Take the 2’s complement of the subtrahend (source operand).
 Add it to the minuend (destination operand).
 Invert the carry.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


14
Cont…
 These three steps are performed for every SUB instruction by the internal
hardware of the 80x86 CPU. It is after these three steps that the result is
obtained and the flags are set.
 Example:

 The flags would be set as follows: CF = 0, ZF = 0, AF = 0, PF = 0, and SF =


0. The programmer must look at the carry flag (not the sign flag) to
determine if the result is positive or negative.
 Example:- if AL=3FH and BL=23H, find SUB BL, AL?
 Solution:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


15
Cont…
 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.
 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 l’s complement of the operand;
then the operand is incremented to get the 2’s complement.
 See Example.
 Example:- If AL contains 00 and BL contains 0l, what is the result of SUB
AL, BL?
 Solution: Subtracting 01 from 00 in 8-bit binary results in FFH, which is the
2's complement representation of -1.
 So, the contents of AL are replaced with FFH, and both the carry and
sign flags are set to indicate a borrow and a negative result.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
16
SBB (subtract with borrow)
 This instruction is used for multi-byte (multiword) numbers and will
take care of the borrow of the lower operand.
 If the carry flag is 0, SBB works like SUB.
 If the carry flag is 1, SBB subtracts 1 from the result.
 Example: If AL = 95H, and BL = 4FH prior to the execution of
"SBB AL, BL",
a) what will be the contents of AL after the subtraction if CF = 1?
b) what will be the contents of AL after the subtraction if CF = 0?
 Solution:
a) SBB AL, BL ; since CF= 1, AL = AL - BL – CF = 45H
b) SBB AL, BL ; Since CF = 0, AL = AL - BL = 46H

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


17
SBB (subtract with borrow)

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


18
DEC INSTRUCTION:
DEC DESTINATION.
 The DEC instruction subtract 1 from the specified destination.
 The destination may be a register or a memory location.
 The AF, OF, PF, SF and ZF flags are affected.
 Note that: The CF is not affected.
 If the contents of 8-bit register are 00H and 16-bit register are 0000H,
after DEC instruction contents of registers will be FFH and FFFFH
respectively without affecting CF.
 Example:- What is the result of DEC AL? Assume that the AL = B0H
 Solution:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


19
NEG THIS INSTRUCTION
FORM 2’S COMPLEMENT; NEG DESTINATION
 This instruction replaces the number in a destination with
the 2’s complement of that number.
 The destination can be a register or a memory location.
 Instruction can be implemented by inverting each bit and
adding 1 to it.
 The flags that can be affected are: AF, CF, SF, PF, ZF and
OF.
If AL is 35H, determine NEG AL

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


20
CMP (COMPARISON INSTRUCTION)
CMP DESTINATION, SOURCE
 This instruction compares a byte from the specified source with a byte
from the specified destination, or a word from the specified source
with a word from the specified destination.
 The source can be an immediate number, a register, or a memory
location.
 The destination can be a register or a memory location.
 The comparison is actually done by subtracting the source byte or
word from the destination byte or word.
 The source and the destination are not changed, but the flags are set to
indicate the results of the comparison.
 AF, OF, SF, ZF, PF, and CF are updated by the CMP instruction.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


21
CMP (COMPARISON INSTRUCTION)
.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


22
MULTIPLICATION INSTRUCTIONS:
 This group of instructions consist of following group of
instructions:
 MUL: Unsigned Multiplication
 IMUL: Signed Multiplication
 In multiplying or dividing two numbers in the 80x86
microprocessor, the use of registers AX, AL, AH, and DX is
necessary since these functions assume the use of those registers.
MULTIPLICATION OF UNSIGNED NUMBERS
 In discussing multiplication, the following cases will be
examined:
 Byte times byte,
 Word times word, and
 Byte times word.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
23
MULTIPLICATION OF UNSIGNED NUMBERS
 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.
 Example:- determine MUL BL, if AL=25H and BL=10H.
 Solution: AX = AL * BL = 0250H
 Example: Write a program to multiply two byte number and save the result
in one word memory location RESULT. You can take any two byte.
 Solution:

 In the program above, 25H is multiplied by 65H and the result is saved in
word-sized memory named RESULT.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
24
Cont…
 WORD X WORD:
 In word by word multiplication, registers AX and DX will contain the
result.
 Since word x word multiplication can produce a 32-bit result, AX will
hold the lower word and DX the higher word.
 Example: write a program to multiply two words and save a result in
two word memory location of RESULT1.
 Solution:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


25
Cont…
 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.
 Example: write a program to multiply a byte and a word and save a
result in two word memory location RESULT3.
 Solution:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


26
SUMMERY OF UNSIGNED MULTIPLICATION
 These three steps are performed for every SUB instruction by the

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


27
MULTIPLICATION INSTRUCTIONS:
 Example :- What is the result of MUL CL if AL contains 20H and CL
contains 80H? What is the result of MUL AX if AX contains A064H?
 Solution:
 The decimal equivalents of 20H and 80H are 32 and 128,
respectively.
 Upon completion, AX will contain 1000H.
 The decimal equivalent of A064H is 41,060.
 MUL AX multiplies the accumulator by itself, giving
1,685,923,600 as the result, which is 647D2710H.
 The upper half of this hexadecimal number (647DH) will be
placed into register DX.
 The lower half (2710H) will replace the contents of AX.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


28
DIVISION INSTRUCTIONS
 This group consists of the following group of instructions
 DIV The division of unsigned numbers
 IDIV The division of signed numbers
 DIV Source: (Divide Bytes or Words Unsigned).
 In this instruction, the accumulator is divided by the value
represented by the source operand.
 All numbers are interpreted as unsigned binary numbers.
 If the source is a byte, the quotient is placed into AL and
the remainder into AH.
 If the source is a word, it is divided into the 32-bit number
represented by DX and AX (with DX holding the upper 16
bits).
 The 16-bit quotient is placed into AX and the 16-bit
remainder into DX.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


29
DIVISION INSTRUCTIONS
 EXAMPLE:
 What is the result of DIV BL if AX contains 2710H
and BL contains 32H?

 SOLUTION:
 The decimal equivalents of 2710H and 32H are 10,000
and 50, respectively.
 The quotient of these two numbers is 200, which is
00C8H.
 This is the byte placed into AL.
 Because the division had no remainder, AH will
contain 00.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


30
DIVISION INSTRUCTIONS
IMUL
IDIV

[READING ASSINGEMENT]

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


31
BCD AND ASCII ARITHMETIC INSTRUCTIONS
 In 80x86 microprocessors, there are many instructions that handle
ASCII and BCD numbers.
BCD NUMBERS
 BCD stands for binary coded decimal. BCD is needed because human
beings use the digits 0 to 9 for numbers.
 Binary representation of 0 to 9 is called BCD (see Figure below).
 In computer literature one encounters two terms for BCD numbers:
 Unpacked BCD
 Packed BCD

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


32
Cont…
Unpacked BCD
 In unpacked BCD, the lower 4 bits of the number represent the
BCD number and the rest of the bits are 0.
 Example: "0000 1001" and "0000 0101" are unpacked BCD for
9 and 5, respectively.
 In the case of unpacked BCD it takes 1 byte of memory location
or a register of 8 bits to contain it.
Packed BCD
 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.
 It takes only 1 byte of memory to store the packed BCD
operands.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
33
ASCII numbers
 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.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


34
ASCII to BCD conversion
1) ASCII to unpacked BCD conversion
 This can be done by freeing 3 from ASCII.
 To do that, each ASCII number is ANDed with "0000 1111" (0FH), as
shown in the next example.
 Example: if ASCII in AL is 35H, then convert this to unpacked BCD.
 Solution:
MOV AL, 35H
AND AL, 0FH ;
 Example: if ASCII in AL is 38H, then convert this to unpacked BCD.
 Solution: MOV AL, 38H
AND AL, 0FH

AL=08H, unpacked BCD


Hu, Institute Of Technology Department Of Electrical And Computer Engineering
35
Cont…
Packed BCD to ASCII conversion
 To convert packed BCD to ASCII, it must first be converted to
unpacked and then the unpacked BCD is ORed with 011 0000 (30H).
 The following shows the process of converting from packed BCD to
ASCII.

BCD Addition And Subtraction


 There are two instructions that deal specifically with BCD numbers:
 DAA(Decimal Adjust After Addition) and
 DAS(Decimal Adjust After Subtraction)

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


36
BCD addition and subtraction
BCD addition and correction
 There is a problem with adding BCD numbers, which must be corrected.
 Look at this example: if AL = 17H and BL= 28H then, ADD AL, BL will be,
 AL = 0011 1111 (3FH), which is not BCD!
 A BCD number can only have digits from 0000 to 1001 (or 0 to 9).
 In other words, adding two BCD numbers must give a BCD result.
 To correct this problem, the programmer must add 6 (0110) to the low digit:
3F + 06 = 45H.
 This instruction works as follows:
 If the value of the low-order four bits (D3-D0) in the AL is greater than
9 or if AF is set, the instruction adds 6 (06) to the low-order four bits.
 If the value of the high-order four bits (D7-D4) in the AL is greater
than 9 or if carry flag is set, the instruction adds 6 (60) to the high-
order four bits.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


37
THE DAA (DECIMAL ADJUST AFTER ADDITION)
 Example:- If ADD AL, BL is executed with AL containing 15H and
BL containing 25H, what is the resulting value of AL? What does AL
contain if DAA is executed next?
 Solution:
 Adding l5H to 25H gives 3AH, as we just saw.
 When DAA executes, it will examine AL and determine that it
should be corrected to 40H to give the correct packed decimal
answer.
 It is interesting that the processor adds 06H to AL to convert it
into a packed number.
MOV AL,15H
MOV BL, 25H
ADD AL, BL
DAA ; AL IS THEN ADJUSTED

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


38
Cont…
 Note that DAA works only on AL.
 That means, the destination must be AL in order for DAA to work.
 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
 Example: If ADD AL, BL is executed with AL containing 29H and BL
containing 18H, what is the resulting value of AL? What does AL contain if
DAA is executed next?
 Solution:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


39
Cont…

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


40
BCD AND ASCII ARITHMETIC INSTRUCTIONS
THE DAS (DECIMAL ADJUST AFTER SUBTRACTION)
 This instruction works as follows:
 If the value of the low-order four bits (D3-D0) in the AL is greater than
9 or if AF is set, the instruction subtracts 6 (06) from the low-order four
bits.
 If the value of the high-order four bits (D7-D4) in the AL is greater
than 9 or if carry flag is set, the instruction subtracts 6 (60) from the
high-order four bits.
 Example:- If AL contains 10H and CL contains 02H, what is the
result of SUB AL, CL? What happens if DAS is executed next?
 Solution:
 Subtracting 02H from 10H will result in AL containing OEH.
 Following the SUB instruction with DAS will cause AL to be
converted into 08H, the correct packed decimal answer.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


41
Cont…

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


42
ASCII ARITHMETIC INSTRUCTIONS
 Numerical data coming into a computer from a terminal is
usually in ASCII code.
 In this code, the numbers 0 to 9 are represented by the
ASCII codes 30H to 39H.
 The 8086 provides four instructions for ASCII arithmetic.
 AAA: ASCII adjust after addition
 AAS: ASCII adjust after subtraction
 AAM: ASCII adjust after multiplication
 AAD: ASCII adjust before division

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


43
ASCII ARITHMETIC INSTRUCTIONS
AAA: ASCII ADJUST AFTER ADDITION
 Used to add the ASCII codes for two decimal digits without masking
off the "3" in the upper nibble of each.
 After the addition, the AAA Instruction is used to make sure the result
is the correct unpacked BCD.
 AAA will examine the contents of AL, adjusting the lower 4 bits to
make the correct decimal result(unpacked BCD).
 Then it will clear the upper 4 bits and add 1 to AH if the lower 4 bits
of AL were initially greater than 9.
 Only the carry and auxiliary carry flags are directly affected.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


44
Cont…
 Example: Assume AL= ASCII 5 and BL=ASCII 2, then:

 Example: Assume AL= ASCII 5 and BL=ASCII 9, then:

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


45
ASCII ARITHMETIC INSTRUCTIONS
AAS: ASCII ADJUST AFTER SUBTRACTION
 Used to subtract the ASCII codes for two decimal digits without masking the
"3" in the upper nibble of each.
 The AAS instruction is then used to make sure the result is the correct
unpacked BCD.
 The AAS instruction leaves the correct unpacked BCD result in the low
nibble of AL and resets the upper nibble of AH to all 0's.
 If you want to get ASCII CODE, you can OR AL with 30H to produce the
correct ASCII code for the result.
 If multiple-digit numbers are being subtracted, the CF can be taken into
account by using the SBB instruction when subtracting the next digits.
 The AAS instruction works only on the AL register.
 It updates AF and CF, but OF, PF, SF, and ZF are left undefined.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


46
ASCII ARITHMETIC INSTRUCTIONS
 .

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


47
ASCII ARITHMETIC INSTRUCTIONS
AAM: ASCII ADJUST AFTER MULTIPLICATION
 After the two unpacked BCD digits are multiplied, the AAM instruction is
used to adjust the product to two unpacked BCD digits in AX.
 This instruction is used to adjust the results of a MUL instruction so that the
results can be interpreted as ASCII characters.
 Both the operands used in the multiplication must be less than or equal to 9
 This ensures that the upper byte of the result placed in AX will be 00, a
necessary requirement for proper operation of AAM.
 AAM will convert the binary product found in AL into two separate digits.
 One digit replaces the contents of AL and the other digit replaces the
contents of AH.
 Software can then be used to add 30H to each value to obtain the ASCII
codes for the correct product.
 Only the parity, sign, and zero flags are directly affected.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
48
ASCII ARITHMETIC INSTRUCTIONS
 Example:- AL and BL are given initial values of 7 and 6,
respectively. MUL BL produces 002AH in AX. Note that 7 times 6 is
42 and that 2AH is equal to 42. What are the contents of AX after
AAM is executed?
 Solution:
 Because the upper 4 bits of each operand used in the
multiplication were 0 and the lower 4 bits contained numbers less
than or equal to 9, AAM will be able to correctly convert the
product in AX.
 AAM converts the 2AH in AL into the digits 4 and 2 and writes
them in AH and AL, respectively.
 The final contents of AX are 0402H.
 If we now add 30H to each byte in AX, we get 3432H, the two
ASCII codes for 42.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


49
Cont…

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


50
ASCII ARITHMETIC INSTRUCTIONS
AAD: ASCII ADJUST BEFORE DIVISION
 READING ASSIGNMENT

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


51
BIT MANIPULATION INSTRUCTIONS
 The instructions in this group are used to perform:
 Boolean (logical) operations on binary data,
 Shift or Rotate bits left or
 Shift or Rotate bits Right in register or memory
operands.
 These operations are very useful when converting data from
one form to another or for manipulating specific patterns,
such as a single bit that moves through each position of a
register.
 These instructions include:
 NOT, AND, OR, XOR, TEST, SHL/SAL, SHR, SAR,
ROL, ROR, RCL, & RCR
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
52
BIT MANIPULATION INSTRUCTIONS
NOT INSTRUCTION; NOT DESTINATION
 The NOT instruction inverts each bit (forms the 1's complement) of
the byte or word at the specified destination.
 The destination can be a register or a memory location.
 No flags are affected by the NOT Instruction.
 Example:-
 NOT BX ; Complement contents of BX register
 NOT BYTE PTR [BX] ;Complement memory byte at offset
[BX] in data segment
 Example:-What is the result of NOT AL if AL contains 3EH?
 Solution: The binary equivalent of 3EH is 00111110. When these bits
are complemented we get 11000001, which is C1H. This is the final
value in AL.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
53
BIT MANIPULATION INSTRUCTIONS
AND INSTRUCTION: AND DESTINATION, SOURCE
 ANDs each bit in a source byte or word with the same number bit in a
destination byte or word. The result is put in the specified destination.
 The contents of the specified source will not be changed.
 A bit can be masked (reset) by ANDing it with 0.
 The source operand can be an immediate number, the contents of a
register, or the contents of a memory location.
 The destination can be a register or a memory location.
 The source and the destination cannot both be memory locations in
the same instruction.
 Flags: CF and OF are both 0 after AND.
 PF, SF, and ZF are updated by AND. AF is undefined.
 Note that PF has meaning only for an 8-bit operand.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
54
BIT MANIPULATION INSTRUCTIONS
 Example:- What is the result of AND AL, BL. Assume that the BL=27H
and that AL contains BCH.
 Solution:
 ANDs its contents
 27H = 0010 0111
 BCH = 1011 1100
 The result is 24H, which is placed into AL.

 EXAMPLES :

AND CX, [SI] ;AND word in DS at offset [SI] with word in CX register
;Result in CX register

AND BH, CL ;AND byte in CL with byte in BH Result in BH

AND BX, 00FFH ;AND word in BX with immediate 00FFH.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


55
BIT MANIPULATION INSTRUCTIONS
OR INSTRUCTION; OR DESTINATION, SOURCE
 This instruction ORs each bit in a source byte or word with the
corresponding bit in a destination byte or word. The result is put in the
specified destination.
 The contents of the specified source will not be changed.
 A bit in the destination operand can be set to a 1 by simply ORing that bit
with a 1 in the same bit of the source operand. A bit ORed with 0 is not
changed.
 The source operand can be an immediate number, the contents of a register,
or the contents of a memory location .
 The destination can be a register or a memory location.
 The source and the destination cannot both be memory locations in the same
instruction.
 Flags: CF and OF are both 0 after OR.
 PF, SF, and ZF are updated by the OR instruction. AF is undefined after OR.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
56
BIT MANIPULATION INSTRUCTIONS
 Example:-
 Register BX contains 2589H. What is the result of OR BX,
0C77CH?
 Solution:
 As a matter of habit, whenever we use a hexadecimal number in
an instruction, we precede it with a leading 0 if the first
hexadecimal digit is greater than 9.
 So, the correct way to specify C77CH in the OR instruction is to
write 0C77CH.
 The purpose for doing this is to prevent the assembler from
thinking that C77CH is the name of a label somewhere in the
program.
 So, the OR instruction operates on BX and the immediate data to
produce the final result of E7FDH in register BX.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


57
Cont….

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


58
BIT MANIPULATION INSTRUCTIONS
XOR Instruction; XOR Destination, source
 This instruction Exclusive-ORs each bit in a source byte or word with the
same number bit in a destination byte or word.
 The result replaces the contents of the specified destination. The contents of
the specified source will not be changed.
 A bit Exclusive-ORed with a 1 will be inverted. A bit Exclusive-ORed with
a 0 will not be changed.
 The source operand can be an immediate number, the contents of a register,
or the contents of a memory .
 The destination can be a register or a memory location. The source and
destination cannot both be memory locations in the same instruction.
 Flags: CF and OF are both 0 after XOR. PF, SF, and ZF are updated.
 PF has meaning only for an 8-bit operand.
 AF is undefined after XOR.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
59
BIT MANIPULATION INSTRUCTIONS
 EXAMPLES:
XOR CL, BH ;Byte in BH Exclusive-ORed with byte in CL.
;Result in CL, and BH not changed
XOR BP, DI ;Word in DI Exclusive-ORed with word in BP.
;Result in BP, and DI not changed
XOR WORD PTR [BX], 00FFH ;Exclusive-OR immediate number
;00FFH with word at offset
` ;[BXI in data segment.
;Result in memory location [BX]
 Example:- If BX = 3CD9H, CX = 00FFH, then Find XOR BX, CX.
 Solution:- Result: BX = 0011 1100 0010 0110 =3C26H
 Note bits in lower byte are inverted ZF = 0, PF =0

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


60
BIT MANIPULATION INSTRUCTIONS
TEST INSTRUCTION; TEST DESTINATION, SOURCE
 This instruction ANDs the contents of a source byte or word with the
contents of the specified destination word.
 Flags are updated, but neither operand is changed.
 The TEST instruction is often used to set flags before a Conditional Jump
instruction.
 The source operand can be an immediate number, the contents of a register,
or the contents of a memory .
 The destination operand can be in a register or in a memory location. The
source and the destination cannot both be memory locations in an
instruction.
 Flags: CF and OF are both 0's after TEST.
 PF, SF, and ZF will be updated to show the results of the ANDing.
 PF has meaning only for the lower 8 bits of the destination.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
61
Cont…

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


62
SHIFT AND ROTATE INSTRUCTIONS
 PF, SF and ZF flags are affected by shift instructions.
 Shift Instructions:
 SHL : Shift Logical Right Byte, or Word
 SHR : Shift Logical Left Byte, or Word
 SAR : Shift Arithmetic Right Byte, or Word
 SAL : Shift Arithmetic Left Byte, or Word
 ROTATE Instructions:
 ROL: Rotate Left Byte, or Word.
 ROR: Rotate Right Byte, or Word.
 RCL: Rotate Left through Carry Byte, or Word
 RCL: Rotate Right Through carry Byte, or Word

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


63
SHIFT INSTRUCTION
SHL TARGET, COUNT ;
 The count operand indicates how many bits the destination operand is
to be shifted to the left.
 The target can be a register or a memory
 SHL multiplies the number signed or unsigned by 2 on each shift.
 Shifts may be specified as
 SHL Target, 1 ;
 SHL Target, CL ;
 All bits are shifted left, with a 0 entering the LSB each time.
 Bits that shift out of the MSB position are placed into the carry flag.
 Other flags are modified according to the final results.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


64
SHIFT INSTRUCTION

 Example:- What are the results of SHL AL, CL


if AL contains 75H and CL contains 3?
 Solution:
 A 0 is shifted in from the right each time.
 The last bit shifted out of the MSB
position is the final state of the carry flag.
 The final contents of AL are A8H

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


65
SHIFT INSTRUCTION
SHR TARGET, COUNT ;
 Bits shifting to the right in the destination operand. The target can be a register or a
memory. SHR dividing the number signed or unsigned by 2 on each shift.
 Zeros are shifted in from the left and the bits that fall out of the LSB position end up
in the carry flag.
 Shifts may be specified as
 SHR Target, 1 ;
 SHR Target, CL ;
 Example:- What are the results of SHL AL, CL if AL = 75H and CL = 3?
 Solution:-
 Example: Determine the content of AL and BL after execution of the following
instruction.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


66
SHIFT INSTRUCTION
SAL TARGET, COUNT ( Shift arithmetic left instruction )
 SAL is very similar to SHL.
 SAL Target, 1;
 SAL Target, CL ;
 SAL multiply a signed number by 2 on each shift. The target can be a
register or a memory. The count should be 1 or CL.
 Example: Determine the content of AL nad BL after execution of the
following instruction. Find CF, OF, ZF, PF, SF

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


67
SHIFT INSTRUCTION
SAR TARGET, COUNT ( Shift arithmetic right instruction )
 SAR is very similar to SHR with the important difference being that the MSB is
shifted back into itself. The target can be a register or a memory
 SHR dividing the number signed by 2 on each shift. The count should be 1 or CL.
 Shifts may be specified as
 SAR Target, 1 ;
 SAR Target, CL ;
 Example:- What are the results of SAR AL, CL if AL = 96H and CL = 3?
 Solution:-
 Example: Determine the content of AL and BL after execution of the following
instruction.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


68
ROTATE INSTRUCTION
ROL TARGET, COUNT (Rotate left byte, or word).
 The data inside the destination operand circulated within itself.
 A copy of the bit that rotates out of the MSB is placed into the carry flag.
 The only other flag affected is the overflow flag.
 ROL Target, 1 ;
 ROL Target, CL ;
 The count should be 1 or CL.
 Example:- What are the results of ROL AL, CL if AL = B7H and CL = 3?
 Solution:-
 Example: Determine the content of AL after execution.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


69
ROTATE INSTRUCTION
ROR TARGET, COUNT (Rotate right byte, or word).
 The data inside the destination operand circulated within itself. A copy of the
bit that rotates out of the LSB is placed into the carry flag.
 The only other flag affected is the overflow flag.
 ROR Target, 1 ;
 ROR Target, CL ;
 The count should be 1 or CX.
 Target can be all except immediate and segment register
 Example: MOV AL, BCH
ROR AL, 1
ROR AL, 1

 Exercise:- What is the result of executing ROR AX, 1 twice? AX


initially contains 3E95H.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
70
ROTATE INSTRUCTION
RCL DESTINATION, COUNT (Rotate left through carry).
 The operation of RCL is similar to ROL except for how the carry flag is
used. RCL is a 9-, or 17-bit rotate.
 The bit that gets rotated out of the MSB goes into the carry flag and the bit
that was in the carry flag gets rotated into the LSB.
 RCL Target, 1 ;
 RCL Target, CL ;
 The count should be 1 or CL.
 Target can be all except immediate and segment register
 Example: Assume initially CF = 0, find AL after execution of the following:
MOV AL, BCH
RCL AL, 1
RCL AL, 1
RCL AL, 1
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
71
ROTATE INSTRUCTION
RCR DESTINATION, COUNT (Rotate right through carry).
 This instruction complements the operation of RCL, rotating data bits right
within the destination operand.
 The bit rotated out of the LSB goes into the carry flag. The bit that comes
out of the carry flag goes into the MSB. RCR is a 9-, or 17-bitrotate.
 RCR Target, 1 ;
 RCR Target, CL ;
 Target can be all except immediate and segment register
 Example: What is in AX after execution of RCR AX, CL if CL contains 2 and AX
contains ABCDH? The carry flag is initially cleared.
 Example: Assume initially CF = 0, find AL after execution of the following;
MOV AL, BCH
RCR AL, 1
RCR AL, 1
RCR AL, 1
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
72
SUBROUTINE AND INTERRUPT INSTRUCTIONS
CALL– CALL A PROCEDURE:
 The CALL instruction is used to transfer execution to a subprogram or
procedure. There are two basic types of calls, near and far.
 A near call is a call to a procedure, which is in the same code segment as
the CALL instruction.
 When the 8086 executes a near CALL instruction, it decrements the stack
pointer by 2 and copies the offset of the next instruction after the CALL onto
the stack. This offset saved on the stack is referred to as the return address.
 A near CALL instruction will also load the instruction pointer with the offset
of the first instruction in the procedure.
 A RET instruction at the end of the procedure will return execution to the
instruction after the call by copying the offset saved on the stack back to IP.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


73
SUBROUTINE AND INTERRUPT INSTRUCTIONS
 A far call is a call to a procedure, which is in a different segment from
the one that contains the CALL instruction.
 When the 8086 executes a far call, it decrements the stack pointer by 2
and copies the contents of the CS register to the stack.
 It then decrements the stack pointer by 2 again and copies the offset of
the instruction after the CALL instruction to the stack.
 Finally, it loads CS with the segment base of the segment, which
contains the procedure, and loads IP with the offset of the first
instruction of the procedure in that segment.
 A RET instruction at the end of the procedure will return execution to
the next Instruction after the CALL by restoring the saved values of
CS and IP from the stack.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
74
Cont…

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


75
SUBROUTINE AND INTERRUPT INSTRUCTIONS
RET-Return Execution from Procedure to Calling Program
 The RET instruction will return execution from a procedure to the
next instruction after the CALL instruction which was used to CALL
the procedure.
 If the procedure is a near procedure (in the same code segment as the
CALL instruction), then the return will be done by replacing the
instruction pointer with a word from the top of the stack.
 If the procedure is a far procedure, then the instruction pointer will be
replaced by the word at the top of the stack.
 The stack pointer will then be incremented by 2.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


76
Cont…
 The code segment register is then replaced with a word from the new
top of the stack.
 A RET instruction can be followed by a number, for example, RET 6.
 In this case the stack pointer will be incremented by an additional six
addresses after the IP or the IP and CS are popped off the stack.
 The RET instruction affects no flags.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


77
INTERRUPT INSTRUCTIONS
INT-interrupt Program Execution-INT Type
 ‘Interrupt’ is an instruction that breaks the normal sequence of
execution of instructions, diverts its execution to some other program
called Interrupt Service Routine (ISR).
 After executing ISR, the control is transferred back again to the main
program which was being executed at the time of interruption.
 Normal program can be interrupted by three ways:
 By external signal
 By a special instruction in the program or
 By the occurrence of some condition.
 The term type in the instruction format refers to a number between 0
and 255 which identifies the interrupt.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


78
INTERRUPT INSTRUCTIONS
 When an 8086 executes an INT instruction, it will:
 Decrement the stack pointer by 2 and push- the flags onto the
stack.
 Decrement the stack pointer by 2 and push the contents of CS
onto the stack.
 Decrement the stack pointer by 2 and push the offset of the
next instruction after the INT number Instruction on the stack.
 Get a new value for IP from an absolute memory address of 4
times the type specified in the instruction.
 For an INT 8 instruction, for example, the new value of CS will
be read from address 00022H.
 Reset both IF and TF.
 Other flags are not affected.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


79
INTERRUPT INSTRUCTIONS

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


80
INTERRUPT INSTRUCTIONS
 Examples :-
 INT 35 ;New IP from 0008CH, new CS from 0008EH
 INT 3 ;This is a special form, which has the single-byte
;code of CCH.
;Many systems use this as a breakpoint instruction.
;New IP from 0000CH, new CS from 0000EH.
 Therefore, there are two major types of interrupts
 Hardware generated (derived from a hardware signal or
external signal)
 Software generated (internally derived from the execution of an
instruction which could be by special instruction in the program
or condition produced by instruction)

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


81
INTERRUPT INSTRUCTIONS
 External Signal (Hardware Interrupt)
 In case of 8086, there are two interrupt pins:
 NMI and
 INTR.
 The NMI is a non-maskable interrupt input pin, which means that any interrupt
request at NMI input cannot be masked, or disabled by any means.
 The INTR interrupt, however, may be masked using the interrupt flag (IF).
 Special Instruction (software Interrupt)
 Intel 8086 supports special instruction for interrupt, called INT, to execute
special program.
 Example: INT 21H, INT 10H
 Condition Produced by Instruction (software Interrupt)
 Intel 8086 can be interrupted by some condition produced by execution of an
instruction.
 Example: MOV BL, 00H then DIV BX ; divide by zero ISR is executed

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


82
INTERRUPT INSTRUCTIONS
 Interrupt Vector
 This vector gives us information about the location of an ISR in
the memory.
 An interrupt vector is a 4-byte number.
 The first two bytes give us the value of IP (offset) for the ISR,
where the last two bytes contain the value of CS register.
 The physical address (20 bits) is defined by : CS*10h + IP
 The interrupt vectors are located in the first 1K byte area of the
memory
 Intel reserves the first 32 interrupt vectors for the present and
features microprocessor products.
 The remaining interrupt vectors (32-255) are available for the
user.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


83
CONT…

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


84
EXAMPLE
 Examples:
 INT 35 ;New IP from 0008CH, new CS from 0008EH

 Example of MS-DOS interrupts:


 INT 21h/AH = 1read character from standard input, with echo,
result is stored in AL.
 If there is no character in the keyboard buffer, the function waits
until any key is pressed.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


85
EXAMPLE OF MS-DOS INTERRUPTS:
 Example:- INT 21h /AH = 2 write character to standard output entry. DL
register contains the character to write, after execution AL = DL.

 Example:- INT 21h / AH=9 - Output of a string at DS:DX. String must be


terminated by ‘$’.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


86
PROCESSOR CONTROL INSTRUCTIONS
 FLAG SET/CLEAR INSTRUCTIONS:
 STC: sets the carry flag, STC doesn’t affect any other flag.
 CLC: resets the carry flag to zero. CLC doesn’t affect any other
flag.
 CMC: complements the carry flag. CMC doesn’t affect any other
flag.
 STD: sets the direction flag. It doesn’t affect any other flag.
 CLD: reset the DF. It doesn’t affect any other flag.
 STI: sets the interrupt flag to one. This enables INTR interrupt of
the 8086. It doesn’t affect any other flag.
 CLI: resets the interrupt flag to zero. Due to this 8086 will not
respond to an interrupt signal on its INTR input. It doesn’t affect
any other flag.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


87
PROCESSOR CONTROL INSTRUCTIONS
 External hardware synchronization instructions:
 These instructions include: HLT, WAIT, ESC, LOCK, NOP
 HLT instruction:
 The HLT instruction will cause the 8086 to stop fetching and
executing instructions.
 The 8086 will enter a halt state.
 The only ways to get the processor out of the halt state are with
an interrupt signal on the INTR pin, an interrupt signal on the
NMI pin, or a reset signal on the RESET input.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


88
PROCESSOR CONTROL INSTRUCTIONS
 WAIT-Wait for Test Signal or interrupt Signal
 When this instruction executes, the 8086 enters an idle condition
in which it is doing no processing.
 The 8086 will stay in this idle state until the 8086 TEST input pin
is made low or until an interrupt signal is received on the INTR
or the NMI interrupt input pins.
 If a valid interrupt occurs while the 8086 is in this idle state, the
8086 will return to the idle state after the interrupt service
procedure executes.
 It returns to the idle state because the address of the WAIT
instruction is the address pushed on the stack when the 8086
responds to the interrupt

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


89
PROCESSOR CONTROL INSTRUCTIONS
 ESC-Escape
 This instruction is used to pass instructions to a coprocessor, such as the
8087-math coprocessor, which shares the address and data bus with an
8086.
 Instructions for the coprocessor are represented by a 6-bit code
embedded in the escape instruction.
 As the 8086 fetches instruction bytes, the coprocessor also catches
these bytes from the data bus and puts them in its queue.
 However, the coprocessor treats all the normal 8086 instructions as
NOPs.
 When the 8086 fetches an ESC instruction, the coprocessor decodes the
instruction and carries out the action specified by the 6-bit code
specified in the instruction.
 In most cases the 8086 treats the ESC instruction as a NOP.
 In some cases the 8086 will access a data item in memory for the
coprocessor.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
90
PROCESSOR CONTROL INSTRUCTIONS
 LOCK-Assert Bus Lock Signal
 Many microcomputer systems contain several microprocessors.
 The LOCK prefix allows a microprocessor to make sure that another
processor does not take control of the system bus while it is in the
middle of a critical instruction which uses the system bus.
 The LOCK prefix is put in front of the critical instruction.
 When an instruction with a LOCK prefix executes, the 8086 will assert
its bus lock signal output.
 This signal is connected to an external bus controller device, which
then prevents any other processor from taking over the system bus.
 LOCK affects no flags.
 EXAMPLE: LOCK XCHG SEMAPHORE, AL ;
 The XCHG instruction requires two bus accesses.
 The LOCK prefix prevents another processor from taking control of the
system bus between the two accesses.
Hu, Institute Of Technology Department Of Electrical And Computer Engineering
91
PROCESSOR CONTROL INSTRUCTIONS
 NOP-Perform No Operation
 This instruction simply uses up three clock cycles and Increments
the instruction pointer to point to the next instruction.
 NOP affects no flags.
 The NOP instruction can be used to increase the delay of a delay
loop.
 When hand coding a NOP can also be used to hold a place in a
program for an instruction that will be added later.

Hu, Institute Of Technology Department Of Electrical And Computer Engineering


92

You might also like