8086 Instr ns tru u ctio ct ion n Forma or matt & Ad A d d r ess es s i n g Mod Mo d es
Instruction Format 1 byte AAA, CMC, DAA, DAS 2 byte
DEC 8bit reg, ADD AL,07H
3 byte
MOV 43H[SI], DH
4 byte
MOV AH,[2000]
5 byte
Direct Intersegment Call, JMP
6 byte ADC (Immediate to Memory)
Instruction Format • • • • • • • • • • • •
An instruction can be coded with 1 to 6 bytes •Byte 1 contains three kinds of information – Opcode field (6 bits) specifies the operation (add, subtract, move) –Register Direction Bit (D bit) Tells the register operand in REG field in byte 2 is source or destination operand 1: destination 0: source -Data Size Bit (W bit) Specifies whether the operation will be performed on 8-bit or 16-bit data 0: 8 bits 1: 16 bits
• •
An instruction can be coded with 1 to 6 bytes Byte 1 contains three kinds of information – Opcode field (6 bits) specifies the operation (add, subtract, move) – Register Direction Bit (D bit) Tells the register operand in REG field in byte 2 is source or destination operand • 1: destination • 0: source – Data Size Bit (W bit) Specifies whether the operation will be performed on 8-bit or 16-bit data • 0: 8 bits • 1: 16 bits
•
Byte 2 has three fields – Mode field (MOD) – Register field (REG) used to identify the register for the first operand – Register/memory field (R/M field)
Mod/Displacement •
00 - If r/m is 110, Displacement (16 bits) is address; otherwise, no displacement
•
01 - Eight-bit displacement, sign-extended to 16 bits
•
10 -16-bit displacement (example: MOV [BX + SI]+ displacement,al)
•
11 - r/m is treated as a second "reg" field
r/m
Operand address
000
(BX) + (SI) + displacement (0, 1 or 2 bytes long)
001
(BX) + (DI) + displacement (0, 1 or 2 bytes long)
010
(BP) + (SI) + displacement (0, 1 or 2 bytes long)
011
(BP) + (DI) + displacement (0, 1 or 2 bytes long)
100
(SI) + displacement (0, 1 or 2 bytes long)
101
(DI) + displacement (0, 1 or 2 bytes long)
110
(BP) + displacement unless mod = 00 (see mod table)
111
(BX) + displacement (0, 1 or 2 bytes long)
Example: Immediate operand • Now, if we were to want to use an immediate operand, as follows: XOR CL, 12H • In this case, because there are no square brackets, 12H is immediate: it is the number we are going to XOR against. • The opcode for an immediate XOR is 1000000w; in this case, we are using a byte, so w is 0. So our first byte is (10000000b). • The second byte, for an immediate operation, takes the form "mod 110 r/m". Since the destination is a register, mod is 11, making the r/m field a register value. We already know that the register value for CL is 001, so our second byte is (11 110 001b). • The third byte (and fourth byte, if this were a word operation) are the immediate data. As it is a byte, there is only one byte of data, 12H = (00010010b). • All together, then: • XOR CL, 12H = 10000000 11110001 00010010 = 80H F1H 12H
Addressing Modes 1.
Immediate Addressing 1. This is when a constant value is moved into a register or memory location. 2. It is not really an address since it does not point to any location within the memory or CPU. 3. Immediate addressing can only be used for the source since immediate values are not themselves stored anywhere; during assembly of the program, the immediate value becomes part of the machine code instruction. 4. example: mov ax,10h
2. Register Addressing 1. 2. 3.
A register can be used as both the source and destination of the instruction. Registers are very fast for most operations so maximum use must be made thereof. Examples: mov ax,bx mov ax,10h mov si,es:[bx]
3.Direct Memory Addressing 1.
A memory location can be used by using its address as the operand in an instruction.
2.
example: mov ax,aDigit
Direct (uses default segment, DS)
4.Register Indirect Addressing 1.
Instead of specifying the memory location as an operand, the memory location can be stored in a register.
2.
Then this register must be specified to access the memory.
3.
For indirect addressing, the 8086 can only use the BX, BP, SI and DI registers.
4.
example: mov ax,[bx]
5.Indexed Addressing Mode 1.
Offset of the operand is stored in one of the index registers.
2.
DS & ES are the default segment registers for SI & DI
3.
Example:
MOV AX,[SI]
6. Register Relative Addressing 1. A possible combination of direct and indirect addressing techniques could be when an indirect address is specified as well as an offset from that value. 2. To specify base relative addressing, the programmer must indicate the base register and displacement/offset as a sum. 3. examples: mov ax,[bx+4] mov ax,[bx]+4 mov ax,4[bx] 4. All these instructions will use the same address, which is 4 more than the address stored in the bx register. 5. The only registers allowed are BX and BP (the so-called "base“ registers). This technique can also called Direct Indexed Addressing, when it utilises the SI and DI registers (the so-called "index" registers).
7 .Relative Base Indexed Add ressin g • • • • • •
This is a combination of base register and direct indexed addressing. Instead of specifying the address as being stored in one register, the CPU can add the values stored in two registers to get the effective address. Of course, an offset can also be specified. Since this technique uses a base register and an index register, there are only four allowed combinations of registers. example: mov ax,[bx+si+4] mov ax,[bx][si]+4 mov ax,[bx][si+4] All these instructions will use the same address. If BX=4 and SI=8, then the address will be 4+8+4 =16 (decimal) = 10h. Base indexed addressing is useful for indexing two-dimensional arrays, whereas the other methods are used for the simpler one-dimensional cases.