SlideShare a Scribd company logo
11
Most read
12
Most read
14
Most read
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 1
Chapter 5 and 6: Instructions
5.0 INTRODUCTION
 An instruction is a binary pattern designed inside a microprocessor to perform a specific
function.
 In 8086, each instruction consumes one byte to six bytes of memory
 The entire group of instructions that a microprocessor supports is called Instruction Set.
 8086 has more than 20,000 instructions.
5.1 INSTRUCTION SET OF 8086
The Instruction set of 8086 microprocessor is classified into 7, they are:
1. Data transfer instructions
2. Arithmetic and logical instructions
3. Shift / rotate instructions
4. Program control transfer instructions
5. Machine Control Instructions
6. Flag manipulation instructions
7. String instructions
5.2 DATA TRANSFER INSTRCUTIONS
Data transfer instruction, as the name suggests is for the transfer of data from memory to
internal register, from internal register to memory, from one register to another register, from
input port to internal register, from internal register to output port etc.
1. MOV instruction
 It is a general purpose instruction to transfer byte or word from register to register,
memory to register, register to memory or with immediate addressing. General Form:
o MOV Des, Src
 Here the source and destination needs to be of the same size that is either 8 bit or 16 bit.
 MOV instruction does not affect any flags.
 Both Src and Des cannot be memory location at the same time.
Example:
MOV BX, 00F2H ; load the immediate number 00F2H in BX register
MOV CL, [2000H] ; Copy the 8 bit content of the memory location, at a displacement of
2000H from data segment base to the CL register
MOV [589H], BX ; Copy the 16 bit content of BX register on to the memory location,
which at a displacement of 589H from the data segment base.
MOV DS, CX ; Move the content of CX to DS
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 2
2. PUSH instruction
 The PUSH instruction decrements the stack pointer by two and copies the word from
source to the location where stack pointer now points.
 Here the source must be of word size data. Source can be a general purpose register,
segment register or a memory location.
 The PUSH instruction first pushes the most significant byte to sp-1, then the least
significant to the sp-2.
 Push instruction does not affect any flags
Figure 3.1: PUSH operation.
Example:
PUSH CX ; Decrements SP by 2, copy content of CX to the stack (figure shows execution
of this instruction)
PUSH DS ; Decrement SP by 2 and copy DS to stack
3. POP instruction
 The POP instruction copies a word from the stack location pointed by the stack pointer
to the destination.
 The destination can be a General purpose register, a segment register or a memory
location. Here after the content is copied the stack pointer is automatically incremented
by two.
 The execution pattern is similar to that of the PUSH instruction.
Example:
POP CX ; Copy a word from the top of the stack to CX and increment SP by 2.
4. IN & OUT instructions
 The IN instruction copies data from a port to the accumulator. If the data is 8 bit, it goes
to AL and if it is 16 bit then it goes to AX. General Form:
o IN Accumulator, Port Address
 Similarly OUT instruction is used to copy data from accumulator to an output port.
General Form:
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 3
o OUT Port Address, Accumulator
 Both IN and OUT instructions can be done using direct and indirect addressing modes.
Example:
IN AL, 0F8H ; Copy a byte from the port 0F8H to AL
MOV DX, 30F8H ; Copy port address in DX
IN AL, DX ; Move 8 bit data from 30F8H port
IN AX, DX ; Move 16 bit data from 30F8H port
OUT 047H, AL ; Copy contents of AL to 8 bit port 047H
MOV DX, 30F8H ; Copy port address in DX
OUT DX, AL ; Move 8 bit data to the 30F8H port
OUT DX, AX ; Move 16 bit data to the 30F8H port
5. XCHG instruction
 The XCHG instruction exchanges contents of the destination and source. General
Format:
o XCHG Des, Src
 Destination and source can be register and register, or register and memory location,
but XCHG cannot interchange the value of two memory locations.
Example:
XCHG BX, CX ; exchange word in CX with the word in BX
XCHG AL, CL ; exchange byte in CL with the byte in AL
XCHG AX, SUM[BX] ; here physical address, which is DS+SUM+[BX]. The content
at physical address and the content of AX are interchanged
5.3 ARITHEMATIC AND LOGIC INSTRCUTIONS
The arithmetic and logic group of instructions include:
1. ADD instruction
 Add instruction is used to add the current contents of destination with that of source
and store the result in destination. General Format:
o ADD Des, Src
 Here we can use register and/or memory locations. AF, CF, OF, PF, SF, and ZF flags
are affected
Example:
ADD AL, 0FH ; Add the immediate content, 0FH to the content of AL and store
the result in AL
ADD AX, BX ; AX <= AX+BX
ADD AX, 0100H ; IMMEDIATE
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 4
ADD AX, BX ; REGISTER
ADD AX, [SI] ; REGISTER INDIRECT OR INDEXED
ADD AX, [5000H] ; DIRECT
ADD [5000H], 0100H ; IMMEDIATE
ADD 0100H ; DESTINATION AX (IMPLICT)
2. ADC: ADD WITH CARRY
 This instruction performs the same operation as ADD instruction, but adds the carry
flag bit (which may be set as a result of the previous calculation in CF) to the result.
 All the condition code flags are affected by this instruction.
Example:
ADC AX, BX ; REGISTER
ADC AX, [SI] ; REGISTER INDIRECT OR INDEXED
ADC AX, [5000H] ; DIRECT
ADC [5000H], 0100H ; IMMEDIATE
ADC 0100H ; IMMEDIATE (AX IMPLICT)
3. SUB instruction
 SUB instruction is used to subtract the current contents of destination with that of source
and store the result in destination. General Format:
o SUB Des, Src
 Here we can use register and/or memory locations. AF, CF, OF, PF, SF, and ZF flags
are affected
Example:
SUB AL, 0FH ; subtract the immediate content, 0FH from the content of AL and store
the result in AL
SUB AX, BX ; AX <= AX-BX
SUB AX, 0100H ; IMMEDIATE (DESTINATION AX)
SUB AX, BX ; REGISTER
SUB AX, [5000H] ; DIRECT
SUB [5000H], 0100H ; IMMEDIATE
4. SBB: SUBTRACT WITH BORROW
 Subtract with borrow instruction subtracts the source operand and the borrow flag (CF)
which may reflect the result of the previous calculations, from the destination operand.
 Subtraction with borrow, here means subtracting 1 from the subtraction obtained by
SUB, if carry (borrow) flag is set.
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 5
 The result is stored in the destination operand. All the flags are affected (condition code)
by this instruction.
Example:
SBB AX, 0100H ; IMMEDIATE (DESTINATION AX)
SBB AX, BX ; REGISTER
SBB AX, [5000H] ; DIRECT
SBB [5000H], 0100H ; IMMEDIATE
5. CMP: COMPARE
 The instruction compares the source operand, which may be a register or an immediate
data or a memory location, with a destination operand that may be a register or a
memory location.
 For comparison, it subtracts the source operand from the destination operand but does
not store the result anywhere.
 The flags are affected depending upon the result of the subtraction.
o If both of the operands are equal, zero flag is set.
o If the source operand is greater than the destination operand, carry flag is set or
else, carry flag is reset.
Example:
CMP BX, 0100H ; IMMEDIATE
CMP AX, 0100H ; IMMEDIATE
CMP [5000H], 0100H ; DIRECT
CMP BX, [SI] ; REGISTER INDIRECT OR INDEXED
CMP BX, CX ; REGISTER
6. INC & DEC instructions
 INC and DEC instructions are used to increment and decrement the content of the
specified destination by one.
 AF, CF, OF, PF, SF, and ZF flags are affected.
