Академический Документы
Профессиональный Документы
Культура Документы
Instruction set
The 8086 instruction set is relatively simple and it has a total of 117 basic instructions. If
you understand all basic instructions then you can fully control the 8086. Moreover, since
Intel processors are upward compatible so your assembly language program can run on
all Intel processor including the P5 or Core2 Duo!!!!
We will go through the different groups one by one, however, for details descriptions, you
should refer to the document provided by the emu8086 software or a proper reference.
The main purpose of the data transfer instructions is to move data either between its
internal registers or between an internal register and a storage location in memory. The
most frequently used data transfer instruction is the move operation, mnemonic MOV.
Mem16 Seg-reg
The above table shows the syntax for the MOV instruction and the possible combinations
for the source and destination.
Example:
MOV AL, BL ; register to register
MOV AL, #16 ; immediate (a value 16) to register
Load effective address (LEA) is a frequently used instruction for saving the offset
address of a variable. This is similar to x = &y in C++ programming!
Syntax: LEA SI, INPUT ; effective address of INPUT will be stored in SI register
INPUT in the above example represents a variable
Since an offset address is stored so the destination must be a 16-bit register! An offset
address is a 16-bit value!!!
LEA usually is used to access data defined in the program, as shown below.
Example
As its name implies, LDS will load a specified register and the DS register in a single
operation!
Syntax: LDS SI, [200] ; 4 bytes of data will be fetched, the first 2 bytes (location
200 and 201) stored in SI and the following 2 bytes (locations 202 and 203) stored in
DS.
There is also the load register and ES (LES). Similar to LDS, LES will load a specific
register and the ES register.
Arithmetic instructions
Packed decimal – two BCD digits are packed into a byte register or memory location.
Each BCD is 4 bits representing digit 0 to 9.
Unpacked decimal numbers are stored one BCD digit per byte
After an arithmetic operation, the flags (such as Carry, Sign, Overflow, Zero) are updated
accordingly. Based on the flag status, different operations can be performed, similar to an
IF ELSE syntax.
Table 1 Arithmetic operation of 8086
ADC Add with carry ADC D, S D = S+D+CF OF, SF, ZF, AF, PF, CF
CF = carry
DAA Decimal adjust for DAA SF, ZF, AF, PF, CF, OF
addition undefined
Destination Source
Register Register
Register Memory
Memory Register
Register Immediate
Memory Immediate
AX Immediate
Example
AL = 12H, BL = 70H
Carry = 1
ADC AL, BL
Then result is in AL = 83H
The carry is only 1-bit and it is being added to the LSB (least significant bit).
Example
BX = 1234 CX= 0123
Carry is 0
SBB BX, CX ; subtract with borrow
Give 1111
Subtraction
Destination Source
Register Register
Register Memory
Memory Register
AX Immediate
Register Immediate
Memory immediate
If BX = 3A
NEG BX
Give FFC6
Multiplication
When multiplying 8-bit data, result is 16-bit and stored in AX (a 16-bit register).
When multiplying two 16-bit data, result is 32-bit and result stored in AX and DX.
The AX holds the LSW (Least significant word) and DX stores the MSW (Most
significant word). Only the source operand is specified in the multiply operation and
the other operand must be stored in AL, or AX. This also applies in the division
operation.
In 8-bit division, the result is stored in AH and AL. Where AH is the remainder and AL is
the quotient.
For 16-bit division, AX contains the quotient and DX contains the remainder
Special functions
CBW – convert byte (8-bit) to word (16-bit). So what is the additional 8 bits?
CBW – fill AH with 0 if AL is positive; fill AH with 1s if AL is negative then AH
becomes FF. In doing so, the sign of the original data will be retained.
CWD – convert word (16-bit) to double word (32-bit) and the DX register is used to store
the high order word.
CWD – fill DX with 0 if AL is positive; fill DX with 1s if AX is negative then DX
becomes FF
Both CBW and CWD do not require operand, the data must be stored in AL or AX.
Ans.
AX = FFA1 after CBW ; AL 8-bit converts to 16-bit AX
DX = FFFF after CWD ; AX is 16-bit converts to 32-bit (DX + AX)
Multiplication and division functions
S, Z, A, P undefined
DIV Division DIV S AL = Q (AX/S8)
(unsigned) AH = R(AX/S8) OF, CF, SF, ZF, AF, PF
all undefined
IMUL Integer IMUL S AX = AL*S8 OF, CF depends on
multiply DX,AX=AX*S16 result
(signed) S, Z, A, P undefined
IDIV Integer divide IDIV S AX = Q(AX/S8) OF, CF, SF, ZF, AF, PF
(signed) AH=R(AX/S8) all undefined
AX = Q(DX,AX)/S16
DX=R(DX,AX)/S16
If Q is positive and
exceeds 7FFF or if Q
is negative and
becomes less than
8001 then type 0
interrupt occurs
Logic instructions
Logic operations include: AND, OR, exclusiveOR (XOR) and NOT
Example:
MOV AL, 10101100B
AND AL, 11110000B ; bit by bit operation
AL 1 0 1 0 1 1 0 0
1 1 1 1 0 0 0 0
AND 1 0 1 0 0 0 0 0
Ans. AL = 00100101
Shift instructions
As its name implies, shift instructions are for shifting the bit(s) out of the operand. So
what will happen to the bit shifted out? What will happen to the vacated bit position?
There are 4 shift operations
Can perform: logical shift and arithmetic shift
Can shift left as well as right
Logical shift – the vacated bit will be filled with ‘0’ and the bit shifted out will be stored
in the Carry flag.
Arithmetic shift – the vacated bit will be filled with the original most significant bit (this
only applies in right shift, in left shift vacated bit is filled with ‘0’ as well.)
Original 0 0 0 0 0 0 0 1
Shifted 0 0 0 0 0 0 1 0
1 bit
left
On the other hand, shift to the right is the same as divide the value by 2. For
example, if the original value is 2 then shift to the right by 1 bit, the value becomes 1.
Shift operations can be used in multiply and divide operations.
Bits being shifted
Shift Right
Example
Mov AL, 10001100b
MOV CL, #1
SHR AL, CL ; shift right 1 time
Rotate instructions
Rotate is similar to shift but the vacate bit is filled with the bit rotated out from the other
side.
Rotate left through carry (RCL) – bit rotated out is stored in the carry flag, the vacated bit
is filled by the carry flag
Carry
Rotate operations
ROL Rotate left ROL D, Count Rotate the D left by the C, O depends
number of bit positions on result
equal to Count. Each bit
shifted out from the OF = 0 if first
leftmost bit goes back operand keeps
into the rightmost bit original sign
position
ROR Rotate ROR D, Count Rotate the D right by C, O depends
Right the number of bit on result
positions equal to
Count. Each bit shifted OF = 0 if first
out from the rightmost operand keeps
bit goes back into the original sign
leftmost bit position
Exercise
1. How would the decimal number 1234 be coded in ASCII and stored in memory
starting at address 0C000H? Assume that least significant digit is stored at the lower
addressed memory location
Ans. 1 = (31H in 0C003H) 2 = (32H in 0C002H) 3 = (33H in 0C001H) 4 = (34H in
0C000H)
2. If register BX contains the value 0100H, register DI contains 0010 and register
DS contains 1075, what physical memory location is swapped when the following
instruction is executed
XCHG [BX+DI], AX
PA = 10750+0100+0010 = 10860H
3. Compute the physical address for the specified operand in each of the following
instructions:
MOV [DI], AX (destination operand) (0B000+0200 = 0B200)
MOV DI, [SI] (source operand) (0B000+0100 = 0B100)
MOV DI+XYZ, AH (destination operand) (0B000+0200+0400 = 0B600)
Given CS=0A00, DS=0B00, SI=0100, DI=0200, BX=0300, XYZ=0400