Intel 8086 : 8086 Instruction Set , Data Transfer Instructions , Arithmetic Instructions , Bit Manipulation Instructions and String Instructions

9.5 8086 Instruction Set

The 8086 has approximately 117 different instructions with about 300 op-codes. The 8086 instruction set contains no-operand, single-operand, and two-operand instructions. Except for string instructions that involve array operations, 8086 instructions do not permit memory-to-memory operations. Appendices F and H provide 8086 instruction reference data and the instruction set (alphabetical order), respectively. The 8086 instructions can be classified into eight groups:

1. Data Transfer Instructions

2. Arithmetic Instructions

3. Bit Manipulation Instructions

4. String Instructions

5. Unconditional Transfer Instructions

6. Conditional Branch Instructions

7. Interrupt Instructions

8. Processor Control Instructions

Let us now explain some of the 8086 instructions with numerical examples. Note that

image

in the following examples , symbol ( ) is used to indicate the contents of a register or a memory location.

9.5.1 Data Transfer Instructions

Table 9.1 lists the data transfer instructions. Note that LEA is used to load 16-bit offset to a specified register; LDS and LES are similar to LEA except that they load specified register as well as DS or ES. As an example, LEA BX, 3000H has the same meaning as MOV BX,3000H. On the other hand, if (SI)=2000H, then LEA BX,4[Sl] will load 2004H into BX while MOV BX,4[SI] will initialize BX with the contents of memory locations computed from 2004H and DS. The LEA instruction can be useful when memory computation is desirable.

In Table 9.1, there are 14 data transfer instructions. These instructions move single bytes and words between a register, a memory location, or an I/0 port. Let us explain some of the instructions in Table 9.1.

LDS s I, [ 0 01OH] loads SI and DS from memory. For example, if (DS) = 2000H, (20010) = 0200H, and (20012) = 0100H, then, after LOS SI, [0010], Sl and DS will contain 0200H and 0100H, respectively.

In the 8086, the SP is decremented by 2 for PUSH and incremented by 2 for POP. For example, consider PUSH [BX]. If (DS) = 2000 16, (BX) = 020016, (SP) = 300016, (SS) = 4000 16, and (20200) = 012016, then, after execution of PUSH [BX], memory locations 42FFF and 42FFE will contain 01 16 and 20 16,respectively, and the contents of SP will be 2FFE 16XCHG has three variations: XCHG reg, reg and XCHG mem, reg or XCHG reg, mem.

For example, XCHG AX, BX exchanges the contents of 16-bit register BX with the contents of AX. XCHG mem, reg exchanges 8- or 16-bit data in mem with 8-or 16-bit reg.

XLAT can be used to employ an index in a table or for code conversion. This instruction utilizes BX to hold the starting address of the table in memory consisting of 8-bit data elements. The index in the table is assumed to be in the AL register. For example, if (BX) = 0200 16, (AL) = 0416, and (DS) = 300016, then, after XLAT, the contents of location 30204 16 will be loaded into AL. Note that the XLAT instruction is the same as MOV AL, [AL] [ BX] . As mentioned before, XLAT instruction can be used to convert from one code to another. For example, consider an 8086-based microcomputer with an ASCII keyboard connected to Port A and an EBCDIC printer connected to Port B. Suppose that it is desired to enter numerical data via the ASCII keyboard, and then print them on the EBCDIC printer. Note that numerical data entered into this computer via the keyboard will be in ASCII code. Since the printer only understands EBCDIC code, an ASCII to EBCDIC code conversion program is required. The ASCII codes for numbers 0 through 9 are 30H through 39H while the EBCDIC codes for numbers 0 to 9 are FOH to F9H (Table 2.6). The EBCDIC codes for the numbers 0 to 9 can be stored in a table starting at an offset 2030H , data can be input from the keyboard using IN AL, PORTA, convert this ASCII data to EBCDIC using XLAT instruction, and then output to Port Busing OUT PORTB, AL. The instruction sequence for the code conversion program is provided below:

imageConsider fixed port addressing, in which the 8-bit port address is directly specified as part of the instruction. IN AL, 38H inputs 8-bit data from port 38H into AL. IN AX, 38 H inputs 16-bit data from ports 38H and 39H into AX. OUT 3 8H, AL outputs the contents of ALto port 38H. OUT 3 8H, AX, on the other hand, outputs the 16-bit contents of AX to ports 38H and 39H.

For variable port addressing, the port address is 16-bit and is specified in the DX register. Assume (DX) = 312416 in all the following examples.

IN AL, DX inputs 8-bit data from 8-bit port 312416 into AL.

IN AX, DX inputs 16-bit data from ports 312416 and 312516 into AX.

OUT DX, AL outputs 8-bit data from AL into port 312416OUT DX, AX outputs 16-bit data from AX into ports 312416 and 3125 16Variable port addressing allows up to 65,536 ports with addresses from 0000H to FFFFH. The port addresses in variable port addressing can be calculated dynamically in a program. For example, assume that an 8086-based microcomputer is connected to three printers via three separate ports. Now, in order to output to each one of the printers, separate programs are required if fixed port addressing is used. However, with variable port addressing, one can write a general subroutine to output to the printers and then supply the address of the port for a particular printer in which data output is desired to register DX in the subroutine.

9.5.2 Arithmetic Instructions