Example:
INC AL ; AL<= AL + 1
INC AX ; AX<=AX + 1
DEC AL ; AL<= AL – 1
DEC AX ; AX<=AX – 1
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 6
7. MUL: Unsigned Multiplication
 Unsigned multiplication instruction. It assumes one of the operand is in AL or AX.
General Format:
o MUL Src
 It multiplies two bytes to produce a word or two words to produce a double word. Src
can be a register or memory location.
AX = AL * Src
DX : AX = AX * Src
Example:
MUL Reg/ Mem ; for byte: (AX) <= (AL) x (Reg8/Mem8)
; for word: (DX)(AX) <= (AX) x (Reg16/Mem16)
8. IMUL: Signed Multiplication
 Signed multiplication instruction.
IMUL Reg/ Mem ; for byte: (AX) <= (AL) x (Reg8/Mem8)
; for word: (DX)(AX) <= (AX) x (Reg16/Mem16)
9. DIV: Unsigned Division
 Unsigned division instruction. It divides word by byte or double word by word. General
Format:
o DIV Src
 The operand is stored in AX, divisor is Src and the result is stored as:
o AH = remainder AL = quotient
Example:
DIV Reg/ Mem ; For 16-bit ÷ 8-bit:
(AL) <= (AX) ÷ (Reg8/Mem8) Quotient
(AH) <= (AX) MOD (Reg8/Mem8) Remainder
; For 32-bit ÷ 16-bit:
(AX) <= (DX)(AX) ÷ (Reg16/Mem16) Quotient
(DX) <= (DX)(AX) MOD (Reg16/Mem16) Remainder
10. IDIV: Signed Division
 Signed division instruction.
Example:
IDIV Reg/ Mem ; For 16-bit ÷ 8-bit:
(AL) <= (AX) ÷ (Reg8/Mem8) Quotient
(AH) <= (AX) MOD (Reg8/Mem8) Remainder
; For 32-bit ÷ 16-bit:
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 7
(AX) <= (DX)(AX) ÷ (Reg16.Mem16) Quotient
(DX) <= (DX)(AX) MOD (Reg16/Mem16) Remainder
11. AND instruction
 This instruction logically ANDs each bit of the source byte/word with the
corresponding bit in the destination and stores the result in destination. General Format:
o AND Des, Src
 The source can be an immediate number, register or memory location. Destination can
be a register or memory location.
 The CF and OF flags are both made zero, PF, ZF, SF are affected by the operation and
AF is undefined.
Example:
AND BL, AL ; suppose BL=1000 0110 and AL = 1100 1010 then after the operation
BL would be BL= 1000 0010.
AND CX, AX ; CX <= CX AND AX
AND CL, 08 ; CL<= CL AND (0000 1000)
12. OR instruction
 This instruction logically ORs each bit of the source byte/word with the corresponding
bit in the destination and stores the result in destination. General Format:
o OR Des, Src
 The source can be an immediate number, register or memory location. Destination can
be a register or memory location.
 The CF and OF flags are both made zero, PF, ZF, SF are affected by the operation and
AF is undefined.
Example:
OR BL, AL ; suppose BL=1000 0110 and AL = 1100 1010 then after the operation
BL would be BL= 1100 1110.
OR CX, AX ; CX <= CX AND AX
OR CL, 08 ; CL<= CL AND (0000 1000)
13. NOT instruction
 The NOT instruction complements (inverts) the contents of an operand register or a
memory location, bit by bit.
Example:
NOT AX ; Before AX= (1011)2= (B)16. After execution AX= (0100)2= (4)16.
NOT [5000H]
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 8
14. XOR instruction
 The XOR operation is carried out in a similar way to the AND and OR operation. The
constraints on the operands are also similar.
 The XOR operation gives a high output (1), when the two input bits are dissimilar.
Otherwise, the output is zero.
Example:
XOR AX, 0098H
XOR AX, BX
XOR AX, [5000H]
5.4 SHIFT / ROTATE INSTRCUTIONS
 Shift instructions move the binary data to the left or right by shifting them within the
register or memory location. They also can perform multiplication of powers of 2+n
anddivision of powers of 2-n
.
 There are two type of shifts logical shifting and arithmetic shifting, later is used with
signed numbers while former with unsigned.
 Rotate on the other hand rotates the information in a register or memory either from
one end to another or through the carry flag.
1. SHL/SAL instruction
 Both instruction shifts each bit to left, and places the MSB in CF and LSB is made 0.
 The destination can be of byte size or of word size, also it can be a register or a memory
location. General Format:
o SAL/SHL Des, count
 Number of shifts is indicated by the count. All flags are affected.
Example:
MOV BL, B7H ; BL is made B7H
SAL BL, 1; shift the content of BL register one place to left. Before
execution:
2. SHR instruction
 This instruction shifts each bit in the specified destination to the right and 0 is stored in
the MSB position. The LSB is shifted into the carry flag.
CY
0
B7
1
B6
0
B5
1
B4
1
B3
0
B2
1
B1
1
B0
1
After the execution:
CY B7 B6 B5 B4 B3 B2 B1 B0
1 0 1 1 0 1 1 1 0
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 9
 The destination can be of byte size or of word size, also it can be a register or a memory
location. General Format:
o SHR Des, count
 Number of shifts is indicated by the count. All flags are affected
Example:
MOV BL, B7H ; BL is made B7H
SHR BL, 1 ; shift the content of BL register one place to the right.
Before execution:
3. ROL instruction
 This instruction rotates all the bits in a specified byte or word to the left some number
of bit positions. MSB is placed as a new LSB and a new CF.
 The destination can be of byte size or of word size, also it can be a register or a memory
location. General Format:
o ROL Des, count
 Number of shifts is indicated by the count. All flags are affected
Example:
MOV BL, B7H ; BL is made B7H
ROL BL, 1 ; rotates the content of BL register one place to the left.
Before execution:
4. ROR instruction
 This instruction rotates all the bits in a specified byte or word to the right some number
of bit positions. LSB is placed as a new MSB and a new CF.
 The destination can be of byte size or of word size, also it can be a register or a memory
location. General Format:
o ROR Des, count
B7
1
B6
0
B5
1
B4
1
B3
0
B2
1
B1
1
B0
1
CY
0
After execution:
B7 B6 B5 B4 B3 B2 B1 B0 CY
0 1 0 1 1 0 1 1 1
CY
0
B7
1
B6
0
B5
1
B4
1
B3
0
B2
1
B1
1
B0
1
After the execution:
CY B7 B6 B5 B4 B3 B2 B1 B0
1 0 1 1 0 1 1 1 1
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 10
 Number of shifts is indicated by the count. All flags are affected
Example:
MOV BL, B7H ; BL is made B7H
ROR BL, 1 ; shift the content of BL register one place to the right.
Before execution:
5. RCR instruction
 This instruction rotates all the bits in a specified byte or word to the right some number
of bit positions along with the carry flag. LSB is placed in a new CF and previous carry
is placed in the new MSB.
 The destination can be of byte size or of word size, also it can be a register or a memory
location. General Format:
o RCR Des, count
 Number of shifts is indicated by the count. All flags are affected
Example:
MOV BL, B7H ; BL is made B7H
RCR BL, 1 ; shift the content of BL register one place to the right.
Before execution:
After Execution:
B7 B6 B5 B4 B3 B2 B1 B0 CY
0 1 0 1 1 0 1 1 1
B7
1
B6
0
B5
1
B4
1
B3
0
B2
1
B1
1
B0
1
CY
0
After execution:
B7 B6 B5 B4 B3 B2 B1 B0 CY
1 1 0 1 1 0 1 1 1
B7 B6 B5 B4 B3 B2 B1 B0 CY
1 0 1 1 0 1 1 1 0
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 11
5.5 PROGRAM CONTROL INSTRCUTIONS
 These instructions cause change in the sequence of the execution of instruction.
 This change can be through a condition or sometimes unconditional.
 There are two types of Program control transfer instructions:
 Unconditional transfer instructions – JMP, CALL, RET
 Conditional transfer instructions – J condition
