There are 117 basic instructions in the instruction set of 8086. The instruction set of 8086 can be divided into the following number of groups, namely:
- Data copy / Transfer instructions
- Arithmetic and Logical instructions
- Branch instructions
- Loop instructions
- Machine control instructions
- Flag Manipulation instructions
- Shift and Rotate instructions
- String instructions
1. Data copy / Transfer instructions:
The data movement instructions copy values from one location to another. These instructions include MOV, XCHG, LDS, LEA, LES, PUSH, PUSH, PUSHFD, POP, POPF, LAHF, AND SAHF.
MOV The MOV instruction copies a word or a byte of data from a source to a destination. The destination can be a register or a memory location. The source can be a register, memory location, or immediate data. MOV instruction does not affect any flags. The move instruction takes several different forms: The MOV instruction cannot:
- Set the value of the CS and IP registers.
- Copy the value of one segment register to another segment register (should copy to the general register first). MOV CS, DS (Invalid)
- Copy immediate value to segment register (should copy to general register first). MOV CS, 2000H (Invalid)
Example:
ORG 100h
MOV AX, 0B800h ; set AX = B800h
MOV DS, AX; copy value of AX to DS.
MOV CL, 'A' ; CL = 41h (ASCII code).
2. The XCHG Instruction:
Exchange This instruction exchanges the contents of the specified source and destination operands, which may be registered, or one of them, maybe a memory location. However, the exchange of data contents of two memory locations is not permitted.
Example: MOV AL, 5 ; AL = 5 MOV BL, 2 ; BL = 2
XCHG AL,BL ; AL = 2, BL = 5
PUSH:
Push to stack; this instruction pushes the contents of the specified register/memory location onto the stack. The stack pointer is decremented by 2, after each execution of the instruction. The actual current stack-top is always occupied by the previously pushed data. Hence, the push operation decrements SP by two and then stores the two-byte contents of the operand onto the stack. The higher byte is pushed first and then the lower byte. Thus out of the two decremented stack addresses, the higher byte occupies the higher address and the lower byte occupies the lower address.
PUSH AX
PUSH DS
PUSH [500OH]; Content of location 5000H and 5001 H in DS are pushed onto the stack.
POP: Pop from Stack this instruction when executed loads the specified register/memory location with the contents of the memory location of which the address is formed using the current stack segment and stack pointer as usual. The stack pointer is incremented by 2. The POP instruction serves exactly the opposite of the PUSH instruction.
- POP BX
- POP DS
- POP [5000H]
PUSHF: Push Flags to Stack The push flag instruction pushes the flag register onto the stack; first the upper byte and then the lower byte will be pushed onto the stack. The SP is decremented by 2, for each push operation. The general operation of this instruction is similar to the PUSH operation.
POPF: Pop Flags from Stack The pop flags instruction loads the flag register completely (both bytes) from the word contents of the memory location currently addressed by SP and SS. The SP is incremented by 2 for each pop operation.
LAHF: Load AH from Lower Byte of Flag This instruction loads the AH register with the lower byte of the flag register. This instruction may be used to observe the status of all the condition code flags (except overflow) at a time.
SAHF: Store AH to Lower Byte of Flag Register This instruction sets or resets the condition code flags (except overflow) in the lower byte of the flag register depending upon the corresponding bit positions in AH. If a bit in AH is 1, the flag corresponding to the bit position is set, else it is reset.
LEA: Load Effective Address The load effective address instruction loads the offset of an operand in the specified register. This instruction is similar to MOV, MOV is faster than LEA.
LEA cx, [bx+si] ; CX (BX+SI) mod 64K If bx=2f00 H; si=10d0H cx 3fd0H
The LDS AND LES instructions:
- LDS and LES load a 16-bit register with an offset address retrieved from a memory location then load either DS or ES with a segment address retrieved from memory. This instruction transfers the 32-bit number, addressed by DI in the data segment, into the BX and DS registers.
- LDS and LES instructions obtain a new far address from memory.– offset address appears first, followed by the segment address
- This format is used for storing all 32-bit memory addresses.
- A far address can be stored in memory by the assembler.
LDS BX,DWORD PTR[SI] BL
[SI];
BH [SI+1]
DS [SI+3:SI+2]; in the data segment
LES BX,DWORD PTR[SI] BL [SI];
BH [SI+1]
ES [SI+3:SI+2]; in the extra segment
I/O Instructions: The 80x86 supports two I/O instructions: in and out15. They take the forms: In ax, port
an ax, dx out port, ax out dx, ax
port is a value between 0 and 255.
The instruction reads the data at the specified I/O port and copies it into the accumulator. The out instruction writes the value in the accumulator to the specified I/O port.
Arithmetic instructions: These instructions usually perform the arithmetic operations, like addition, subtraction, multiplication, and division along with the respective ASCII and decimal adjust instructions. The increment and decrement operations also belong to this type of instruction.
The ADD and ADC instructions: The add instruction adds the contents of the source operand to the destination operand. For example, add ax, bx adds bx to ax leaving the sum in the ax register. Add computes dest:=dest+source while ADC computes dest:=dest+source+C where C represents the value in the carry flag. Therefore, if the carry flag is clear before execution, and behaves exactly like the add instruction.
Both instructions affect the flags identically. They set the flags as follows:
- The overflow flag denotes a signed arithmetic overflow.
- The carry flag denotes an unsigned arithmetic overflow.
- The sign flag denotes a negative result (i.e., the H.O. bit of the result is one).
- The zero flag is set if the result of the addition is zero.
- The auxiliary carry flag contains one if a BCD overflow out of the L.O. nibble occurs.
- The parity flag is set or cleared depending on the parity of the L.O. eight bits of the result. If there is an even number of one bit in the result, the ADD instructions will set the parity flag to one (to denote even parity). If there is an odd number of one bit in the result, the ADD instructions clear the parity flag (to denote odd parity).
The INC instruction: The increment instruction adds one to its operand. Except for carrying flag, inc sets the flags the same way as Add ax, 1 same as inc ax. The inc operand may be an eight-bit, sixteen-bit. The inc instruction is more compact and often faster than the comparable add reg, 1 or adds mem, 1 instruction.
The AAA and DAA Instructions
The aaa (ASCII adjust after addition) and data (decimal adjust for addition) instructions support BCD arithmetic. BCD values are decimal integers coded in binary form with one decimal digit(0..9) per nibble. ASCII (numeric) values contain a single decimal digit per byte, the H.O. nibble of the byte should contain zero (30 ….39).
The aaa and data instructions modify the result of a binary addition to correct it for ASCII or decimal arithmetic. For example, to add two BCD values, you would add them as though they were binary numbers and then execute the data instruction afterward to correct the results.
Note: These two instructions assume that the add operands were proper decimal or ASCII values. If you add binary(non-decimal or non-ASCII) values together and try to adjust them with these instructions, you will not produce correct results.
Aaa (which you generally execute after an add, ADC, or add instruction) checks the value in all for BCD overflow. It works according to the following basic algorithm:
if ( (al and 0Fh) > 9 or (AuxC =1) ) then al := al + 6 else
ax := ax +6 endif ah :=
ah + 1
Alex := 1 ;Set Auxilliary carry Carry := 1 ; and carry flags. Else
Alex := 0 ;Clear Auxilliary carry Carry := 0 ; and carry flags.
add al=08 +06; al=0E >9 al=0E+06=04
ah=00+01=01
al=04+03=08, now al<9,
so only clear ah=0
endif
al:= al and 0Fh
The aaa instruction is mainly useful for adding strings of digits where there is exactly one decimal digit per byte in a string of numbers.
The data instruction functions like aaa except it handle packed BCD values rather than the one digit per byte unpacked values aaa handles. As for aaa, DAA's main purpose is to add strings of BCD digits
(with two digits per byte). The algorithm for data is
if ( (AL and 0Fh) > 9 or (AuxC = 1)) then
al=24+77=9B, as B>9 add 6 to al
al := al + 6 al=9B+06=A1, as higher nibble A>9, add 60
Alex:= 1; Set Auxiliary carry. to al, al=A1+60=101
Endif
Note: if higher or lower nibble of AL <9 then
if ( (al > 9Fh) or (Carry = 1)) then no need to add 6 to AL
al := al + 60h
Carry := 1; ;Set carry flag.
Endif
EXAMPLE:
Assume AL = 0 0 1 1 0 1 0 1, ASCII 5 BL
= 0 0 1 1 1 0 0 1, ASCII 9
ADDAL,BL Result: AL= 0 1 1 0 1 1 1 0 = 6EH,which is incorrect BCD AAA Now AL = 00000100, unpacked BCD 4.
CF = 1 indicates answer is 14 decimal
NOTE: OR AL with 30H to get 34H, the ASCII code for 4. The AAA instruction works only on the AL register. The AAA instruction updates AF and CF, but OF, PF, SF, and ZF are left undefined.
EXAMPLES:
AL = 0101 1001 = 59 BCD ; BL = 0011 0101 = 35 BCD ADD AL, BL AL = 1000 1110 = 8EH
DAA Add 01 10 because 1110 > 9 AL = 1001 0100 = 94 BCD AL
= 1000 1000 = 88 BCD BL = 0100 1001 = 49 BCD ADD AL, BL AL = 1101 0001, AF=1
DAA Add 0110 because AF =1, AL = 11101 0111 = D7H
1101 > 9 so add 0110 0000
AL = 0011 0111= 37 BCD, CF =1
The DAA instruction updates AF, CF, PF, and ZF. OF is undefined after a DAA instruction.
The SUBTRACTION instructions: SUB, SBB, DEC, AAS, and DAS
The sub-instruction computes the value dest:=dest - src. The SBB instruction computes dest:=dest src - C.
The sub, sub, and dec instructions affect the flags as follows:
- They set the zero flag if the result is zero. This occurs only if the operands are equal for sub and sub. The dec instruction sets the zero flag only when it decrements the value one.
- These instructions set the sign flag if the result is negative.
- These instructions set the overflow flag if signed overflow/underflow occurs.
- They set the auxiliary carry flag as necessary for BCD/ASCII arithmetic.
- They set the parity flag according to the number of bit appearing in the result value.
- The sub and sbb instructions set the carry flag if an unsigned overflow occurs. Note that the dec instruction does not affect the carry flag.
The aas instruction, like its aaa counterpart, lets you operate on strings of ASCII numbers with one decimal digit (in the range 0..9) per byte. This instruction uses the following algorithm:
if ( (al and 0Fh) > 9 or AuxC = 1) then al
:= al - 6
ah := ah - 1
Alex := 1 ;Set Auxilliary carry Carry := 1 ; and carry flags. else
Alex := 0 ;Clear Auxilliary carry Carry := 0 ; and carry flags.
endif
al:= al and 0Fh
The das instruction handles the same operation for BCD values, it uses the following algorithm:
if ( (al and 0Fh) > 9 or (AuxC = 1)) then al
:= al -6
AuxC = 1 endif
if (al > 9Fh or Carry = 1) then al
:= al - 60h
Carry := 1 ;Set the Carry flag. Endif
EXAMPLE:
ASCII 9-ASCII 5 (9-5)
AL = 00111001 = 39H = ASCII 9 BL = 001 10101 = 35H = ASCII 5
SUB AL, BL Result: AL = 00000100 = BCD 04 and CF = 0 AAS Result: AL = 00000100 = BCD 04 and CF = 0
no borrow required ASCII 5-ASCII 9 (5-9)
Assume AL = 00110101 = 35H ASCII 5 and BL = 0011 1001 = 39H = ASCII 9
SUB AL, BL Result: AL = 11111100 = - 4 in 2s complement and CF =1 AAS Result: AL = 00000100 = BCD 04 and CF = 1, borrow needed
EXAMPLES:
AL 1000 0110 86 BCD ; BH 0101 0111 57 BCD
SUB AL,BH AL 0010 1111 2FH, CF = 0
DAS Lower nibble of result is 1111, so DAS automatically
subtracts 0000 0110 to give AL = 00101001 29 BCD AL
0100 1001 49 BCD BH 0111 0010 72 BCD SUB AL,BH
AL 1101 0111 D7H, CF = 1
DAS Subtracts 0110 0000 (- 60H) because 1101 in upper nibble > 9 AL = 01110111= 77 BCD, CF=1 CF=1 means borrow was needed
The CMP Instruction: The CMP (compare) instruction is identical to the sub instruction with one crucial difference– it does not store the difference back into the destination operand. The syntax for the CMP instruction is very similar to sub, the generic form is modest, src
Consider the following CMP instruction: CMP ax, bx
This instruction performs the computation ax-box and sets the flags depending upon the result of the computation. The flags are set as follows:
Z: The zero flag is set if and only if ax = box. This is the only time ax-bx produces a zero result. Hence, you can use the zero flag to test for equality or inequality.
S: The sign flag is set to one if the result is negative.
O: The overflow flag is set after a CMP operation if the difference of ax and be produced an overflow or underflow.
C: The carry flag is set after a CMP operation if subtracting bx from ax requires a borrow. This occurs only when the ax is less than bx where ax and bx are both unsigned values.
The Multiplication Instructions: MUL, IMUL, and AAM: This instruction multiplies an unsigned byte or word by the contents of AL. The unsigned byte or word may be in any one of the general-purpose registers or memory locations. The most significant word of the result is stored in DX, while the least significant word of the result is stored in AX.
The mul instruction, with an eight-bit operand, multiplies the al register by the operand and stores the 16-bit result in ax. So
mul operand (Unsigned) MUL BL i.e. AL * BL; Al=25 * BL=04; AX=00 (AH) 64 (AL) imul operand (Signed) IMUL BL i.e. AL * BL; AL=09 * BL=-2; AL * 2’s comp(BL)
AL=09 * BL (0EH)=7E; 2’s comp (7e)=-82
The aam (ASCII Adjust after Multiplication) instruction, adjust an unpacked decimal value after multiplication. This instruction operates directly on the ax register. It assumes that you’ve multiplied two eight bit values in the range 0..9 together and the result is sitting in ax (actually, the result will be sitting in al since 9*9 is 81,the largest possible value; ah must contain zero). This instruction divides ax by 10 and leaves the quotient in ah and the remainder in al: mul bl; al=9, bl=9 al*bl=9*9=51H; AX=00(AH) 51(AL); AAM ; first hexadecimal value is converted to decimal value i.e. 51 to 81; al=81D; second convert packed BCD to unpacked BCD, divide AL content by 10 i.e. 81/10 then AL=01, AH =08; AX = 0801
EXAMPLE:
AL 00000101 unpacked BCD 5 BH
00001001 unpacked BCD 9 MUL BH AL x BH; result in AX
AX = 00000000 00101101 = 002DH
AAM AX = 00000100 00000101 = 0405H, which is unpacked BCD for 45.
If ASCII codes for the result are desired, use next instruction OR AX, 3030H Put 3 in upper nibble of each byte.
AX = 0011 0100 0011 0101 = 3435H, which is ASCII code for 45
The Division Instructions: DIV, IDIV, and AAD
The 80x86 divide instructions perform a 64/32 division (80386 and later only), a 32/16division or a 16/8 division. These instructions take the form:
Div reg For unsigned division Div mem
Idiv reg For signed division Idiv mem
The div instruction computes an unsigned division. If the operand is an eight bit operand ,div divides the ax register by the operand leaving the quotient in al and the remainder(modulo) in ah. If the operand is a 16 bit quantity, then the div instruction divides the 32 bit quantity in dx ax by the operand leaving the quotient in ax and the remainder in .
Note: If an overflow occurs (or you attempt a division by zero) then the80x86 executes an INT 0 (interrupt zero).
The aad (ASCII Adjust before Division) instruction is another unpacked decimal operation. It splits apart unpacked binary coded decimal values before an ASCII division operation. The aad instruction is useful for other operations. The algorithm that describes this instruction is
al := ah*10 + al AX=0905H; BL=06; AAD; AX=AH*10+AL=09*10+05=95D;
convert decimal to hexadecimal; 95D=5FH; al=5f; DIV BL; AL/BL=5F/06; AX=05(AH)0F(AL) ah := 0
EXAMPLE:
AX = 0607H unpacked BCD for 67 decimal CH = 09H, now adjust to binary
AAD Result: AX = 0043 = 43H = 67 decimal DIV CH Divide AX by unpacked BCD in CH Quotient:
AL = 07 unpacked BCD Remainder:
AH = 04 unpacked BCD Flags undefined after DIV
NOTE: If an attempt is made to divide by 0, the 8086 will do a type 0 interrupt.
CBW-Convert Signed Byte to Signed Word: This instruction copies the sign of a byte in AL to all the bits in AH. AH is then said to be the sign extension of AL. The CBW operation must be done before a signed byte in AL can be divided by another signed byte with the IDIV instruction. CBW affects no flags.
EXAMPLE:
AX = 00000000 10011011 155 decimal
CBW Convert signed byte in AL to signed word in AX Result: AX = 11111111 10011011 155 decimal
CWD-Convert Signed Word to Signed Double word: CWD copies the sign bit of a word in AX to all the bits of the DX register. In other words it extends the sign of AX into all of DX. The CWD operation must be done before a signed word in AX can be divided by another signed word with the IDIV instruction. CWD affects no flags.
EXAMPLE:
DX = 00000000 00000000
AX = 11110000 11000111 3897 decimal
CWD Convert signed word in AX to signed double word in DX:AX Result DX = 11111111 11111111
AX = 11110000 11000111 3897 decimal
Logical, Shift, Rotate and Bit Instructions:
The 80x86 family provides five logical instructions, four rotate instructions, and three shift instructions. The logical instructions are and, or, xor, test, and not; the rotates are ror, rol, rcr, and rcl; the shift instructions are shl/sal, shr, and sar.
The Logical Instructions: AND, OR, XOR, and NOT: The 80x86 logical instructions operate on a bit-by- bit basis. Except not, these instructions affect the flags as follows:
- They clear the carry flag.
- They clear the overflow flag.
- They set the zero flag if the result is zero, they clear it otherwise.
- They copy the H.O. bit of the result into the sign flag.
- They set the parity flag according to the parity (number of one bits) in the result.
- They scramble the auxiliary carry flag.
The not instruction does not affect any flags.
The AND instruction sets the zero flag if the two operands do not have any ones in corresponding bit positions. AND AX, BX
The OR instruction will only set the zero flag if both operands contain zero. OR AX, BX
The XOR instruction will set the zero flag only if both operands are equal. Notice that the xor operation will produce a zero result if and only if the two operands are equal. Many programmers commonly use this fact to clear a sixteen bit register to zero since an instruction of the form
xor reg16, reg16; XOR AX, AX is shorter than the comparable mov reg,0 instruction.
You can use the and instruction to set selected bits to zero in the destination operand. This is known as masking out data; Likewise, you can use the or instruction to force certain bits to one in the destination operand;
The Shift Instructions: SHL/SAL, SHR, SAR: The 80x86 supports three different shift instructions (shl and sal are the same instruction):shl (shift left), sal (shift arithmetic left), shr (shift right), and sar (shift arithmetic right).
SHL/SAL: These instructions move each bit in the destination operand one bit position to the left the number of times specified by the count operand. Zeros fill vacated positions at the L.O. bit; the H.O. bit shifts into the carry flag.
The shl/sal instruction sets the condition code bits as follows:
- If the shift count is zero, the shl instruction doesn’t affect any flags.
- The carry flag contains the last bit shifted out of the H.O. bit of the operand.
- The overflow flag will contain one if the two H.O. bits were differen tprior to a single bit shift. The overflow flag is undefined if the shift count is not one.
- The zero flag will be one if the shift produces a zero result.
- The sign flag will contain the H.O. bit of the result.
- The parity flag will contain one if there are an even number of one bits inthe L.O. byte of the result.
- The A flag is always undefined after the shl/sal instruction.
The shift left instruction is especially useful for packing data. For example, suppose you have two nibbles in al and ah that you want to combine. You could use the following code to do this:
shl ah, 4 ;
or al, ah ;Merge in H.O. four bits.
Of course, al must contain a value in the range 0..F for this code to work properly (the shift left operation automatically clears the L.O. four bits of ah before the or instruction).
SHL OPERATION
H.O. four bits of al are not zero before this operation, you can easily clear them with an and instruction:
shl ah, 4 ;Move L.O. bits to H.O. position. and al, 0Fh ;Clear H.O. four bits.
or al, ah ;Merge the bits.
Since shifting an integer value to the left one position is equivalent to multiplying that value by two, you can also use the shift left instruction for multiplication by powers of two:
shl ax, 1 ;
Equivalent to AX*2 shl ax, 2 ;
Equivalent to AX*4 shl ax, 3;
Equivalent to AX*8
SAR:Thesar instruction shifts all the bits in the destination operand to the right one bit, replicating the H.O. bit.
The sar instruction’s main purpose is to perform a signed division by some power of two. Each shift to the right divides the value by two. Multiple right shifts divide the previous shifted result by two, so multiple shifts produce the following results:
SAR OPERATION
sar ax, 1 ;Signed division by 2 sar ax,
2 ;Signed division by 4 sar ax, 3
;Signed division by 8 sar ax, 4 ;
Signed division by 16 sar ax, 5 ;
Signed division by 32 sar ax, 6 ;
Signed division by 64 sar ax, 7 ;
Signed division by 128 sar ax, 8 ;
Signed division by 256
There is a very important difference between the sar and idiv instructions. The idiv instruction always truncates towards zero while sar truncates results toward the smaller result. For positive results, an arithmetic shift right by one position produces the same result as an integer division by two. However, if the quotient is negative, idiv truncates towards zero while sar truncates towards negative infinity.
SHR: The shr instruction shifts all the bits in the destination operand to the right one bit shifting a zero into the H.O. bit
SHR OPERATION
The shift right instruction is especially useful for unpacking data. shifting an unsigned integer value to the right one position is equivalent to dividing that value by two, you can also use the shift right instruction for division by powers of two:
shr ax, 1 ;Equivalent to AX/2 shr ax,
2 ;Equivalent to AX/4 shr ax, 3;
Equivalent to AX/8 shr ax, 4;
Equivalent to AX/16
The Rotate Instructions: RCL, RCR, ROL, and ROR
The rotate instructions shift the bits around, just like the shift instructions, except the bits shifted out of the operand by the rotate instructions re-circulate through the operand. They include rcl(rotate through carry left), rcr(rotate through carry right), rol(rotate left), and ror(rotate right). These instructions all take the forms :rcldest, count roldest, count rcr dest, count ror dest, count
RCL: The rcl(rotate through carry left), as its name implies, rotates bits to the left, through the carry flag, and back into bit zero on the right. The rcl instruction sets the flag bits as follows:
- The carry flag contains the last bit shifted out of the H.O. bit of the operand.
- If the shift count is one, rcl sets the overflow flag if the sign changes as a result of the rotate. If the count is not one, the overflow flag is undefined.
- The rcl instruction does not modify the zero, sign, parity, or auxiliary carry flags.
RCL OPERATION
RCR: The rcr (rotate through carry right) instruction is the complement to the rcl instruction. It shifts its bits right through the carry flag and back into the H.O. bit. This instruction sets the flags in a manner analogous to rcl:
- The carry flag contains the last bit shifted out of the L.O. bit of the operand.
- The rcr instruction does not affect the zero, sign, parity, or auxiliary carry flags.
RCR OPERATION
ROL: The rol instruction is similar to the rcl (rotate through carry right) instruction in that it rotates its operand to the left the specified number of bits. The major difference is that rol shifts its operand’s H.O. bit ,rather than the carry, into bit zero.
Rol also copies the output of the H.O. bit into the carry flag . The rol instruction sets the flags identically to rcl. Other than the source of the value shifted into bit zero, this instruction behaves exactly like the rcl instruction.
Like shl, the rol instruction is often useful for packing and unpacking data.
ROL OPERATION
ROR: The ror instruction relates to the rcr instruction in much the same way that the rol instruction relates to rcl. That is, it is almost the same operation other than the source of the input bit to the operand. Rather than shifting the previous carry flag into the H.O. bit of the destination operation, ror shifts bit zero into the H.O. bit.
ROR OPERATION
String Instructions: A string is a collection of objects stored in contiguous memory locations. Strings are usually arrays of bytes or words on 8086.All members of the 80x 86 families support five different string instructions: MOVS, CMPS, SCAS, LODS, AND STOS.
The string instructions operate on blocks (contiguous linear arrays) of memory. For example, the movs instruction moves a sequence of bytes from one memory location to another. The cmps instruction compares two blocks of memory. The scas instruction scansa block of memory for a particular value. These string instructions often require three operands, a destination block address, a source block address, and (optionally) an element count. For example, when using the movs instruction to copy a string, you need a source address, a destination address, and a count (the number of string elements to move).
The operands for the string instructions include:
- the SI (source index) register,
- • the DI (destination index) register,
- • the CX (count) register, the AX register, and
- • the direction flag in the FLAGS register.
The REP/REPE/REPZ and REPNZ/REPNE Prefixes: The repeat prefixes tell the 80x86 to do a multi-byte string operation. The syntax for the repeat prefix is:
Field:
Label repeat mnemonic operand ; comment
For MOVS:
Rep movs {operands} For CMPS:
Repe cmps
{operands}
{operands} For SCAS:
Repz cmps
{operands} Repne cmps {operands} Repnz cmps
Repe scas {operands} Repz scas {operands} Repne scas {operands} repnz scas {operands} For STOS:
repstos {operands}
When specifying the repeat prefix before a string instruction, the string instruction repeats cx times. Without the repeat prefix, the instruction operates only on a single byte,word, or double word.
If the direction flag is clear, the CPU increments si and di after operating upon eachstring element. If the direction flag is set, then the 80x86 decrements si and di after processing eachstring element. The direction flag may be set or cleared using the cld (clear direction flag) and std (setdirection flag) instructions.
The MOVS Instruction: The movsb (move string, bytes) instruction fetches the byte at address ds:si, stores it at address es:di, and then increments or decrements the si and di registers by one. If the rep prefix is present, the CPU checks cx to see if it contains zero. If not, then it moves the byte from ds:si to es:di and decrements the cx register. This process repeats until cx becomes zero. The syntax is :
{REP} MOVSB {REP} MOVSW
The CMPS Instruction: The cmps instruction compares two strings. The CPU compares the string referenced by es:di to the string pointed at by ds:si. Cx contains the length of the two strings (whenusing the rep prefix). The syntax is: {REPE} CMPSB {REPE} CMPSW
To compare two strings to see if they are equal or not equal, you must compare corresponding elements in a string until they don’t match or the length of the string cx=0. Therese prefix accomplishes this operation. It will compare successive elements in a string as long as they are equal and cx is greater than zero.
The SCAS Instruction: The scars instruction, by itself, compares the value in the accumulator (al or ax) against the value pointed at by es: di and then increments (or decrements) di by one or two. The CPU sets the flags according to the result of the comparison. When using the refine prefix (repeat while not equal), scas scans the string searching for the first string element which is equal to the value in the accumulator. The scars instruction takes the following forms:{REPNE} SCASB {REPNE} SCASW The STOS
Instruction: The store instruction stores the value in the accumulator at the location specified byes: di. After storing the value, the CPU increments or decrements di depending upon the state of the direction flag. Its primary use is to initialize arrays and strings to a constant value. {REP} STAUB
{REP} STOSW
The LODS Instruction: The lods instruction copies the byte or word pointed at by ds: si into the al or ax register, after which it increments or decrements the si register by one or two. {REP} LODSB
{REP} LOW
Flag Manipulation and Processor Control Instructions: These instructions control the functioning of the available hardware inside the processor chip. These are categorized into two types; (a) flag manipulation instructions and (b) machine control instructions.
The flag manipulation instructions directly modify some of the flags of 8086. The machine control instructions control the bus usage and execution. The flag manipulation instructions and their functions are as follows:
CLC - Clear carry flag CMC - Complement carry flag STC - Set carry flag CLD - Clear direction flag STD - Set direction flag CLI - Clear interrupt flag STI - Set interrupt flag
These instructions modify the carry(CF), direction(DF) and interrupt(IF) flags directly. The DF and IF, which may be modified using the flag manipulation instructions, further control the processor operation; like interrupt responses and auto-increment or auto-decrement modes.
The machine control instructions supported by 8086 and 8088 are listed as follows along with their functions. These machine control instructions do not require any operand.
WAIT - Wait for the Test input pin to go low HLT - Halt the processor NOP - No Operation ESC - Escape to an external device like NDP (numeric co-processor) LOCK - Bus lock instruction prefix.
After executing the HLT instruction, the processor enters the halt state. The two ways to pull it out of the halt state are to reset the processor or to interrupt it.
When NOP instruction is executed, the processor does not perform any operation till 4 clock cycles, except incrementing the IP by one. It then continues with further execution after 4 clock cycles.
ESC instruction when executed frees the bus for an external master like a coprocessor or peripheral devices.
The LOCK prefix may appear with another instruction. When it is executed, the bus access is not allowed for another master till the lock prefixed instruction is executed completely. This instruction is used in the case of programming for multiprocessor systems.
The WAIT instruction when executed holds the operation of the processor with the current status till the logic level on the TEST pin goes low. The processor goes on inserting WAIT states in the instruction cycle, till the TEST pin goes low. Once the TEST pin goes low, it continues further execution.
Program Flow Control Instructions: The control transfer instructions are used to transfer the control from one memory location to another memory location. In 8086 program control instructions belong to three groups: unconditional transfers, conditional transfers, and subroutine call and return instructions.
Unconditional Jumps: The jump (jump) instruction unconditionally transfers control to another point in the program. Intra-segment jumps are always between statements in the same code segment. Intersegment jumps can transfer control to a statement in a different code segment.
JMP Address
Unconditional jump
Conditional jump
Conditional Jump: The conditional jump instructions are the basic tool for creating loops and other conditionally executable statements like if…..then statement. The conditional jumps test one or more bits in the status register to see if they match some particular pattern. If the pattern matches, the control transfers to the target location. If the condition fails, the CPU ignores the conditional jump and execution continues with the next instruction. Some instructions, for example, test the conditions of the sign, carry, overflow, and zero flags.
Loop Instruction:
These instructions are used to repeat a set of instructions several times.
- Format: LOOP Short-Label
- Operation: (CX) ß (CX)-1
- Jump is initialized to the location defined by a short label if CX≠0. Otherwise, execute the next sequential instruction.
- Instruction LOOP works concerning the contents of CX. CX must be preloaded with a count that represents the number of times the loop is to be repeated.
- Whenever the loop is executed, contents at CX are first decremented and then checked to determine if they are equal to zero.
- If CX=0, the loop is complete and the instruction following the loop is executed.
- If CX ≠ 0, content return to the instruction at the label specified in the loop instruction.
- LOOP AGAIN is almost the same as DEC CX, JNZ AGAIN SUBROUTINE & SUBROUTINE HANDLING INSTRUCTIONS: CALL,
RET
- A subroutine is a special segment of a program that can be called for execution from any point in a program.
- An assembly language subroutine is also referred to as a “procedure”.
- Whenever we need the subroutine, a single instruction is inserted into the main body of the program to call a subroutine.
- Transfers the flow of the program to the procedure.
- CALL instruction differs from the jump instruction because a CALL saves a return address on the
- o stack.
- The return address returns control to the instruction that immediately follows the CALL in a program when a RET instruction executes.
- To branch a subroutine the value in the IP or CS and IP must be modified.
- After execution, we want to return the control to the instruction that immediately follows the one called the subroutine i.e., the original value of IP or CS and IP must be preserved.
- Execution of the instruction causes the contents of the IP to be saved on the stack. (this time (SP) ß (SP) -2 )
- A new 16-bit (near-proc, mem16, reg16 i.e., Intra Segment) value which is specified by the operand of the instruction is loaded into IP.
Examples: CALL 1234H
CALL BX
CALL [BX]
Return Instruction: RET instruction removes an address from the stack so the program returns to the instruction following the CALL
Every subroutine must end by executing an instruction that returns control to the main program. This is the return (RET) instruction.
By execution, the value of IP or IP and CS that were saved in the stack is to be returned to their corresponding registers. (this time (SP) ß (SP)+2 )