8086 Instructions
8086 Instructions
8086 Instructions
A series of data byte or word available in memory at consecutive locations, to be referred as Byte
String or Word String. A String of characters may be located in consecutive memory locations,
where each character may be represented by its ASCII equivalent.
The 8086 supports a set of more powerful instructions for string manipulations for referring to a
string, two parameters are required.
The length of the string is usually stored as count in the CX register.The incrementing or
decrementing of the pointer, in string instructions, depends upon the Direction Flag (DF) Status.
If it is a Byte string operation, the index registers are updated by one. On the other hand, if it is a
word string operation, the index registers are updated
by two.
This instruction is used as a prefix to other instructions, the instruction to which the REP prefix
is provided, is executed repeatedly until the CX register becomes zero (at each iteration CX is
automatically decremented by one).
These are used for CMPS, SCAS instructions only, as instruction prefixes.
The CMPS instruction can be used to compare two strings of byte or words. The length of the
string must be stored in the register CX. If both the byte or word strings are equal, zero Flag is
set.The REP instruction Prefix is used to repeat the operation till CX (counter) becomes zero or
the condition specified by the REP Prefix is False.
This instruction scans a string of bytes or words for an operand byte or word specified in the
register AL or AX. The String is pointed to by ES:DI register pair. The length of the string s
stored in CX. The DF controls the mode for scanning of the string. Whenever a match to the
specified operand, is found in the string, execution stops and the zero Flag is set. If no match is
found, the zero flag is reset.
LODS : Load String Byte or String Word
The LODS instruction loads the AL / AX register by the content of a string pointed to by DS : SI
register pair. The SI is modified automatically depending upon DF, If it is a byte transfer
(LODSB), the SI is modified by one and if it is a word transfer (LODSW), the SI is modified by
two. No other Flags are affected by this instruction.
The STOS instruction Stores the AL / AX register contents to a location in the string pointer by
ES : DI register pair. The DI is modified accordingly, No Flags are affected by this instruction.
The direction Flag controls the String instruction execution, The source index SI and Destination
Index DI are modified after each iteration automatically. If DF=1, then the execution follows
autodecrement mode, SI and DI are decremented automatically after each iteration. If DF=0, then
the execution follows autoincrement mode. In this mode, SI and DI are incremented
automatically after each iteration.
BCD Arithmetic
Two arithmetic techniques operate with BCD data: addition and subtraction. The instruction set
provides two instructions that correct the result of a BCD addition and a BCD subtraction. The
DAA (decimal adjust after addition) instruction follows BCD addition, and the DAS (decimal
adjust after subtraction) follows BCD subtraction. Both instructions correct the result of the
addition or subtraction so that it is a BCD number.
For BCD data, the numbers always appear in the packed BCD form and are stored as two
BCD digits per byte. The adjustment instructions function only with the AL register after BCD
addition and subtraction.
DAA Instruction. The DAA instruction follows the ADD or ADC instruction to adjust the result
into a BCD result. Suppose that DX and BX each contain 4-digit packed BCD numbers.
Example 518 provides a short sample program that adds the BCD numbers in DX and BX, and stores
the result in CX.
EXAMPLE 518
0000 BA 1234 MOV DX,1234H ;load 1234 BCD
0003 BB 3099 MOV BX,3099H ;load 3099 BCD
0006 8A C3 MOV AL,BL ;sum BL and DL
0008 02 C2 ADD AL,DL
000A 27 DAA
000B 8A C8 MOV CL,AL ;answer to CL
000D 9A C7 MOV AL,BH ;sum BH, DH and carry
000F 12 C6 ADC AL,DH
0011 27 DAA
0012 8A E8 MOV CH,AL ;answer to CH
Because the DAA instruction functions only with the AL register, this addition must
occur 8 bits at a time. After adding the BL and DL registers, the result is adjusted with a DAA
instruction before being stored in CL. Next, add BH and DH registers with carry; the result is
then adjusted with DAA before being stored in CH. In this example, a 1234 is added to 3099 to
generate a sum of 4333, which moves into CX after the addition. Note that 1234 BCD is the
same as 1234H.
DAS Instruction. The DAS instruction functions as does the DAA instruction, except that it
follows
a subtraction instead of an addition. Example 5-19 is the same as Example 518, except that
it subtracts instead of adds DX and BX. The main difference in these programs is that the DAA
instructions change to DAS, and the ADD and ADC instructions change to SUB and SBB
instructions.
EXAMPLE 519
0000 BA 1234 MOV DX,1234H ;load 1234 BCD
0003 BB 3099 MOV BX,3099H ;load 3099 BCD
0006 8A C3 MOV AL,BL ;subtract DL from BL
0008 2A C2 SUB AL,DL
000A 2F DAS
000B 8A C8 MOV CL,AL ;answer to CL
000D 9A C7 MOV AL,BH ;subtract DH
000F 1A C6 SBB AL,DH
0011 2F DAS
0012 8A E8 MOV CH,AL ;answer to CH
ASCII Arithmetic
The ASCII arithmetic instructions function with ASCII-coded numbers. These numbers range in
value from 30H to 39H for the numbers 09. There are four instructions used with ASCII
arithmetic
operations: AAA (ASCII adjust after addition), AAD (ASCII adjust before division),
AAM (ASCII adjust after multiplication), and AAS (ASCII adjust after subtraction). These
instructions use register AX as the source and as the destination.
AAA Instruction. The addition of two one-digit ASCII-coded numbers will not result in any
useful data. For example, if 31H and 39H are added, the result is 6AH. This ASCII addition
( ) should produce a two-digit ASCII result equivalent to a 10 decimal, which is a 31H and
a 30H in ASCII code. If the AAA instruction is executed after this addition, the AX register will
contain a 0100H. Although this is not ASCII code, it can be converted to ASCII code by adding
3030H to AX which generates 3130H. The AAA instruction clears AH if the result is less than
10,
and adds 1 to AH if the result is greater than 10.
EXAMPLE 520
0000 B8 0031 MOV AX,31H ;load ASCII 1
0003 04 39 ADD AL,39H ;add ASCII 9
0005 37 AAA ;adjust sum
0006 05 3030 ADD AX,3030H ;answer to ASCII
Example 520 shows the way ASCII addition functions in the microprocessor. Please note
that AH is cleared to zero before the addition by using the MOV AX,31H instruction. The
operand of 0031H places 00H in AH and 31H into AL.
AAD Instruction. Unlike all other adjustment instructions, the AAD instruction appears before
a division. The AAD instruction requires that the AX register contain a two-digit unpacked BCD
number (not ASCII) before executing. After adjusting the AX register with AAD, it is divided
bExample 521 illustrates how 72 in unpacked BCD is divided by 9 to produce a quotient
of 8. The 0702H loaded into the AX register is adjusted by the AAD instruction to 0048H.
Notice that this converts a two-digit unpacked BCD number into a binary number so it can be
divided with the binary division instruction (DIV). The AAD instruction converts the unpacked
BCD numbers between 00 and 99 into binary.
EXAMPLE 521
0000 B3 09 MOV BL,9 ;load divisor
0002 B8 0702 MOV AX,702H ;load dividend
0005 D5 0A AAD ;adjust
0007 F6 F3 DIV BL ;divide
AAM Instruction. The AAM instruction follows the multiplication instruction after multiplying
two one-digit unpacked BCD numbers. Example 522 shows a short program that multiplies 5
times 5. The result after the multiplication is 0019H in the AX register. After adjusting the result
with the AAM instruction, AX contains 0205H. This is an unpacked BCD result of 25. If 3030H
is added to 0205H, it has an ASCII result of 3235H.
EXAMPLE 522
0000 B0 05 MOV AL,5 ;load multiplicand
0002 B1 03 MOV CL,3 ;load multiplier
0004 F6 E1 MUL CL
0006 DA 0A AAM ;adjust
The AAM instruction accomplishes this conversion by dividing AX by 10. The remainder
is found in AL, and the quotient is in AH. Note that the second byte of the instruction contains
0AH. If the 0AH is changed to another value, AAM divides by the new value. For example, if the
second byte is changed to 0BH, the AAM instruction divides by 11. This is accomplished with
DB 0D4H, 0BH in place of AAM, which forces the AMM instruction to multiply by 11.
One side benefit of the AAM instruction is that AAM converts from binary to unpacked
BCD. If a binary number between 0000H and 0063H appears in the AX register, the AAM
instruction converts it to BCD. For example, if AX contains a 0060H before AAM, it will contain
0906H after AAM executes. This is the unpacked BCD equivalent of 96 decimal. If 3030H
is added to 0906H, the result changes to ASCII code.
Example 523 shows how the l6-bit binary content of AX is converted to a four-digit
ASCII character string by using division and the AAM instruction. Note that this works for numbers
between 0 and 9999. First DX is cleared and then DXAX is divided by 100. For example,
ifAX = 24510 , AX = 2 andDX = 45 after the division. These separate halves are converted to
BCD using AAM, and then 3030H is added to convert to ASCII code.
EXAMPLE 523
0000 33 D2 XOR DX,DX ;clear DX
0002 B9 0064 MOV CX,100 ;divide DX-AX by 100
0005 F7 F1 DIV CX
0007 D4 0A AAM ;convert to BCD
0009 05 3030 ADD AX,3030H ;convert to ASCII
000C 92 XCHG AX,DX ;repeat for remainder
000D D4 0A AAM
000F 05 3030 ADD AX,3030H
Example 524 uses the DOS 21H function AH=02H to display a sample number in decimal
on the video display using the AAM instruction. Notice how AAM is used to convert AL
into BCD. Next, ADD AX,3030H converts the BCD code in AX into ASCII for display with
DOS INT 21H. Once the data are converted to ASCII code, they are displayed by loading DLwith the
most significant digit from AH. Next, the least significant digit is displayed from AL.
Note that the DOS INT 2lH function calls change AL.
AAS Instruction. Like other ASCII adjust instructions, AAS adjusts the AX register after an
ASCII subtraction. For example, suppose that 35H subtracts from 39H. The result will be 04H,
which requires no correction. Here, AAS will modify neither AH nor AL. On the other hand, if
38H is subtracted from 37H, then AL will equal 09H and the number in AH will decrement by 1.
This decrement allows multiple-digit ASCII numbers to be subtracted from each other.