1. Unconditional JUMP (JMP XXX)
o Short Jump
o Near Jump
o Far jump
 Short and near jump are often called intrasegment jump and far jumps are often
called intersegment jump
 Short jump and near jump follows a distance or displacement to jump where as far
jump follows an address (segment + offset) to jump
A. Short JUMP (JMP 1byte-displacement)
• Short jump is a two-byte instruction.
• Instead of a jump address, it jumps by following a 8-bit (one byte) signed displacement.
• It allows jumps or branches to memory location within +127 and -128 bytes
from the address following the jump.
• The displacement is sign-extended and added to the instruction pointer (IP) to
generate the jump address within the current code segment
1 byte 1byte
JMP disp; here disp is 8-bit signed
Displacement or distance
Example:
JMP 04H
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 12
65536
1024
B. Near Jump (JMP 2byte-displacement)
• Near jump is similar to short jump, except that the distance is farther.
• Near jump is a three-byte instruction.
• Displacement is 16-bit (2 byte) signed displacement.
• It allows jumps or branches to memory location within ±32 𝐾 𝑏𝑦𝑡𝑒𝑠 of current code
segment.
 216 = 65536 = 64 𝐾𝑏𝑦𝑡𝑒 = −32𝐾𝑏𝑦𝑡𝑒 𝑡𝑜 + 32𝐾𝑏𝑦𝑡𝑒
• The signed displacement added to the instruction pointer (IP) to generate the jump address.
C. Far Jump (JMP 4byte-displacement)
• Afar jump instruction obtain a new segment and offset address to accomplish the
jump.
• It is a 5 byte instruction.
• Byte 2 and 3 contain new offset address. Byte 4 and 5 contains new segment address.
• It allows jumps or branches to any memory location of any memory segment. That is
why far jump is called intersegment jump.
Example:
JMP 0002H
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 13
Example:
JMP 0127: A300
Jump to CSX10+IP = A300X10+0127 = A3127
2. CALL instruction
o The CALL instruction transfer the flow of the program to the procedure. The CALL
instruction differ from a jump instruction because a CALL saves a return address on the
stack.
Whenever a CALL instruction executes it:
 Pushes the IP or, CS: IP on the stack.
 Changes the value of IP or, CS: IP.
Jumps to the procedure by new IP or, CS: IP address
Difference between JMPand CALL instruction
JMP CALL
Doesn’t use stack Uses stack
Doesn’t return to the next instruction
of JMP
Must return to the next instruction of
CALL
Used to jump to new location Used to execute subroutine
Has conditional and unconditional
jump
8086 has only unconditional call
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 14
Types of CALL
(a) Near CALL (b) Far CALL
Difference between Near CALLand Far Call
Near CALL Far CALL
(1) Procedure located within the same code
segment (±32KB)
(1) Procedure located in the entire memory
(1 MB)
(2) 3-byte instruction (2) 5-byte instruction
(3) Only IP content is replaced by
(IP±displacement)
(3) Both CS and IP contents are replaced
by new CS and IP address
(4) Stack stores only return IP address (2 byte) (4) Stack stores the return CS and IP
address. (4 byte)
3. RET instruction
o RET instruction returns execution from a procedure to the next instruction after the
CALL instruction in the calling program.
o If it was a near call, then IP is replaced with the value at the top of the stack, if it had
been a far call, then another POP of the stack is required.
o This second popped data from the stack is put in the CS, thus resuming the execution
of the calling program.
o RET instruction can be followed by a number, to specify the parameters passed. RET
instruction does not affect any flags. General format:
• RET
Example:
p1 PROC ; procedure declaration.
MOV AX, 1234h
RET ; return to caller.
p1 ENDP
4. Conditional JUMP
o A conditional jump instruction allows the programmer to make decision based
upon numerical tests.
o The conditional jump instructions are always short jump in 8086.
o Conditional jump instructions test the following flag bits: sign (S), zero (O), carry (C),
parity (P) and overflow (O).
o If the condition under test is true, a branch to the label associated with the jump
instruction occurs. If the condition is false, the next sequential step in the program
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 15
executes.
o For example, a JC will jump if the carry bit is set.
Examples of some common conditional JUMP
Assembly language Tested Condition Operation
JNE or, JNZ Z=0 Jump if not equal or jump if not zero
JE or JZ Z=1 Jump if equal or jump if zero
JNO O=0 Jump if no overflow
JNP or JPO P=0 Jump if no parity of jump if parity odd
JP or JPE P=1 Jump if parity or jump if parity even
5. Iteration control instructions
o These instructions are used to execute a series of instructions some number of times.
o The number is specified in the CX register, which will be automatically decremented in
course of iteration. But here the destination address for the jump must be in the rangeof -
128 to 127 bytes.
Example:
Instructions here are:-
LOOP ; loop through the set of instructions until CX is 0
LOOPE/LOOPZ ; here the set of instructions are repeated until CX=0 or ZF=0
LOOPNE/LOOPNZ ; here repeated until CX=0 or ZF=1
6. Controlling the flow of the Program
o It is much easier to use the assembly language statements .IF, .ELSE, .ELSEIF, and .ENDIF to
control the flow of the program than it is to use the correct conditional jump statement.
o Other statements: .REPEAT - .UNTIL and .WHILE - .ENDW, .BREAK, .CONTINUE
o These statements can replace the conditional jump and loop statements
7. Procedures
o A procedure is a group of instructions (subroutine or function) that usually perform a
specific task.
Advantages:
(a) It is reusable section of the software that is stored in memory once, but used as
often as necessary.
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 16
(b) It saves memory space.
(c) Makes easier to develop software
Disadvantages
It takes the compiler a small amount of time to link the procedure and return from it
How procedure links with main program
o The CALL instruction links to the procedure and the RET (return) instruction return from
the procedure
o The CALL instruction pushes the address (return address) of the instruction following the
CALL on the stack. The RET instruction removes an address from the stack so the
program return to the instruction following the CALL.
o A procedure begins with the PROC directive and ends with the ENDP directive. The
PROC directive is followed by the type of procedure: NEAR (intrasegment) or FAR
(intersegment)
Format of a procedure Example
XXX PROC NEAR/FAR SUMS PROC NEAR
5.6 MACHINE CONTROL INSTRCUTIONS
1. HLT instruction
 The processor enters into a halt state. The processor gets out of this Halt signal upon an
interrupt signal in INTR pin/NMI pin or a reset signal on RESET input.
 General form:
o HLT
2. WAIT instruction
 When this instruction is executed, the 8086 enters into an idle state. This idle state is
continued till a high is received on the TEST input pin or a valid interrupt signal is
received.
 Wait affects no flags. It generally is used to synchronize the 8086 with a peripheral
device(s).
…………………………………….. ADD AX,BX
…………………………………….. ADD AX,CX
…………………………………….. ADD AX,DX
RET RET
XXX ENDP SUMS ENDP
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 17
3. ESC instruction
 This instruction is used to pass instruction to a coprocessor like 8087. There is a 6 bit
instruction for the coprocessor embedded in the ESC instruction.
 In most cases the 8086 treats ESC and a NOP, but in some cases the 8086 will access
data items in memory for the coprocessor
4. LOCK instruction
 In multiprocessor environments, the different microprocessors share a system bus,
which needed to access external devices like disks.
 LOCK instruction is given as prefix in the case when a processor needs exclusive access
of the system bus for a particular instruction. It affects no flags.
5. NOP instruction
 At the end of NOP instruction, no operation is done other than the fetching and decoding