Table 9.2 shows the 8086 arithmetic instructions. These operations can be performed on four types of numbers: unsigned binary, signed binary, unsigned packed decimal, and signed packed decimal numbers. Binary numbers can be 8 or 16 bits wide. Decimal numbers are stored in bytes; two digits per byte for packed decimal and one digit per byte for unpacked decimal with the high 4 bits filled with zeros.

Let us explain some of the instructions in Table 9.2.

imageFinal carry is one's complemented after subtraction to reflect the correct borrow. Hence, CF = 0. Also, PF = 1 (Even parity; number of I 'sin the result is 0 and 0 is an even number), AF =I, ZF = 1 (Zero Result), SF= 0 (Most Significant bit of the result is zero), and OF= cf EB CP =I EB I= 0.

image

image

In the above, ifSUB DL, [BX) wereused instead of CMP DL, [BX), then the number to be matched needed to be loaded after each subtraction because the contents of DL would have been lost after each SUB. Since we are only interested in the match rather than the result, CMP DL, [BX) instead of SUB DL, [BX) should be used in the above. Numerical data received by an 8086-based microcomputer from a terminal is usually in ASCII code. The ASCII codes for numbers 0 to 9 are 30H through 39H. Two 8-bit data items can be entered into an 8086-based microcomputer via a keyboard. The ASCII codes for these data items (with 3 as the upper nibble for each type) can be added. AAA instruction can then be used to provide the correct unpacked BCD. Suppose that ASCII codes for 2 (3216) and 5 (3516) are entered into an 8086-based microcomputer via a keyboard. These ASCII codes can be added and then the result can be adjusted to provide the correct unpacked BCD using the AAA instruction as follows:

image

Note that, in order to print the unpacked BCD result 0716 on an ASCII printer, (AL) = 07 can be ORed with 30H to provide 37H, the ASCII code for 7.

In case of an invalid BCD digit after addition, AAA instruction can be used to obtain correct unpacked BCD as follows:

 

image

image

The invalid BCD digit (F) in the low 4 bits of the result can be corrected by subtracting 6 from F:

image

(AH) = remainder = 0 I H

(AL) =quotient= 04H

image

Note that in the 8086, after IDIV, the sign of remainder is always the same as the dividend unless the remainder is equal to zero. Therefore, in this example, because the dividend is negative (-510), the remainder is negative (-1 10).

image

MUL CH

AAM

Note that the 8086 does not allow multiplication of two ASCII codes. Therefore, before multiplying two ASCII bytes received from a terminal, one must make the upper 4 bits of each one of these bytes zero, multiply them as two unpacked BCD digits, and then use AAM for adjustment to convert the unpacked BCD product back to ASCII by ORing the product with 3030H. The result in decimal can then be printed on an ASCII printer.

9.5.3 Bit Manipulation Instructions

The 8086 provides three groups of bit manipulation instructions. These are logicals, shifts, and rotates, as shown in Table 9.3. The operand to be shifted or rotated can be either 8- or 16-bit. Let us explain some of the instructions in Table 9.3

Consider AND BH, 8FH. If prior to execution of this instruction, (BH) = 72H, then after execution of AND BH, 8FH, the following result is obtained:

imageZF = 0 (Result is nonzero), SF = 0 (Most Significant Bit of the result is 0), PF = 0 (Result has odd parity). CF, AF, and OF are always cleared to 0 after logic operation. The status flags are similarly affected after execution of other logic instructions such as OR, XOR, NOT, and TEST.

The AND instruction can be used to perform a masking operation. If the bit value in a particular bit position is desired in a word, the word can be logically ANDed with appropriate data to accomplish this. For example, the bit value at bit 2 of an 8- bit number 0100 1Y10 (where unknown bit value of Y is to be determined) can be obtained as follows:

image

If the bit value Y at bit 2 is 1, then the result is nonzero (Flag Z=O); otherwise, the result is zero (Flag Z= 1) . The Z flag can be tested using typical conditional JUMP instructions such as JZ (Jump if Z= 1) or JNZ (Jump if Z=O) to determine whether Y

TABLE9.3image

is 0 or 1. This is called masking operation. The AND instruction can also be used to determine whether a binary number is ODD or EVEN by checking the Least Significant bit (LSB) of the number (LSB=O for even and LSB=l for odd).

image

image

image

Prior to execution of the above instruction sequence, if (DX) = 9716 and CF = 0, then after execution

of the above instruction sequence, (DX)= 25 16 and CF = I.

9.5.4 String Instructions

The word "string" means that an array of data bytes or words is stored in consecutive memory locations. String instructions are available to MOVE, COMPARE, or SCAN for a value as well as to move string elements to and from AL or AX. The instructions, listed in Table 9.4, contain "repeat" prefixes that cause these instructions to be repeated in hardware, allowing long strings to be processed much faster than if done in a software loop.

Let us explain some of the instructions in Table 9.4.

image0006,16. Assuming (1000216) = 123416, the following 8086 instruction sequence will accomplish the above:

image

Note that DS (source segment) in the MOVS instruction can be overridden while the destination segment, ES is fixed, cannot be overridden. For example, the instruction ES: MOVS WORD will override the source segment, DS byES while the destination segment remains at ES so that data will be moved in the same extra segment, ES.

image

Labels: