Program Control Instruction
Program Control Instruction
Prepared By:
Mohammed Abdul kader
Lecturer, EEE, IIUC
Jump (JMP) instruction
Jump (JMP) instruction allows the programmer to skip sections of a program and branch to
any part of the memory for the next instruction. Jump are two types:
• Unconditional Jump
• Conditional Jump
• Short Jump
• Near Jump
• 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.
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 2
Jump (JMP) instruction (Continued)
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 1 byte
JMP disp ; here disp is 8-bit signed
displacement or distance
Example:
JMP 04H
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 3
Jump (JMP) instruction (Continued)
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.
65536
216 = 65536 = 64 𝐾𝑏𝑦𝑡𝑒 = −32𝐾𝑏𝑦𝑡𝑒 𝑡𝑜 + 32𝐾𝑏𝑦𝑡𝑒
1024
• The signed displacement added to the instruction pointer (IP) to generate the jump address.
Example:
JMP 0002H CS=1000
IP=0003H
New IP= 0005
4
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC
Jump (JMP) instruction (Continued)
Far Jump (JMP 4byte-displacement)
• A far 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’s why
far jump is called intersegment jump.
Example:
JMP 0127: A300
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 5
Jump (JMP) instruction (Continued)
Conditional Jump
• A conditional jump instruction allows the programmer to make decision based upon
numerical tests.
• The conditional jump instructions are always short jump in 8086.
• Conditional jump instructions test the following flag bits: sign (S), zero (O), carry (C),
parity (P) and overflow(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
executes. For example, a JC will jump if the carry bit is set.
Example of some common conditional jump
Problem:
Using LOOP instruction write assembly language program to find out the sum of following
series.
1+2+3+……………………….+100
Solution:
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 8
PROCEDURES
Advantages:
(a) It is reusable section of the software that is stored in memory once, but used as often as
necessary.
(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.
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 9
PROCEDURES (continued)
How procedure links with main program
The CALL instruction links to the procedure and the RET (return) instruction return
from the procedure.
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.
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 Procedure Example:
N.B
XXX is the name of level (both level name should be same)
To call a procedure in main program write: CALL XXX
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 10
PROCEDURES (continued)
CALL instruction
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.
JMP CALL
Doesn’t use stack Uses stack
Doesn’t return to the next instruction of Must return to the next instruction of
JMP CALL
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 11
PROCEDURES (continued)
Types of CALL
(a) Near CALL (b) Far CALL
Difference between Near CALL and Far Call
(1) Procedure located within the same code (1) Procedure located in the entire memory
segment (±32KB) (1 MB)
(2) 3-byte instruction (2) 5-byte instruction
(3) Only IP content is replaced by (3) Both CS and IP contents are replaced by
(IP±displacement) 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)
RET instruction
The return (RET) instruction removes a 16-bit number (near return) from the stack and
places it into IP or removes a 32-bit number (far return) and places it into IP and CS.
The near and far return instructions are both defined in the procedure’s PROC directive,
which automatically selects the proper return instruction.
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 12
INTERRUPTS
Definition
An interrupt is either a hardware-generated CALL (externally derived from a hardware
signal) or a software-generated CALL (internally derived from the execution of an
instruction or some other internal event) that allow normal program execution to be
interrupted (stopped).
In response to an interrupt, the microprocessor stops execution its current program and
calls a procedure called interrupt service procedure (ISP).
An IRET instruction at end of the interrupt-service procedure returns execution to the
interrupted program.
Instruction: INT nn ; where nn indicates interrupt vector number (0 to 255)
Each INT instruction is 2-byte long .1st byte contain opcode and 2nd byte contains
vector type number. (exception: INTO and INT3 both are 1-byte instruction)
Types
The 8086 interrupts can be classified into three types. These are:
1. Predefined interrupts
2. User-defined software interrupts
3. User-defined hardware interrupts
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 13
INTERRUPTS (Continued)
Interrupt Vectors
Interrupt vector is the 4 byte long (CS:IP) address of interrupt service procedure stored in
the first 1024 bytes (out of 1Mbytes) of the memory (00000-003FFH). This memory
location (1024 byte) is known as interrupt vector table.
There are 256 different interrupt vectors, and each vector contains 4 byte address of ISP.
The first two bytes contain the IP and last two byte contains the CS.
8086 interrupt vector table /pointer table
Instruction: INT nn ;
where nn indicates interrupt vector number
Type 1 (Single step execution): Once TF is set to one, the 8086 automatically generates a
TYPE 1 interrupt after execution of each instruction.
Type 2 (NMI pin): The nonmaskable interrupt is initiated via the 8086 NMI pin. It is edge
triggered (LOW to HIGH) and must be active for two clock cycles to guarantee recognition.
It is normally used for catastrophic failures such as power failure.
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 16
INTERRUPTS (Continued)
PREDEFINED INTERRUPTS (0 TO 4)- Continued
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 17
INTERRUPTS (Continued)
Consequences of Software interrupt instruction (INT instruction)
Whenever a software interrupt executes it:
Pushes flags onto stack.
Clears the T and I flag bits.
Pushes CS onto stack.
Fetches new CS from vector table.
Pushes IP onto stack.
Fetches new IP from vector table.
Jumps to service procedure pointed by new CS:IP.
IRET instruction
The interrupt return instruction (IRET) is used only with software and hardware interrupt
service procedure.
It is a special return instruction which perform following task-
• POP stack data back into the IP.
• POP stack data back into CS.
• POP stack data back into the flag register
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 18
INTERRUPTS (Continued)
Why we need to clear T and I flag in case of software interrupt?
I flag controls the external hardware interrupt. During software interrupt I flag is cleared
to prevent hardware interrupt, because microprocessor does not allow hardware and
software interrupt simultaneously.
T flag is cleared to stop debugging so that no debugging occurs during interrupt.
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 19
Miscellaneous Control Instruction
Controlling the carry flag bit
STC= Sets the carry flag.
CLC= Clears the carry flag.
CMC= Complements the carry flag.
NOP
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 20
Miscellaneous Control Instruction (Continued)
WAIT instruction
Wait instruction monitors the 𝑇𝐸𝑆𝑇 pin of 8086 microprocessor. If WAIT instruction
executes while 𝑇𝐸𝑆𝑇 pin is 1(high), nothing will happen.
If WAIT instruction executes while 𝑇𝐸𝑆𝑇 pin is low microprocessor waits until 𝑇𝐸𝑆𝑇
pin becomes 1(high).
Lecture materials on "Program Control Instructions", Prepared by: Mohammed Abdul Kader, Lecturer, EEE, IIUC 21