of the instruction.
 NOP affects no flags.
5.7 FLAG MANIPULATION INSTRCUTIONS
1. STC instruction
 This instruction sets the carry flag. It does not affect any other flag.
2. CLC instruction
 This instruction resets the carry flag to zero. CLC does not affect any other flag.
3. CMC instruction
 This instruction complements the carry flag. CMC does not affect any other flag.
4. STD instruction
 This instruction is used to set the direction flag to one so that SI and/or DI can be
decremented automatically after execution of string instruction. STD does not affect
any other flag.
5. CLD instruction
 This instruction is used to reset the direction flag to zero so that SI and/or DI can be
incremented automatically after execution of string instruction. CLD does not affect
any other flag.
6. STI instruction
 This instruction sets the interrupt flag to 1. This enables INTR interrupt of the 8086.
STI does not affect any other flag.
7. CLI instruction
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 18
 This instruction resets the interrupt flag to 0. Due to this the 8086 will not respond to
an interrupt signal on its INTR input. CLI does not affect any other flag.
5.8 STRING INSTRCUTIONS
 String is a sequence of bytes or words
 8086 instruction set includes instruction for string movement, comparison, scan, load
and store.
 REP instruction prefix : used to repeat execution of string instructions
 String instructions end with S or SB or SW. S represents string, SB string byte and SW
string word.
 Offset or effective address of the source operand is stored in SI register and that of the
destination operand is stored in DI register.
 Depending on the status of DF, SI and DI registers are automatically updated.
 DF = 0 => SI and DI are incremented by 1 for byte and 2 for word.
 DF = 1 => SI and DI are decremented by 1 for byte and 2 for word.
1. MOVS/MOVSB/MOVSW
 These instructions copy a word or byte from a location in the data segment to a location
in the extra segment.
 The offset of the source is in SI and that of destination is in DI. For multiple word/byte
transfers the count is stored in the CX register.
 When direction flag is 0, SI and DI are incremented and when it is 1, SI and DI are
decremented.
 MOVS affect no flags. MOVSB is used for byte sized movements while MOVSW is
for word sized.
Example:
CLD ; clear the direction flag to auto increment SI and DI
MOV AX, 0000H ;
MOV DS, AX ; initialize data segment register to 0
MOV ES, AX ; initialize extra segment register to 0
MOV SI, 2000H ; Load the offset of the string1 in SI
MOV DI, 2400H ; Load the offset of the string2 in DI
MOV CX, 04H ; load length of the string in CX
REP MOVSB ; decrement CX and MOVSB until CX will be 0
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 19
MOVSB MA = (DS) x 10 + (SI)
MAE = (ES) x 10 + (DI)
(MAE) <= (MA)
If DF = 0, then (DI) <= (DI) + 1; (SI) <= (SI) + 1
If DF = 1, then (DI) <= (DI) - 1; (SI) <= (SI) – 1
MOVSW MA = (DS) x 10 + (SI)
MAE = (ES) x 10 + (DI)
(MAE ; MAE + 1) <= (MA; MA + 1)
If DF = 0, then (DI) <= (DI) + 2; (SI) <= (SI) + 2
If DF = 1, then (DI) <= (DI) - 2; (SI) <= (SI) - 2
2. REP/REPE/REP2/REPNE/REPNZ
 REP is used with string instruction; it repeats an instruction until the specified condition
becomes false.
 REP => CX=0
 REPE/REPZ => CX=0 OR ZF=0
 REPNE/REPNZ => CX=0 OR ZF=1
Example:
REP MOVSB STR1, STR2
3. LODS / LODSB /LODSW
 Load a byte or word in AL or AX
 Copies byte or word from memory location pointed by SI into AL or AX register.
Example:
LODSB S_STRING
LODSB MA = (DS) x 10 + (SI)
(AL) <= (MA)
If DF = 0, then (SI) <= (SI) + 1
If DF = 1, then (SI) <= (SI) – 1
LODSW MA = (DS) x 10 + (SI)
(AX) <= (MA ; MA + 1)
If DF = 0, then (SI) <= (SI) + 2
If DF = 1, then (SI) <= (SI) – 2
4. STOS / STOSB / STOSW
 Store byte or word in a string. It does not affect any flags.
 Copies a byte or word contained in AL or AX to memory location pointed by DI.
Example:
STOS D_STRING
CoSc-3025: Microprocessor and Assembly Language Programming
Page 5- 20
STOSB MAE = (ES) x 10 + (DI)
(MAE) <= (AL)
If DF = 0, then (DI) <= (DI) + 1
If DF = 1, then (DI) <= (DI) – 1
STOSW MAE = (ES) x 10 + (DI)
(MAE ; MAE + 1 ) <= (AX)
If DF = 0, then (DI) <= (DI) + 2
If DF = 1, then (DI) <= (DI) – 2
5. CMPS / CMPSB / CMPSW
 Compare string bytes or string words.
 The comparison is affected by subtraction of content pointed by DI from that pointed
by SI. The AF, CF, OF, PF, SF and ZF flags are affected by this instruction, but neither
operand is affected.
Example:
MOV SI, OFFSET F_STRING ; point first string
MOV DI, OFFSET S_STRING ; point second string
MOV CX, 0AH ; set the counter as 0AH
CLD ; clear direction flag to auto increment
REPE CMPSB ; repeatedly compare till unequal or counter = 0

More Related Content

PPTX
instructionsetsofjdtufgmictfgfjh8086.pptx
PDF
PDF
Lecture5(1)
PDF
8086 instructions
PPTX
Mastering Assembly Language: Programming with 8086
PDF
Instructionsetof8086 180224060745(3)
DOCX
Instruction set of 8086
PDF
Intrl 8086 instruction set
instructionsetsofjdtufgmictfgfjh8086.pptx
Lecture5(1)
8086 instructions
Mastering Assembly Language: Programming with 8086
Instructionsetof8086 180224060745(3)
Instruction set of 8086
Intrl 8086 instruction set

Similar to Chapter 5 and 6 instructions and program control instructions.pdf (20)

PPT
Instruction set Madha Insstitute of Engineering
PPT
Unit 2 8086 Instruction set.ppt notes good
PPT
1344 Alp Of 8086
PDF
15CS44 MP & MC Module 2
PPT
Chap 3_2.ppt
PPT
An instruction is a binary pattern designed inside a microprocessor to perfor...
PPT
instruction-set-of-8086-mr-binu-joy3.ppt
PPT
Instruction set of 8086
PPT
INTRUCTION SET OF 8086 FOR MICROPROCESSOR
PPTX
Microprocessor Chapter3 hawassa Univetsi
PDF
BISWAJIT ADHIKARI_28101623024_PC EE 602.pdf
PDF
8086 Register organization and Architecture details
PPTX
Introduction of 8086 micro processor .
PPT
Instruction set of 8085
PPTX
Instruction sets of 8086
PPT
instruction-set-of-8085 (1).ppt
PPTX
Arithmetic instrctions
PPT
Instruction set-of-8085
PDF
Intel8086_Flags_Addr_Modes_sample_pgms.pdf
Instruction set Madha Insstitute of Engineering
Unit 2 8086 Instruction set.ppt notes good
1344 Alp Of 8086
15CS44 MP & MC Module 2
Chap 3_2.ppt
An instruction is a binary pattern designed inside a microprocessor to perfor...
instruction-set-of-8086-mr-binu-joy3.ppt
Instruction set of 8086
INTRUCTION SET OF 8086 FOR MICROPROCESSOR
Microprocessor Chapter3 hawassa Univetsi
BISWAJIT ADHIKARI_28101623024_PC EE 602.pdf
8086 Register organization and Architecture details
Introduction of 8086 micro processor .
Instruction set of 8085
Instruction sets of 8086
instruction-set-of-8085 (1).ppt
Arithmetic instrctions
Instruction set-of-8085
Intel8086_Flags_Addr_Modes_sample_pgms.pdf
Ad

