8086 Instructions 2
8086 Instructions 2
(ENEX 201)
Sharad Kumar Ghimire
Department of Electronics and Computer Engineering
Pulchowk Campus
Institute of Engineering
Tribhuvan University
Nepal
Chapter 3
S. K. Ghimire
Contents
8086 Microprocessor Instructions:
Rotate & Shift: RCL, RCR, ROL, ROR, SAL/SHL, SAR, SHR
Flag Related: STC, CLC, CMC, STD, CLD, STI, CLI, LAHF, SAHF
The operation is circular because the MSB of the operand is rotated into the carry
flag and the bit in the carry flag is rotated around into LSB of the operand
To rotate the operand by one bit position, we can specify this by putting a 1 in the
count position of the instruction
To rotate by more than one bit position, load the desired number into the CL
register and put “CL” in the count position of the instruction
The operation circular because the LSB of the operand is rotated into the carry
flag and the bit in the carry flag is rotate around into MSB of the operand
To rotate the operand by one bit position, we can specify this by putting a 1 in the
count position of the instruction
To rotate more than one bit position, load the desired number into the CL register
and put “CL” in the count position of the instruction
Data bit rotated out of MSB is circled back into the LSB. It is also copied into CF
To rotate the operand by one bit position, we can specify this by putting 1 in the
count position in the instruction
To rotate more than one bit position, load the desired number into the CL register
and put “CL” in the count position of the instruction
The bit is moved out of the LSB is rotated around into the MSB. The data bit
moved out of the LSB is also copied into CF
To rotate the operand by one bit position, we can specify this by putting 1 in the
count position in the instruction
To rotate by more than one bit position, load the desired number into the CL
register and put “CL” in the count position of the instruction
Shift each bit in the specified destination some number of bit positions to the left
During left shift a 0 is inserted in the LSB position and MSB will be shifted into CF
To shift the operand by one bit position, we can specify this by putting a 1 in the
count position of the instruction, whereas for shifts of more than 1 bit position, load
the desired number of shifts into the CL register, and put “CL” in the count position
of the instruction
SAR – SAR Destination, Count
Shifts each bit of the destination by given number of bit positions to the right
While shifting from MSB to LSB a copy of the old MSB is put in the MSB position,
i.e., the sign bit is copied into the MSB and LSB will be shifted into CF
To shift the operand by one bit position, we can specify this by putting a 1 in the
count position of the instruction, whereas to shift by more than 1 bit position, load
the desired number of shifts into the CL register, and put “CL” in the count position
of the instruction
SHR – SHR Destination, Count
Shifts each bit of the destination by given number of bit positions to the right
To shift the operand by one bit position, we can specify this by putting a 1 in the
count position of the instruction, whereas for shifts of more than 1 bit position, load
the desired number of shifts into the CL register, and put “CL” in the count position
of the instruction
Transfer of Control Instructions
JMP (Unconditional Jump to Specified Destination)
Causes the microprocessor to fetch the next instruction from the location specified
in this JMP instruction rather than from immediate next location after the JMP
instruction
If the destination is in the same code segment as the JMP instruction, then only
the instruction pointer will be changed to get the destination location - near jump
If the destination for jump instruction is in a different segment from that of the
segment containing JMP instruction, then both the instruction pointer and the code
segment register content will be changed to get the destination location - far jump
Conditional Jump instructions work on the basis of specified flag bit status
If the given condition is satisfied the Jump takes place in given address, otherwise
the next instruction after Jump will be fetched and executed
Unlike 8085 some of these conditional Jump instructions can check more than one
flags also
JO Jump if overflow OF
There two basic type of calls: near call and far call
Near call:
A call to a procedure, within the same code segment as the CALL instruction
While executing CALL instruction, stack pointer is decremented by 2 and the offset
of the next instruction after the CALL is pushed into the stack then IP is loaded
with the address specified in CALL
CALL (CALL a Procedure)
Far call:
A call to a procedure, that is in a different segment from the one that contains the
CALL instruction
While executing a far CALL, the stack pointer is decremented by 2 and CS register
is pushed into stack. Then the stack pointer is again decremented by 2 and the
offset address of the instruction after the CALL instruction is also pushed into the
stack
Finally, it loads CS with the segment address of the segment that contains the
procedure, and loads IP with the offset address of the first instruction of the
procedure in that segment
RET (Return from a Procedure to Calling Procedure)
Returns execution from a called procedure to the next instruction after the CALL
instruction which was used to call the procedure
If the procedure is near procedure (in the same code segment as the CALL
instruction), then the return will be done by replacing the IP with a word from the
top of the stack
If the procedure is a far procedure (in a code segment other than the one from
which it is called), then the instruction pointer will be replaced by the word at the
top of the stack. The stack pointer will then be incremented by 2. The CS register
is then replaced with a word from the new top of the stack
This form is used to increment the stack pointer over parameters passed to the
procedure on the stack
For e.g. while executing RET 6 instruction, the stack pointer will be incremented
by an additional six addresses after the IP and CS are popped off the stack
Flag Related Instructions
Flag Manipulation Instructions
STC (Set carry flag): Sets the carry flag to 1
STI (Set interrupt flag): Setting the interrupt flag to a 1 (to enable INTR interrupt)
CLI (Clear interrupt flag): Resets the interrupt flag to 0 (to disable INTR interrupt)
Flag Register Related Instructions
LAHF (Copy low byte of Flag register to AH register)
It can then be pushed onto the stack along with AL by a PUSH AX instruction
Replaces the low-byte of the 8086 flag register with a byte from the AH
register
The stack segment register and the stack pointer must be initialized before this
instruction can be used
PUSH can be used to save data on the stack so that it will not destroyed by a
procedure
After the word is copied to the specified destination, the stack pointer is
automatically incremented by 2 to point to the next word on the stack
Decrements the stack pointer by 2 and copies a word in the flag register to
two memory locations in stack pointed to by the stack pointer
Copies a word from two memory locations at the top of the stack to the flag
register and increments the stack pointer by 2
Input - Output Instructions
IN – IN Accumulator, Port
Copies data from a port to the AL or AX register
If an 8-bit port is read, the data will go to AL and if a 16-bit port is read, the data
will go to AX
IN instruction has two possible formats, fixed port and variable port
Variable-port: port address is loaded into the DX register before the IN instruction,
E.g. IN AX, DX ;Input a word from 16-bit port given in DX to AX
OUT instruction has two possible forms, fixed port and variable port.
INT – INT Type: Type refers to a number between 0 and 255 (the interrupt no.)
When an 8086 executes an INT instruction, it will
● Decrease the stack pointer by 2 and push the flags on to the stack
● Decrease the stack pointer by 2 and push the content of CS onto the stack
● Decrease the stack pointer by 2 and push the offset of the next instruction after the INT
on the stack
● Get a new value for IP from an absolute memory address of 4 times the type specified
in the instruction
● Get a new for value for CS from an absolute memory address of 4 times the type
specified in the instruction plus 2
● Reset both IF and TF, but other flags are not affected
Miscellaneous Instructions
INTO (Interrupt on overflow): If the OF is set, this instruction causes the 8086 to
do an indirect far call to a procedure to handle the overflow condition
Used at the end of the interrupt service procedure to return execution to the
interrupted program
To do return 8086 copies the saved value of IP from the stack to IP, the stored
value of CS from the stack to CS, and the stored value of the flags back to the flag
register
String Manipulation Instructions
String Manipulation Instructions
MOVS – MOVS Destination String Name, Source String Name
Write an Assembly program for 8086 to add two numbers and store the result
TITLE to add two numbers and store the sum
.MODEL SMALL
.STACK 32
.DATA
VAL1 DB 12H
VAL2 DB 78H
SUM DB ?
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
MOV AL,VAL1
ADD AL,VAL2
MOV SUM,AL
MOV AX,4C00H
INT 21H
MAIN ENDP
END MAIN
A program for 8086 to add two numbers
Title AddNo.asm Program to add numbers
.MODEL SMALL
.STACK 100H
.DATA
VAL1 DW 4567H
VAL2 DW 9821H
SUM DW ?
.CODE
MAIN PROC FAR
MOV AX, @DATA
MOV DS, AX
MOV DX, VAL1
ADD DX, VAL2
MOV SUM, DX
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
Write an assembly language program for 8086 to add ten numbers stored in
memory and store the sum in a variable.
Write an assembly language program for 8086 to find the largest number from
given array of ten numbers stored in memory.