Unit 3 Lecturer Notes
Unit 3 Lecturer Notes
The instruction format of 8086 has one or more number of fields associated with it.
The first filled is called operation code field or opcode field, which indicates the
type of operation. The instruction format also contains other fields known as
operand fields.
There are six general formats of instructions in 8086 instruction set. The length of an
instruction may vary from one byte to six bytes.
8086(Machine Language Instruction Formats)
•A machine language instruction format has one or more number of fields associated with
it.
•The first field is called as operation code field or op-code field, which indicates the type
of operation to be performed by the CPU
•The instruction format also contains other fields known as operand fields
•The CPU executes the instruction using the information which reside in these fields
•There are six general formats of instructions in 8086 instructions set.
•The length of an instruction may vary from 1 byte to 6 bytes. The instruction formats are
described as follows
2 Register to Register:
•This format is 2 bytes long
•The first byte of the code specifies the operation code and width of the operand specified
by ‘w’ bit.
•The second byte of the code shows the register operands and R/M field, as shown below
The register represented by the REG field is one of the operands.
•The R/M field specifies another register or memory location i.e. the other operand.
For example:
MOV: data transfer operation from Register to Register.
MOV CL, AL, i.e
3. Register to/from memory with no displacement:•This format is also 2 bytes long and
similar to the Register to Register format except for the MOD field as shown.
The Opcode stands for Operation Code. Every Instruction has a unique 6-bit opcode.
For example, the opcode for MOV is 100010.
D stands for direction
If D=0, then the direction is from the register
If D=1, then the direction is to the register
W stands for word
If W=0, then only a byte is being transferred, i.e. 8 bits
If W=1, them a whole word is being transferred, i.e. 16 bits
The MOD and R/M together is calculated based upon the addressing mode and
register being used in it. This is calculated as follows:
REG stands for register selected. It is a 3-bit code which is calculated as follows:
REG Code Register Selected
000 AL AX
001 CL CX
010 DL DX
011 BL BX
100 AH SP
101 CH BP
110 DH SI
111 BH DI
R/M 0 0 01 10 11
(Memory Mode with (Memory mode with 8 (Memory Mode with 16 (Register
no displacement) bit displacement) bit displacement) Mode)
000 [BX] + [SI] [BX] + [SI] + d8 [BX] + [SI] + d16 AL AX
001 [BX] + [DI] [BX] + [DI] + d8 [BX] + [DI] + d16 CL CX
010 [BP] + [SI] [BP] + [SI] + d8 [BP] + [SI] + d16 DL DX
011 [BP] + [DI] [BP] + [DI] + d8 [BP] + [DI] + d16 BL BX
100 [SI] [SI] + d8 [SI] + d16 AH SP
101 [DI] [DI] + d8 [DI] + d16 CH BP
110 d16 (direct) [BP] + d8 [BP] + d16 DH SI
111 [BX] [BX] + d8 [BX] + d16 BH DI
Solution:
Q2. The opcode of ADD instruction is 000000. Find the instruction format for
the following instructions:
i. ADD AX , [BX]
ii. ADD AL , [2345H]
Solution:
Eg:
MOV AX, [5000H]
Before: After:
AX AX
X XX 33 22
X Memory Location Memory Location
22 5000 22 5000
33 5001 33 5001
5002
5002
BX BX
10 AB 10 AB
Eg:
MOV AX, [BX]
Before: After:
AX AX
33 22
X XX
X BX BX
50 00 50 00
Eg:
MOV AX, [SI]
Before: After:
AX AX
33 22
X XX
X SI SI
50 00 50 00
Eg:
MOV AX, 50H[BX]
Before: After:
AX AX
33 44
X XX
X BX BX
50 00 50 00
Effective Address = Value of PC + Address part of the
instruction
5000 + 50 = 5050 H is the new memory location and get the data from the address location
5050H
Effective Address = 5050 H
Memory Location Memory Location
44 5000
5050 44 5050
33 5051
5001 33 5051
5002
5052
5052
SI SI
20 00 20 00
1000 + 2000 = 3000 is the new memory location and get the data from address
location 3000
Memory Location Memory Location
5000
3000 3000
3001
5001
3001
5002
3002
3002
12 12
34 34
SI SI
20 00 20 00
50 +1000 + 2000 = 3050 is the new memory location and get the data from address
location 3000
Memory Location Memory Location
12 5000
3050 12 3050
34 3051
5001 34 3051
5002
3052
3052
Before: After:
BX BX
X XX 00 F2
X
PUSH – Pushes (sends, writes or moves) the content of a
specified register or memory location(s) onto the top of the stack.
Eg: PUSH AX
Before: After:
AX AX
10 20 10 20
20 20038
20038
20037 10
20037
20036 20036
POP – POP (reads) two bytes from the top of the stack and keeps them in a specified
register, or memory location (s)
Eg:
PUSH AX
Before: After:
AX AX
X XX 10 20
X Stack Location Stack Location
SP SP
20038 20036
20 20038
20 20038
10 20037
10 20037
20036
20036
Before: After:
AX AX
10 AB FF 33
BX BX
FF 33 10 AB
XLAT – This instruction replaces a byte in AL register with a byte using a
table in the memory.
BX register stores the starting address of the lookup table
Instructions for input and output port transfer
IN − Used to read a byte or word from the provided port to the accumulator.
For 16 bit address DX is only register allowed to carry port address
Eg:
IN AL, F8H
Copy an immediate data [F8] to AL
F8 Port address
Before After
AL AL
X XX 12
X
PORT PORT
33 33
IN AX, DX
Before After
AX AX
X XX FF 33
X DX BX
X XX 80 00
X PORT PORT
80 33 80 33
OUT − Used to send out a byte or word from the accumulator to the provided
port.
Eg:
OUT 0FH, AL
Before After
AX AX
00 0F 00 0F
PORT PORT
00 0F 00 0F
LDS − Used to load DS register and other provided register from the memory
Eg:
LDS BX, [FF05H]
Copy the contents of memory at displacement of FF05H and FF06H to AX
Then copy the contents at displacement of FF07H and FF08H to DS
Eg:
DX DX
50 60 50 60
DI DI
X XX 20 10
X
ESR ESR
X XX 40 30
X
DI = [BX], [BX]+1
ES = [BX]+2, [BX]+3
AH 10000101
AL
AH 10000101
AL
PUSHF − Used to copy the flag register at the top of the stack.
POPF − Used to copy a word at the top of the stack to the flag register.
Arithmetic Instructions
These instructions are used to perform arithmetic operations like addition, subtraction,
multiplication, division, etc.
Following is the list of instructions under this group −
Instructions to perform addition
ADD − Used to add the provided byte to byte/word to word.
ADC − Used to add with carry.
INC − Used to increment the provided byte/word by 1.
AAA − Used to adjust ASCII after addition.
DAA − Used to adjust the decimal after the addition/subtraction operation.
Instructions to perform subtraction
SUB − Used to subtract the byte from byte/word from word.
SBB − Used to perform subtraction with borrow.
DEC − Used to decrement the provided byte/word by 1.
NPG − Used to negate each bit of the provided byte/word and add 1/2’s
complement.
CMP − Used to compare 2 provided byte/word.
AAS − Used to adjust ASCII codes after subtraction.
DAS − Used to adjust decimal after subtraction.
Instruction to perform multiplication
MUL − Used to multiply unsigned byte by byte/word by word.
IMUL − Used to multiply signed byte by byte/word by word.
AAM − Used to adjust ASCII codes after multiplication.
Instructions to perform division
DIV − Used to divide the unsigned word by byte or unsigned double word by
word.
IDIV − Used to divide the signed word by byte or signed double word by
word.
AAD − Used to adjust ASCII codes after division.
CBW − Used to fill the upper byte of the word with the copies of sign bit of
the lower byte.
CWD − Used to fill the upper word of the double word with the sign bit of the
lower word.
Bit Manipulation Instructions
These instructions are used to perform operations where data bits are involved, i.e. operations
like logical, shift, etc.
Following is the list of instructions under this group −
Instructions to perform logical operation
NOT − Used to invert each bit of a byte or word.
AND − Used for adding each bit in a byte/word with the corresponding bit in
another byte/word.
OR − Used to multiply each bit in a byte/word with the corresponding bit in
another byte/word.
XOR − Used to perform Exclusive-OR operation over each bit in a byte/word
with the corresponding bit in another byte/word.
TEST − Used to add operands to update flags, without affecting operands.
Instructions to perform shift operations
SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in
LSBs.
SHR − Used to shift bits of a byte/word towards the right and put zero(S) in
MSBs.
SAR − Used to shift bits of a byte/word towards the right and copy the old
MSB into the new MSB.
Instructions to perform rotate operations
ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and
to Carry Flag [CF].
ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB
and to Carry Flag [CF].
RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and
CF to MSB.
RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and
CF to LSB.
String Instructions
String is a group of bytes/words and their memory is always allocated in a sequential order.
Following is the list of instructions under this group −
REP − Used to repeat the given instruction till CX ≠ 0.
REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag
ZF = 1.
REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero
flag ZF = 1.
MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to
another.
COMS/COMPSB/COMPSW − Used to compare two string bytes/words.
INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the
provided memory location.
OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the
provided memory location to the I/O port.
SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a
byte in AL or string word with a word in AX.
LODS/LODSB/LODSW − Used to store the string byte into AL or string
word into AX.
Program Execution Transfer Instructions (Branch and Loop
Instructions)
These instructions are used to transfer/branch the instructions during an execution. It includes
the following instructions −
Instructions to transfer the instruction during an execution without any condition −
CALL − Used to call a procedure and save their return address to the stack.
RET − Used to return from the procedure to the main program.
JMP − Used to jump to the provided address to proceed to the next instruction.
Instructions to transfer the instruction during an execution with some conditions −
JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
JAE/JNB − Used to jump if above/not below instruction satisfies.
JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
JC − Used to jump if carry flag CF = 1
JE/JZ − Used to jump if equal/zero flag ZF = 1
JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
JGE/JNL − Used to jump if greater than/equal/not less than instruction
satisfies.
JL/JNGE − Used to jump if less than/not greater than/equal instruction
satisfies.
JLE/JNG − Used to jump if less than/equal/if not greater than instruction
satisfies.
JNC − Used to jump if no carry flag (CF = 0)
JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
JNO − Used to jump if no overflow flag OF = 0
JNP/JPO − Used to jump if not parity/parity odd PF = 0
JNS − Used to jump if not sign SF = 0
JO − Used to jump if overflow flag OF = 1
JP/JPE − Used to jump if parity/parity even PF = 1
JS − Used to jump if sign flag SF = 1
Processor Control Instructions
These instructions are used to control the processor action by setting/resetting the flag values.
Following are the instructions under this group −
STC − Used to set carry flag CF to 1
CLC − Used to clear/reset carry flag CF to 0
CMC − Used to put complement at the state of carry flag CF.
STD − Used to set the direction flag DF to 1
CLD − Used to clear/reset the direction flag DF to 0
STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.
CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.
Iteration Control Instructions
These instructions are used to execute the given instructions for number of times. Following
is the list of instructions under this group −
LOOP − Used to loop a group of instructions until the condition satisfies, i.e.,
CX = 0
LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF = 1
& CX = 0
LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies ZF
= 0 & CX = 0
JCXZ − Used to jump to the provided address if CX = 0
Interrupt Instructions
These instructions are used to call the interrupt during program execution.
INT − Used to interrupt the program during execution and calling service
specified.
INTO − Used to interrupt the program during execution if OF = 1
IRET − Used to return from interrupt service to the main program
XX 02
After
AL = 02H + 12H
XX 14H
ADC AL,13H
Before
XX 01H
After
AL = 01+14H +01H (carry)
XX 15H
AL
XX 12H
After
AL = 12H +01H (carry)
XX 13H
= 1000 0011
Instructions to perform subtraction
SUB − Used to subtract the byte from byte/word from word.
Eg:
SUB AL, 14H
Before
Before
AL
XX 20H
After
AL = 20H -14H
XX CH
After
AL = 13-02H -01H (carry)
XX 10H
AL
XX 12H
After
AL = 12H -01H (carry)
XX 11H
NEG − Used to negate each bit of the provided byte/word and add 1/2’s
complement.
Eg:
NEG AL
Before
AL = 01001000
0 1 0 0 1 0 0 0
After
AL = 01001000
1 0 1 1 0 1 1 1
IMUL BL
AX = AL × BL
if AL =02 H
BL =05 H
Before AX
AX
XX XX
After
AX
XX A
IMUL BX
AX = AX × BX ( Lower order)
DX = AX × BX ( Higher order)
IDIV − Used to divide the signed word by byte or signed double word by
word.
Eg:
iDIV CL
AL = Quotient [ AX ÷ CL]
AH = Remainder [ AX ÷ CL]
Eg: IDIV CL
Before :
CL = 10 H, AL =02H
After:
AL = 05H, AH = 00 H
CBW − Used to fill the upper byte of the word with the copies of sign bit of
the lower byte.
CWD − Used to fill the upper word of the double word with the sign bit of the
lower word.
Bit Manipulation Instructions
These instructions are used to perform operations where data bits are involved, i.e. operations
like logical, shift, etc.
Following is the list of instructions under this group −
Instructions to perform logical operation
NOT − Used to invert each bit of a byte or word.
Example for NOT Instruction :
Instruction Explanation
; AX = 0010 0000 0000 1111
; performing NOT on AX
NOT AX ; AX = 1101 1111 1111 0000
AND − Used for adding each bit in a byte/word with the corresponding bit in
another byte/word.
Example for AND Instruction :
Instruction Explanation
OR − Used to multiply each bit in a byte/word with the corresponding bit in
another byte/word.
Example for OR Instruction :
Instruction Explanation
; AL = 1001 0011 = 93H
; BL = 0111 0101 = 75H
; OR byte in AL with byte in BL
OR BL, AL (Register, Register) ; BL = 1111 0111 = F7H
Instruction Explanation
; AL = 1010 1111 = AFH
; BL = 1111 0000 = F0H
; XOR byte in AL with byte in BL
XOR BL, AL (Register, Register) ; BL = 0101 1111 = 5FH
TEST − Used to add operands to update flags, the operands remain unchanged
The logical operational instruction implements bit wise AND function
between the two operands placed in the instruction.
Eg:
TEST AX, DX
AX = 1234H --- 0001 0010 0011 0100
DX = 5678H --- 0101 0110 0111 1000
Perform AX AND DX
Result 1230H --- 0001 0010 0011 0000
Instructions to perform shift operations
SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in
LSBs.
SAL BL , 1
Shift left. Here all the bits are shifted to left , and LSB doesn’t get any bit, automatically it gets a zero. So
if there are 4 shift operations , then the lower nibble gets all zeroes, as the number of shifts increases,
zeroes get increased from the lower
side. .
This instruction can be written using any data type SAL or SHL. Both represent the same thing.
SAR BL, 1
In the above instruction, if it was a signed number , then because of shifting it may become a +ve
number from -ve number. SAR stands for shift arithmetic register. In this MSB doesn’t get a zero, MSB
get itself it’s value, MSB also shifts the bit and also retains it.
SHR − Used to shift bits of a byte/word towards the right and put zero(S) in MSBs.
Eg:
SHR BL, 1
Here shifting is done towards right. In this case MSB gets no shifted value from other bit, so this
automatically gets zero.
SAR − Used to shift bits of a byte/word towards the right and copy the old
MSB into the new MSB.
Instructions to perform rotate operations
ROL − This instruction stands for Rotate Left without carry flag. The contents
of the operand (register or memory location) are rotated left bit-wise by some
number of positions depending on the count value. During this rotation, the
most significant bit (MSB) is moved into the carry flag (CF) as well as into the
Instruction Explanation
ROR − This instruction stands for Rotate Right without carry flag. The contents of the operand are rotated right bit-
wise by some number of positions depending on the count value. The ROL instruction seen above rotates the bits
towards the left side, whereas the ROR instruction rotates the bits towards the right side. Since this instruction rotates
the bits right, the least significant bit (LSB) is moved into the carry flag (CF) as well as into the most significant bit (MSB)
position. The below shows the operation ROR instruction with count value 2.
The number of times the bits should be rotated right is given as the count value in the instruction. In case if the
count value is two or more than two times, firstly CL register is loaded with the count value, and then CL is mentioned in
the instruction (i.e, in place of the count value) as shown in the below examples.
Instruction Explanation
RCR − This instruction stands for Rotate Right Through Carry. In this instruction, the contents of the operand (register
or memory location) are rotated right bit-wise by some number of positions along with the carry flag. During the rotation
the least significant bit (LSB) is moved to carry flag, and the carry flag is moved into the most significant bit (MSB). The
below shows the operation RCR instruction with count value 2.
In RCR instruction, during the first rotation, the bit which is already present
in the carry flag is moved to MSB, and LSB is moved to carry flag. Similar to
ROL and ROR, CL is used, if the rotation of bits is more than one time as
shown in the below examples.
Instruction Explanation
Since the bits are rotated left in RCL instruction, the CF is shifted to LSB,
and MSB is shifted to CF and CL register is used in the instruction for rotating
the bits more than one time as shown in the below examples.
Instruction Explanation
String Instructions
String is a group of bytes/words and their memory is always allocated in a sequential order.
Following is the list of instructions under this group −
REP − Used to repeat the given instruction till CX ≠ 0.
REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag
ZF = 1.
REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero
flag ZF = 1.
MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to
another.
COMS/COMPSB/COMPSW − Used to compare two string bytes/words.
INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the
provided memory location.
OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the
provided memory location to the I/O port.
SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a
byte in AL or string word with a word in AX.
LODS/LODSB/LODSW − Used to store the string byte into AL or string
word into AX.
Program Execution Transfer Instructions (Branch and Loop
Instructions)
These instructions are used to transfer/branch the instructions during an execution. It includes
the following instructions −
Instructions to transfer the instruction during an execution without any condition −
CALL − Used to call a procedure and save their return address to the stack.
RET − Used to return from the procedure to the main program.
JMP − Used to jump to the provided address to proceed to the next instruction.
Instructions to transfer the instruction during an execution with some conditions −
JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
JAE/JNB − Used to jump if above/not below instruction satisfies.
JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
JC − Used to jump if carry flag CF = 1
JE/JZ − Used to jump if equal/zero flag ZF = 1
JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
JGE/JNL − Used to jump if greater than/equal/not less than instruction
satisfies.
JL/JNGE − Used to jump if less than/not greater than/equal instruction
satisfies.
JLE/JNG − Used to jump if less than/equal/if not greater than instruction
satisfies.
JNC − Used to jump if no carry flag (CF = 0)
JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
JNO − Used to jump if no overflow flag OF = 0
JNP/JPO − Used to jump if not parity/parity odd PF = 0
JNS − Used to jump if not sign SF = 0
JO − Used to jump if overflow flag OF = 1
JP/JPE − Used to jump if parity/parity even PF = 1
JS − Used to jump if sign flag SF = 1
Processor Control Instructions
These instructions are used to control the processor action by setting/resetting the flag values.
Following are the instructions under this group −
STC − Used to set carry flag CF to 1
CLC − Used to clear/reset carry flag CF to 0
CMC − Used to put complement at the state of carry flag CF.
STD − Used to set the direction flag DF to 1
CLD − Used to clear/reset the direction flag DF to 0
STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.
CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.
Iteration Control Instructions
These instructions are used to execute the given instructions for number of times. Following
is the list of instructions under this group −
LOOP − Used to loop a group of instructions until the condition satisfies, i.e.,
CX = 0
LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF = 1
& CX = 0
LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies ZF
= 0 & CX = 0
JCXZ − Used to jump to the provided address if CX = 0
Interrupt Instructions
These instructions are used to call the interrupt during program execution.
INT − Used to interrupt the program during execution and calling service
specified.
INTO − Used to interrupt the program during execution if OF = 1
IRET − Used to return from interrupt service to the main program
The shift instructions and rotate instructions look similar in operation, but the difference is
that, in shift instructions, the bits shifted out of operand are lost.
Whereas in the case of rotate instructions the bits shifted out of operand from one end will
By using RCL and RCR instructions the bits in the operand are rotated through the carry
ROL Instruction :
The contents of the operand (register or memory location) are rotated left bit-wise by
During this rotation, the most significant bit (MSB) is moved into the carry flag (CF) as
well as into the least significant bit (LSB) position.
The below shows the operation ROL instruction with count value 2.
In the above figure, we can see on shifting of each bit of operand to the left side, the MSB is shifted to CF and to the LSB
position. The number of times the bits should be rotated left is given as the count value in the instruction. In case if the count value
is two or more than two times, firstly CL register is loaded with the count value, and then CL is mentioned in the instruction (i.e, in
Instruction Explanation
ROR Instruction :
This instruction stands for Rotate Right without carry flag. The contents of the operand are rotated right bit-wise by
some number of positions depending on the count value. The ROL instruction seen above rotates the bits towards the
left side, whereas the ROR instruction rotates the bits towards the right side. Since this instruction rotates the bits right,
the least significant bit (LSB) is moved into the carry flag (CF) as well as into the most significant bit (MSB) position. The
below shows the operation ROR instruction with count value 2.
The number of times the bits should be rotated right is given as the count value in the instruction. In case if the
count value is two or more than two times, firstly CL register is loaded with the count value, and then CL is mentioned in
the instruction (i.e, in place of the count value) as shown in the below examples.
Instruction Explanation
RCR Instruction :
This instruction stands for Rotate Right Through Carry. In this instruction, the contents of the operand (register or
memory location) are rotated right bit-wise by some number of positions along with the carry flag. During the rotation the
least significant bit (LSB) is moved to carry flag, and the carry flag is moved into the most significant bit (MSB). The
below shows the operation RCR instruction with count value 2.
In the above figure, we can see that the carry flag is also been the part of the rotation of bits to the right. In the
previous two instructions i.e., ROR and ROL, the carry flag` is not considered while rotating bits, but it is filled with bit
moved from LSB in case of ROR and MSB in case of ROL.
In RCR instruction, during the first rotation, the bit which is already present
in the carry flag is moved to MSB, and LSB is moved to carry flag. Similar to
ROL and ROR, CL is used, if the rotation of bits is more than one time as
shown in the below examples.
Instruction Explanation
RCL Instruction :
This instruction stands Rotate Left Through Carry. This instruction is
similar to RCR instruction, but in this, the contents of the operand will be
rotated bit-wise left with the carry flag involved in the rotation. The below
shows the operation RCL instruction with count value 2.
Since the bits are rotated left in RCL instruction, the CF is shifted to LSB,
and MSB is shifted to CF and CL register is used in the instruction for rotating
the bits more than one time as shown in the below examples.
Example for RCL Instruction:
Instruction Explanation
In the above shift instructions, both SHL and SAL have the same operation. To
perform multiplications and divisions on binary numbers by powers of two, arithmetic
shifts are used. The logical shifts fill the shifted bit portion by zero i.e., fill zero in
LSB position in case of SHL and SAL, and fill zero in MSB position in case of SHR.
Let us learn briefly about each shift instruction in the 8086 microprocessor.
SHL/SAL Instruction :
The number of shifts of bits is given as the count in the instruction and all the
flags are affected according to the result. If the number of shifts required is one, then
one is mentioned at the count position of the instruction. In case of shifting the bits for
two or more than two times, CL register must be used at the count position which is
loaded with the value of the number of shifts required (i.e., shift count) as shown in
the below examples.
Instruction Explanation
SHR Instruction :
This instruction stands for shift Logical Right. The SHR instruction
performs shifting of each bit in the operand (register or memory location) to
the right side and fills the most significant bit (MSB) with zero. The LSB is
shifted into the carry flag (CF). The operation of SHR instruction with 2 times
shift is shown below.
Here also the number of shifts of the bits is given as the count in the
instruction and all the flags are affected according to the result. If the shift
count is one, then it is directly mentioned in the instruction. For shifting the
bits, two or more than two times, CL register must be used at the count
position which is loaded with shift count value as shown in the below
examples.
Instruction Explanation
SAR Instruction :
This instruction stands for Shift Arithmetic Right. It does the same
operation as SHR except it fills the bit portion shifted right from the MSB with a
copy of old MSB i.e., the SAR instruction performs the right shift on each bit
and fills the MSB portion with the copy of old MSB. The operation of SAR
instruction with 2 times shift is shown below.
Similar to SHR, in SAR instruction, LSB is shifted to carry flag (CF). In the
case of multiple shifts, the CF contains the most recent shift bit and all the
flags are affected according to the result.
Instruction Explanation
TEST Instruction :
The TEST instruction performs bit by bit logical AND operation on the
source and destination operand but does not load the destination operand
with result i.e., this instruction performs bit by bit AND and sets the
corresponding Flag. Hence TEST instruction only affects the flag register (OF,
CF, SF, ZF, PF) and AF undefined which indicates the result of the operation
without changing any operand. This is normally used to compare the operands
bit by bit.
Instruction Explanation
; AND CL with AL
TEST AL, CL ; Flags are affected, result is not stored
Sometimes while executing a program there will be a need for repeated execution
of some instructions a specified number of times. In all the programming languages
there are some specific structures to express this repeated execution of instructions.
Similarly, the designers of the 8086 microprocessor came up with a group of
instructions to perform repeated execution of a series of instructions a specified
number of times much easier. These instructions are called LOOP instructions.
Decrements CX by
one, if CX ≠ 0, jump
LOOP Loop LOOP short-label to "short-label".
Decrements CX by
loop while one, if CX ≠ 0 and ZF
equal/Loop LOOPE/LOOPZ = 1, jump to "short-
LOOPE/LOOPZ while zero short-label label".
Decrements CX by
loop while not one, if CX ≠ 0 and ZF
LOOPNE/ equal/Loop LOOPNE/LOOPNZ = 0, jump to "short-
LOOPNZ while not zero short-lablel label".
Jump if CX is If CX = 0, branch to
JCXZ zero JCXZ short-label "short-label".
All the above instructions are iteration control instructions and are used to execute
a series of instructions repeatedly on the successful satisfaction of the condition. The
condition is to check the CX register and zero flag (ZF) or can be only to check the
CX register. CX register must be loaded with the number of iterations to be
performed, prior to entering the section of the code terminated by LOOP instructions.
Let us see briefly each LOOP instruction.
LOOP Instruction :
The LOOP instruction executes the group of instructions a number of
times and it uses relative addressing mode. The number of iterations will
depend on the condition to be satisfied. The CX register will perform the
LOOP operation on the instructions to be iterated.
LOOPE/LOOPZ Instruction :
This instruction is similar to LOOP except that it checks for both CX and
ZF conditions to be satisfied before the jump can take place. Firstly the count
register CX is loaded with a value of the number of times the instructions are
to be repeated. For every iteration or every time the LOOPE/LOOPZ
instruction executes, it decrements the CX register by one without affecting
flags and performs a short jump to the target address (i.e., to the address with
signed 8-bit relative displacement in the range of -128 bytes to +127 bytes
from the instruction address after LOOPE/LOOPZ instruction) until the
condition CX ≠ 0 and ZF = 1 is maintained.
If CX equals 0, or if the zero flag gets cleared within the loop, the loop will
terminate. In other words, the two ways to exit the loop are CX = 0 or ZF = 0,
then after the processor starts executing the next instruction present after
LOOPE/LOOPZ.
LOOPNE/LOOPNZ Instruction :
These instructions are the opposite of LOOPE/LOOPZ. It decrements the
CX register by one without affecting flags and performs a short jump to the
target address if CX ≠ 0 and ZF = 0. In other words, the number of iterations is
loaded into the count register CX and after every time the instruction
LOOPE/LOOPZ executes the CX will be auto decremented by one. Until the
conditions, CX ≠ 0 and ZF = 0 satisfies, the execution performs a short jump
to the target address given in the instruction.
When either of the condition CX ≠ 0 or ZF = 0 fails to satisfies i.e., zero
flag (ZF) is set or CX becomes zero after auto decrement, the execution of the
loop will exit, and the instruction after LOOPE/LOOPZ in the program will be
executed. A group of instructions can be repeated a number of times (i.e., until
CX = 0 and ZF ≠ 0) by using LOOPE/LOOPZ.
JCXZ Instruction :
The JCXZ loop instruction jumps to the target address if CX = 0 without
affecting flags. This instruction is useful at the beginning of a loop to bypass
the loop if CX = 0. IF the CX register is not equal to zero, no jump in the
program is performed, thereby executing the next instructions. As compared
to LOOPE/LOOPZ and LOOPNE/LOOPNZ instructions, in JCXZ the jump is
not based on the zero flag (ZF) rather it only depends on the CX register. In
the example shown below, JCXZ instruction is used to bypass the AGAIN loop
whenever CX = 0.
These two instructions move the bits inside the register side by side. The syntax of these instructions is