More from Getnet Tigabie Askale -(GM) (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Chapter 1 Introduction to Computer Vision and Image Processing .pdf
PPTX
Fundamentals of Database management system Lab Manual.pptx
PPTX
Chapter 2 and 3 8086,8088 architecture and HW specification.pptx
PDF
Chapter 7 Interrupts in microprocessor and assembly language.pdf
PPTX
Chapter 4 addressing mode in microprocessor.pptx
PDF
chapter 1-part 1 introduction o microprocessor.pdf
PPT
Chapter 1-part 2 introduction to microprocessor.ppt
PPTX
CH2 Mobile Computing in wireless communication.pptx
PPTX
CH4 Wireless Local Area Networks in wireless communication.pptx
PPTX
CH3 Wireless Network Principle in wireless Communication.pptx
PPTX
CH1 Introduction to wireless communication and Mobile Computing.pptx
PPTX
CH5 Cellular Networks in wireless communication.pptx
PPTX
CH6 Mobile Network Layer in wireless communication.pptx
PDF
Chapter 4 FD and normalization edited.pdf
PDF
Chapter 3 Database Modeling short slide.pdf
PDF
Chapter 5 record storage and primary file organization.pdf
PDF
chapter 6 Relational Algebra and calculus.pdf
PDF
chapter 1 Introduction to Database Systems Best.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Chapter 2 Digital Image Fundamentals.pdf
Chapter 1 Introduction to Computer Vision and Image Processing .pdf
Fundamentals of Database management system Lab Manual.pptx
Chapter 2 and 3 8086,8088 architecture and HW specification.pptx
Chapter 7 Interrupts in microprocessor and assembly language.pdf
Chapter 4 addressing mode in microprocessor.pptx
chapter 1-part 1 introduction o microprocessor.pdf
Chapter 1-part 2 introduction to microprocessor.ppt
CH2 Mobile Computing in wireless communication.pptx
CH4 Wireless Local Area Networks in wireless communication.pptx
CH3 Wireless Network Principle in wireless Communication.pptx
CH1 Introduction to wireless communication and Mobile Computing.pptx
CH5 Cellular Networks in wireless communication.pptx
CH6 Mobile Network Layer in wireless communication.pptx
Chapter 4 FD and normalization edited.pdf
Chapter 3 Database Modeling short slide.pdf
Chapter 5 record storage and primary file organization.pdf
chapter 6 Relational Algebra and calculus.pdf
chapter 1 Introduction to Database Systems Best.pdf
Ad

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Electronic commerce courselecture one. Pdf
PDF
KodekX | Application Modernization Development
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Machine learning based COVID-19 study performance prediction
PPT
Teaching material agriculture food technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
MYSQL Presentation for SQL database connectivity
MIND Revenue Release Quarter 2 2025 Press Release
“AI and Expert System Decision Support & Business Intelligence Systems”
Reach Out and Touch Someone: Haptics and Empathic Computing
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Unlocking AI with Model Context Protocol (MCP)
Electronic commerce courselecture one. Pdf
KodekX | Application Modernization Development
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Machine learning based COVID-19 study performance prediction
Teaching material agriculture food technology
Empathic Computing: Creating Shared Understanding
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MYSQL Presentation for SQL database connectivity

Chapter 5 and 6 instructions and program control instructions.pdf

  • 1. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 1 Chapter 5 and 6: Instructions 5.0 INTRODUCTION  An instruction is a binary pattern designed inside a microprocessor to perform a specific function.  In 8086, each instruction consumes one byte to six bytes of memory  The entire group of instructions that a microprocessor supports is called Instruction Set.  8086 has more than 20,000 instructions. 5.1 INSTRUCTION SET OF 8086 The Instruction set of 8086 microprocessor is classified into 7, they are: 1. Data transfer instructions 2. Arithmetic and logical instructions 3. Shift / rotate instructions 4. Program control transfer instructions 5. Machine Control Instructions 6. Flag manipulation instructions 7. String instructions 5.2 DATA TRANSFER INSTRCUTIONS Data transfer instruction, as the name suggests is for the transfer of data from memory to internal register, from internal register to memory, from one register to another register, from input port to internal register, from internal register to output port etc. 1. MOV instruction  It is a general purpose instruction to transfer byte or word from register to register, memory to register, register to memory or with immediate addressing. General Form: o MOV Des, Src  Here the source and destination needs to be of the same size that is either 8 bit or 16 bit.  MOV instruction does not affect any flags.  Both Src and Des cannot be memory location at the same time. Example: MOV BX, 00F2H ; load the immediate number 00F2H in BX register MOV CL, [2000H] ; Copy the 8 bit content of the memory location, at a displacement of 2000H from data segment base to the CL register MOV [589H], BX ; Copy the 16 bit content of BX register on to the memory location, which at a displacement of 589H from the data segment base. MOV DS, CX ; Move the content of CX to DS
  • 2. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 2 2. PUSH instruction  The PUSH instruction decrements the stack pointer by two and copies the word from source to the location where stack pointer now points.  Here the source must be of word size data. Source can be a general purpose register, segment register or a memory location.  The PUSH instruction first pushes the most significant byte to sp-1, then the least significant to the sp-2.  Push instruction does not affect any flags Figure 3.1: PUSH operation. Example: PUSH CX ; Decrements SP by 2, copy content of CX to the stack (figure shows execution of this instruction) PUSH DS ; Decrement SP by 2 and copy DS to stack 3. POP instruction  The POP instruction copies a word from the stack location pointed by the stack pointer to the destination.  The destination can be a General purpose register, a segment register or a memory location. Here after the content is copied the stack pointer is automatically incremented by two.  The execution pattern is similar to that of the PUSH instruction. Example: POP CX ; Copy a word from the top of the stack to CX and increment SP by 2. 4. IN & OUT instructions  The IN instruction copies data from a port to the accumulator. If the data is 8 bit, it goes to AL and if it is 16 bit then it goes to AX. General Form: o IN Accumulator, Port Address  Similarly OUT instruction is used to copy data from accumulator to an output port. General Form:
  • 3. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 3 o OUT Port Address, Accumulator  Both IN and OUT instructions can be done using direct and indirect addressing modes. Example: IN AL, 0F8H ; Copy a byte from the port 0F8H to AL MOV DX, 30F8H ; Copy port address in DX IN AL, DX ; Move 8 bit data from 30F8H port IN AX, DX ; Move 16 bit data from 30F8H port OUT 047H, AL ; Copy contents of AL to 8 bit port 047H MOV DX, 30F8H ; Copy port address in DX OUT DX, AL ; Move 8 bit data to the 30F8H port OUT DX, AX ; Move 16 bit data to the 30F8H port 5. XCHG instruction  The XCHG instruction exchanges contents of the destination and source. General Format: o XCHG Des, Src  Destination and source can be register and register, or register and memory location, but XCHG cannot interchange the value of two memory locations. Example: XCHG BX, CX ; exchange word in CX with the word in BX XCHG AL, CL ; exchange byte in CL with the byte in AL XCHG AX, SUM[BX] ; here physical address, which is DS+SUM+[BX]. The content at physical address and the content of AX are interchanged 5.3 ARITHEMATIC AND LOGIC INSTRCUTIONS The arithmetic and logic group of instructions include: 1. ADD instruction  Add instruction is used to add the current contents of destination with that of source and store the result in destination. General Format: o ADD Des, Src  Here we can use register and/or memory locations. AF, CF, OF, PF, SF, and ZF flags are affected Example: ADD AL, 0FH ; Add the immediate content, 0FH to the content of AL and store the result in AL ADD AX, BX ; AX <= AX+BX ADD AX, 0100H ; IMMEDIATE
  • 4. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 4 ADD AX, BX ; REGISTER ADD AX, [SI] ; REGISTER INDIRECT OR INDEXED ADD AX, [5000H] ; DIRECT ADD [5000H], 0100H ; IMMEDIATE ADD 0100H ; DESTINATION AX (IMPLICT) 2. ADC: ADD WITH CARRY  This instruction performs the same operation as ADD instruction, but adds the carry flag bit (which may be set as a result of the previous calculation in CF) to the result.  All the condition code flags are affected by this instruction. Example: ADC AX, BX ; REGISTER ADC AX, [SI] ; REGISTER INDIRECT OR INDEXED ADC AX, [5000H] ; DIRECT ADC [5000H], 0100H ; IMMEDIATE ADC 0100H ; IMMEDIATE (AX IMPLICT) 3. SUB instruction  SUB instruction is used to subtract the current contents of destination with that of source and store the result in destination. General Format: o SUB Des, Src  Here we can use register and/or memory locations. AF, CF, OF, PF, SF, and ZF flags are affected Example: SUB AL, 0FH ; subtract the immediate content, 0FH from the content of AL and store the result in AL SUB AX, BX ; AX <= AX-BX SUB AX, 0100H ; IMMEDIATE (DESTINATION AX) SUB AX, BX ; REGISTER SUB AX, [5000H] ; DIRECT SUB [5000H], 0100H ; IMMEDIATE 4. SBB: SUBTRACT WITH BORROW  Subtract with borrow instruction subtracts the source operand and the borrow flag (CF) which may reflect the result of the previous calculations, from the destination operand.  Subtraction with borrow, here means subtracting 1 from the subtraction obtained by SUB, if carry (borrow) flag is set.
  • 5. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 5  The result is stored in the destination operand. All the flags are affected (condition code) by this instruction. Example: SBB AX, 0100H ; IMMEDIATE (DESTINATION AX) SBB AX, BX ; REGISTER SBB AX, [5000H] ; DIRECT SBB [5000H], 0100H ; IMMEDIATE 5. CMP: COMPARE  The instruction compares the source operand, which may be a register or an immediate data or a memory location, with a destination operand that may be a register or a memory location.  For comparison, it subtracts the source operand from the destination operand but does not store the result anywhere.  The flags are affected depending upon the result of the subtraction. o If both of the operands are equal, zero flag is set. o If the source operand is greater than the destination operand, carry flag is set or else, carry flag is reset. Example: CMP BX, 0100H ; IMMEDIATE CMP AX, 0100H ; IMMEDIATE CMP [5000H], 0100H ; DIRECT CMP BX, [SI] ; REGISTER INDIRECT OR INDEXED CMP BX, CX ; REGISTER 6. INC & DEC instructions  INC and DEC instructions are used to increment and decrement the content of the specified destination by one.  AF, CF, OF, PF, SF, and ZF flags are affected. Example: INC AL ; AL<= AL + 1 INC AX ; AX<=AX + 1 DEC AL ; AL<= AL – 1 DEC AX ; AX<=AX – 1
  • 6. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 6 7. MUL: Unsigned Multiplication  Unsigned multiplication instruction. It assumes one of the operand is in AL or AX. General Format: o MUL Src  It multiplies two bytes to produce a word or two words to produce a double word. Src can be a register or memory location. AX = AL * Src DX : AX = AX * Src Example: MUL Reg/ Mem ; for byte: (AX) <= (AL) x (Reg8/Mem8) ; for word: (DX)(AX) <= (AX) x (Reg16/Mem16) 8. IMUL: Signed Multiplication  Signed multiplication instruction. IMUL Reg/ Mem ; for byte: (AX) <= (AL) x (Reg8/Mem8) ; for word: (DX)(AX) <= (AX) x (Reg16/Mem16) 9. DIV: Unsigned Division  Unsigned division instruction. It divides word by byte or double word by word. General Format: o DIV Src  The operand is stored in AX, divisor is Src and the result is stored as: o AH = remainder AL = quotient Example: DIV Reg/ Mem ; For 16-bit ÷ 8-bit: (AL) <= (AX) ÷ (Reg8/Mem8) Quotient (AH) <= (AX) MOD (Reg8/Mem8) Remainder ; For 32-bit ÷ 16-bit: (AX) <= (DX)(AX) ÷ (Reg16/Mem16) Quotient (DX) <= (DX)(AX) MOD (Reg16/Mem16) Remainder 10. IDIV: Signed Division  Signed division instruction. Example: IDIV Reg/ Mem ; For 16-bit ÷ 8-bit: (AL) <= (AX) ÷ (Reg8/Mem8) Quotient (AH) <= (AX) MOD (Reg8/Mem8) Remainder ; For 32-bit ÷ 16-bit:
  • 7. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 7 (AX) <= (DX)(AX) ÷ (Reg16.Mem16) Quotient (DX) <= (DX)(AX) MOD (Reg16/Mem16) Remainder 11. AND instruction  This instruction logically ANDs each bit of the source byte/word with the corresponding bit in the destination and stores the result in destination. General Format: o AND Des, Src  The source can be an immediate number, register or memory location. Destination can be a register or memory location.  The CF and OF flags are both made zero, PF, ZF, SF are affected by the operation and AF is undefined. Example: AND BL, AL ; suppose BL=1000 0110 and AL = 1100 1010 then after the operation BL would be BL= 1000 0010. AND CX, AX ; CX <= CX AND AX AND CL, 08 ; CL<= CL AND (0000 1000) 12. OR instruction  This instruction logically ORs each bit of the source byte/word with the corresponding bit in the destination and stores the result in destination. General Format: o OR Des, Src  The source can be an immediate number, register or memory location. Destination can be a register or memory location.  The CF and OF flags are both made zero, PF, ZF, SF are affected by the operation and AF is undefined. Example: OR BL, AL ; suppose BL=1000 0110 and AL = 1100 1010 then after the operation BL would be BL= 1100 1110. OR CX, AX ; CX <= CX AND AX OR CL, 08 ; CL<= CL AND (0000 1000) 13. NOT instruction  The NOT instruction complements (inverts) the contents of an operand register or a memory location, bit by bit. Example: NOT AX ; Before AX= (1011)2= (B)16. After execution AX= (0100)2= (4)16. NOT [5000H]
  • 8. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 8 14. XOR instruction  The XOR operation is carried out in a similar way to the AND and OR operation. The constraints on the operands are also similar.  The XOR operation gives a high output (1), when the two input bits are dissimilar. Otherwise, the output is zero. Example: XOR AX, 0098H XOR AX, BX XOR AX, [5000H] 5.4 SHIFT / ROTATE INSTRCUTIONS  Shift instructions move the binary data to the left or right by shifting them within the register or memory location. They also can perform multiplication of powers of 2+n anddivision of powers of 2-n .  There are two type of shifts logical shifting and arithmetic shifting, later is used with signed numbers while former with unsigned.  Rotate on the other hand rotates the information in a register or memory either from one end to another or through the carry flag. 1. SHL/SAL instruction  Both instruction shifts each bit to left, and places the MSB in CF and LSB is made 0.  The destination can be of byte size or of word size, also it can be a register or a memory location. General Format: o SAL/SHL Des, count  Number of shifts is indicated by the count. All flags are affected. Example: MOV BL, B7H ; BL is made B7H SAL BL, 1; shift the content of BL register one place to left. Before execution: 2. SHR instruction  This instruction shifts each bit in the specified destination to the right and 0 is stored in the MSB position. The LSB is shifted into the carry flag. CY 0 B7 1 B6 0 B5 1 B4 1 B3 0 B2 1 B1 1 B0 1 After the execution: CY B7 B6 B5 B4 B3 B2 B1 B0 1 0 1 1 0 1 1 1 0
  • 9. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 9  The destination can be of byte size or of word size, also it can be a register or a memory location. General Format: o SHR Des, count  Number of shifts is indicated by the count. All flags are affected Example: MOV BL, B7H ; BL is made B7H SHR BL, 1 ; shift the content of BL register one place to the right. Before execution: 3. ROL instruction  This instruction rotates all the bits in a specified byte or word to the left some number of bit positions. MSB is placed as a new LSB and a new CF.  The destination can be of byte size or of word size, also it can be a register or a memory location. General Format: o ROL Des, count  Number of shifts is indicated by the count. All flags are affected Example: MOV BL, B7H ; BL is made B7H ROL BL, 1 ; rotates the content of BL register one place to the left. Before execution: 4. ROR instruction  This instruction rotates all the bits in a specified byte or word to the right some number of bit positions. LSB is placed as a new MSB and a new CF.  The destination can be of byte size or of word size, also it can be a register or a memory location. General Format: o ROR Des, count B7 1 B6 0 B5 1 B4 1 B3 0 B2 1 B1 1 B0 1 CY 0 After execution: B7 B6 B5 B4 B3 B2 B1 B0 CY 0 1 0 1 1 0 1 1 1 CY 0 B7 1 B6 0 B5 1 B4 1 B3 0 B2 1 B1 1 B0 1 After the execution: CY B7 B6 B5 B4 B3 B2 B1 B0 1 0 1 1 0 1 1 1 1
  • 10. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 10  Number of shifts is indicated by the count. All flags are affected Example: MOV BL, B7H ; BL is made B7H ROR BL, 1 ; shift the content of BL register one place to the right. Before execution: 5. RCR instruction  This instruction rotates all the bits in a specified byte or word to the right some number of bit positions along with the carry flag. LSB is placed in a new CF and previous carry is placed in the new MSB.  The destination can be of byte size or of word size, also it can be a register or a memory location. General Format: o RCR Des, count  Number of shifts is indicated by the count. All flags are affected Example: MOV BL, B7H ; BL is made B7H RCR BL, 1 ; shift the content of BL register one place to the right. Before execution: After Execution: B7 B6 B5 B4 B3 B2 B1 B0 CY 0 1 0 1 1 0 1 1 1 B7 1 B6 0 B5 1 B4 1 B3 0 B2 1 B1 1 B0 1 CY 0 After execution: B7 B6 B5 B4 B3 B2 B1 B0 CY 1 1 0 1 1 0 1 1 1 B7 B6 B5 B4 B3 B2 B1 B0 CY 1 0 1 1 0 1 1 1 0
  • 11. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 11 5.5 PROGRAM CONTROL INSTRCUTIONS  These instructions cause change in the sequence of the execution of instruction.  This change can be through a condition or sometimes unconditional.  There are two types of Program control transfer instructions:  Unconditional transfer instructions – JMP, CALL, RET  Conditional transfer instructions – J condition 1. Unconditional JUMP (JMP XXX) o Short Jump o Near Jump o Far jump  Short and near jump are often called intrasegment jump and far jumps are often called intersegment jump  Short jump and near jump follows a distance or displacement to jump where as far jump follows an address (segment + offset) to jump A. Short JUMP (JMP 1byte-displacement) • Short jump is a two-byte instruction. • Instead of a jump address, it jumps by following a 8-bit (one byte) signed displacement. • It allows jumps or branches to memory location within +127 and -128 bytes from the address following the jump. • The displacement is sign-extended and added to the instruction pointer (IP) to generate the jump address within the current code segment 1 byte 1byte JMP disp; here disp is 8-bit signed Displacement or distance Example: JMP 04H
  • 12. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 12 65536 1024 B. Near Jump (JMP 2byte-displacement) • Near jump is similar to short jump, except that the distance is farther. • Near jump is a three-byte instruction. • Displacement is 16-bit (2 byte) signed displacement. • It allows jumps or branches to memory location within ±32 𝐾 𝑏𝑦𝑡𝑒𝑠 of current code segment.  216 = 65536 = 64 𝐾𝑏𝑦𝑡𝑒 = −32𝐾𝑏𝑦𝑡𝑒 𝑡𝑜 + 32𝐾𝑏𝑦𝑡𝑒 • The signed displacement added to the instruction pointer (IP) to generate the jump address. C. Far Jump (JMP 4byte-displacement) • Afar jump instruction obtain a new segment and offset address to accomplish the jump. • It is a 5 byte instruction. • Byte 2 and 3 contain new offset address. Byte 4 and 5 contains new segment address. • It allows jumps or branches to any memory location of any memory segment. That is why far jump is called intersegment jump. Example: JMP 0002H
  • 13. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 13 Example: JMP 0127: A300 Jump to CSX10+IP = A300X10+0127 = A3127 2. CALL instruction o The CALL instruction transfer the flow of the program to the procedure. The CALL instruction differ from a jump instruction because a CALL saves a return address on the stack. Whenever a CALL instruction executes it:  Pushes the IP or, CS: IP on the stack.  Changes the value of IP or, CS: IP. Jumps to the procedure by new IP or, CS: IP address Difference between JMPand CALL instruction JMP CALL Doesn’t use stack Uses stack Doesn’t return to the next instruction of JMP Must return to the next instruction of CALL Used to jump to new location Used to execute subroutine Has conditional and unconditional jump 8086 has only unconditional call
  • 14. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 14 Types of CALL (a) Near CALL (b) Far CALL Difference between Near CALLand Far Call Near CALL Far CALL (1) Procedure located within the same code segment (±32KB) (1) Procedure located in the entire memory (1 MB) (2) 3-byte instruction (2) 5-byte instruction (3) Only IP content is replaced by (IP±displacement) (3) Both CS and IP contents are replaced by new CS and IP address (4) Stack stores only return IP address (2 byte) (4) Stack stores the return CS and IP address. (4 byte) 3. RET instruction o RET instruction returns execution from a procedure to the next instruction after the CALL instruction in the calling program. o If it was a near call, then IP is replaced with the value at the top of the stack, if it had been a far call, then another POP of the stack is required. o This second popped data from the stack is put in the CS, thus resuming the execution of the calling program. o RET instruction can be followed by a number, to specify the parameters passed. RET instruction does not affect any flags. General format: • RET Example: p1 PROC ; procedure declaration. MOV AX, 1234h RET ; return to caller. p1 ENDP 4. Conditional JUMP o A conditional jump instruction allows the programmer to make decision based upon numerical tests. o The conditional jump instructions are always short jump in 8086. o Conditional jump instructions test the following flag bits: sign (S), zero (O), carry (C), parity (P) and overflow (O). o If the condition under test is true, a branch to the label associated with the jump instruction occurs. If the condition is false, the next sequential step in the program
  • 15. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 15 executes. o For example, a JC will jump if the carry bit is set. Examples of some common conditional JUMP Assembly language Tested Condition Operation JNE or, JNZ Z=0 Jump if not equal or jump if not zero JE or JZ Z=1 Jump if equal or jump if zero JNO O=0 Jump if no overflow JNP or JPO P=0 Jump if no parity of jump if parity odd JP or JPE P=1 Jump if parity or jump if parity even 5. Iteration control instructions o These instructions are used to execute a series of instructions some number of times. o The number is specified in the CX register, which will be automatically decremented in course of iteration. But here the destination address for the jump must be in the rangeof - 128 to 127 bytes. Example: Instructions here are:- LOOP ; loop through the set of instructions until CX is 0 LOOPE/LOOPZ ; here the set of instructions are repeated until CX=0 or ZF=0 LOOPNE/LOOPNZ ; here repeated until CX=0 or ZF=1 6. Controlling the flow of the Program o It is much easier to use the assembly language statements .IF, .ELSE, .ELSEIF, and .ENDIF to control the flow of the program than it is to use the correct conditional jump statement. o Other statements: .REPEAT - .UNTIL and .WHILE - .ENDW, .BREAK, .CONTINUE o These statements can replace the conditional jump and loop statements 7. Procedures o A procedure is a group of instructions (subroutine or function) that usually perform a specific task. Advantages: (a) It is reusable section of the software that is stored in memory once, but used as often as necessary.
  • 16. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 16 (b) It saves memory space. (c) Makes easier to develop software Disadvantages It takes the compiler a small amount of time to link the procedure and return from it How procedure links with main program o The CALL instruction links to the procedure and the RET (return) instruction return from the procedure o The CALL instruction pushes the address (return address) of the instruction following the CALL on the stack. The RET instruction removes an address from the stack so the program return to the instruction following the CALL. o A procedure begins with the PROC directive and ends with the ENDP directive. The PROC directive is followed by the type of procedure: NEAR (intrasegment) or FAR (intersegment) Format of a procedure Example XXX PROC NEAR/FAR SUMS PROC NEAR 5.6 MACHINE CONTROL INSTRCUTIONS 1. HLT instruction  The processor enters into a halt state. The processor gets out of this Halt signal upon an interrupt signal in INTR pin/NMI pin or a reset signal on RESET input.  General form: o HLT 2. WAIT instruction  When this instruction is executed, the 8086 enters into an idle state. This idle state is continued till a high is received on the TEST input pin or a valid interrupt signal is received.  Wait affects no flags. It generally is used to synchronize the 8086 with a peripheral device(s). …………………………………….. ADD AX,BX …………………………………….. ADD AX,CX …………………………………….. ADD AX,DX RET RET XXX ENDP SUMS ENDP
  • 17. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 17 3. ESC instruction  This instruction is used to pass instruction to a coprocessor like 8087. There is a 6 bit instruction for the coprocessor embedded in the ESC instruction.  In most cases the 8086 treats ESC and a NOP, but in some cases the 8086 will access data items in memory for the coprocessor 4. LOCK instruction  In multiprocessor environments, the different microprocessors share a system bus, which needed to access external devices like disks.  LOCK instruction is given as prefix in the case when a processor needs exclusive access of the system bus for a particular instruction. It affects no flags. 5. NOP instruction  At the end of NOP instruction, no operation is done other than the fetching and decoding of the instruction.  NOP affects no flags. 5.7 FLAG MANIPULATION INSTRCUTIONS 1. STC instruction  This instruction sets the carry flag. It does not affect any other flag. 2. CLC instruction  This instruction resets the carry flag to zero. CLC does not affect any other flag. 3. CMC instruction  This instruction complements the carry flag. CMC does not affect any other flag. 4. STD instruction  This instruction is used to set the direction flag to one so that SI and/or DI can be decremented automatically after execution of string instruction. STD does not affect any other flag. 5. CLD instruction  This instruction is used to reset the direction flag to zero so that SI and/or DI can be incremented automatically after execution of string instruction. CLD does not affect any other flag. 6. STI instruction  This instruction sets the interrupt flag to 1. This enables INTR interrupt of the 8086. STI does not affect any other flag. 7. CLI instruction
  • 18. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 18  This instruction resets the interrupt flag to 0. Due to this the 8086 will not respond to an interrupt signal on its INTR input. CLI does not affect any other flag. 5.8 STRING INSTRCUTIONS  String is a sequence of bytes or words  8086 instruction set includes instruction for string movement, comparison, scan, load and store.  REP instruction prefix : used to repeat execution of string instructions  String instructions end with S or SB or SW. S represents string, SB string byte and SW string word.  Offset or effective address of the source operand is stored in SI register and that of the destination operand is stored in DI register.  Depending on the status of DF, SI and DI registers are automatically updated.  DF = 0 => SI and DI are incremented by 1 for byte and 2 for word.  DF = 1 => SI and DI are decremented by 1 for byte and 2 for word. 1. MOVS/MOVSB/MOVSW  These instructions copy a word or byte from a location in the data segment to a location in the extra segment.  The offset of the source is in SI and that of destination is in DI. For multiple word/byte transfers the count is stored in the CX register.  When direction flag is 0, SI and DI are incremented and when it is 1, SI and DI are decremented.  MOVS affect no flags. MOVSB is used for byte sized movements while MOVSW is for word sized. Example: CLD ; clear the direction flag to auto increment SI and DI MOV AX, 0000H ; MOV DS, AX ; initialize data segment register to 0 MOV ES, AX ; initialize extra segment register to 0 MOV SI, 2000H ; Load the offset of the string1 in SI MOV DI, 2400H ; Load the offset of the string2 in DI MOV CX, 04H ; load length of the string in CX REP MOVSB ; decrement CX and MOVSB until CX will be 0
  • 19. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 19 MOVSB MA = (DS) x 10 + (SI) MAE = (ES) x 10 + (DI) (MAE) <= (MA) If DF = 0, then (DI) <= (DI) + 1; (SI) <= (SI) + 1 If DF = 1, then (DI) <= (DI) - 1; (SI) <= (SI) – 1 MOVSW MA = (DS) x 10 + (SI) MAE = (ES) x 10 + (DI) (MAE ; MAE + 1) <= (MA; MA + 1) If DF = 0, then (DI) <= (DI) + 2; (SI) <= (SI) + 2 If DF = 1, then (DI) <= (DI) - 2; (SI) <= (SI) - 2 2. REP/REPE/REP2/REPNE/REPNZ  REP is used with string instruction; it repeats an instruction until the specified condition becomes false.  REP => CX=0  REPE/REPZ => CX=0 OR ZF=0  REPNE/REPNZ => CX=0 OR ZF=1 Example: REP MOVSB STR1, STR2 3. LODS / LODSB /LODSW  Load a byte or word in AL or AX  Copies byte or word from memory location pointed by SI into AL or AX register. Example: LODSB S_STRING LODSB MA = (DS) x 10 + (SI) (AL) <= (MA) If DF = 0, then (SI) <= (SI) + 1 If DF = 1, then (SI) <= (SI) – 1 LODSW MA = (DS) x 10 + (SI) (AX) <= (MA ; MA + 1) If DF = 0, then (SI) <= (SI) + 2 If DF = 1, then (SI) <= (SI) – 2 4. STOS / STOSB / STOSW  Store byte or word in a string. It does not affect any flags.  Copies a byte or word contained in AL or AX to memory location pointed by DI. Example: STOS D_STRING
  • 20. CoSc-3025: Microprocessor and Assembly Language Programming Page 5- 20 STOSB MAE = (ES) x 10 + (DI) (MAE) <= (AL) If DF = 0, then (DI) <= (DI) + 1 If DF = 1, then (DI) <= (DI) – 1 STOSW MAE = (ES) x 10 + (DI) (MAE ; MAE + 1 ) <= (AX) If DF = 0, then (DI) <= (DI) + 2 If DF = 1, then (DI) <= (DI) – 2 5. CMPS / CMPSB / CMPSW  Compare string bytes or string words.  The comparison is affected by subtraction of content pointed by DI from that pointed by SI. The AF, CF, OF, PF, SF and ZF flags are affected by this instruction, but neither operand is affected. Example: MOV SI, OFFSET F_STRING ; point first string MOV DI, OFFSET S_STRING ; point second string MOV CX, 0AH ; set the counter as 0AH CLD ; clear direction flag to auto increment REPE CMPSB ; repeatedly compare till unequal or counter = 0