8086 Control Flow Instructions Guide
8086 Control Flow Instructions Guide
1
Contents
sequential flow.
Can skip one or group of instruction
4
Jump (JMP) instruction
Example: shows how jump instructions pass control from one part
of the program to another. It also illustrates the use of a label (a
symbolic name for a memory) with the jump instruction.
;JMP operand
XOR BX,BX
;operand=label
START: MOV AX,1
MOV AL,02H
ADD AX,BX
MOV BL,04H
JMP NEXT
ADD BL,AL
MOV CX,200H
JMP ABC ; IP Value 0006=>000E
NEXT: MOV BX,AX
MOV AX,1023H
JMP START
MOV CX,4524H
5 ABC: MOV BH,BL
JMP (UNCONDITIONAL JUMP TO SPECIFIED
DESTINATION)
This instruction will fetch the next instruction from the location specified in
the instruction rather than from the next location after the JMP instruction.
If the destination is in the same as code segment as the JMP instruction, then
only the instruction pointer will be changed to get the destination location. This
is referred to as a near jump.
If the destination for the jump instruction is in a segment with a name different
from that of the segment containing the JMP instruction, then both the
instruction pointer and the code segment register content will be changed to get
the destination location. This referred to as a far jump.
The JMP instruction does not affect any flag.
6
Jump (JMP) instruction
Unconditional Jump (JMP XXX)
In the unconditional jump, the control is transferred unconditionally to the target
label.
It does not depend any condition or numerical tests.
Unconditional Jump types:
Intrasegment: branch is with in the same segment.
Value of the offset register will only change ,the value of the segment register
will remain same.
Short and near jump are often called intrasegment jump
Intersegment: branch is in a different segment.
Value of both the offset register address and the segment address will change.
far jumps are often called intersegment jump.
Short jump and near jump follows a distance or displacement to jump where as
7
far jump follows an address (segment + offset) to jump.
Jump (JMP) instruction
8
Jump (JMP) instruction
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.
9
Jump (JMP) instruction
Example: JMP 04 H
;JMP operand
;operand=direct
MOV AL,02H
JMP 04H ; 04=>IP
MOV AX,50H
HLT
10
Jump (JMP) instruction
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.
In NEAR the control is transferred within the current code segment (intrasegment). IP is
changed.
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𝐾𝑏𝑦𝑡 65536 = −32𝐾𝑏𝑦𝑡𝑒 𝑡𝑜+32𝐾𝑏𝑦𝑡𝑒
1024
The signed displacement added to the instruction pointer (IP) to generate the jump
address.
Intersegment: this is a jump out of the current segment. This type of jump
requires modification of the contents of both CS and IP.
Far Jump (JMP 4byte-displacement)
A far jump instruction obtain a new segment and offset address to accomplish the
jump. Format → JMP far Label (32 bit label)
It is a 5 byte instruction. Byte 2 and 3 contain new offset address. Byte 4 and 5
contains new segment address.
14
Jump (JMP) instruction
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.
Format: Jcc Label => cc = NZ,Z,C,NC,BE,BA …..
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.
In the conditional jump, control is transferred to a new location if a certain
condition is met. JC (jump if carry) [CF is checked], JNZ (jump if not
zero) [ZF is checked].
15
Some common conditional jump instructions are:
18
Controlling the Flow of the Program
LOOP Instruction
LOOP LABEL=>Loop to LABEL if CX is not zero
If CX ≠ 0, it jumps to the address indicated by the label.
when CX (the count register) becomes 0, the next sequential instruction executes.
EXAMPLES:
PRICES DB 10H,20H,30H,45H ; Declare array of 4 bytes named PRICE
;and initialize them with specified values.
MOV BX, OFFSET PRICES ;Point BX at first element in array
MOV CX, 5H ;Load CX with number of elements in array
NEXT: MOV AL, [BX] ; Get element from array
INC AL ;Increment the content of AL
MOV [BX], AL ;Put result back in array
INC BX ;Increment BX to point to next location
LOOP NEXT ;Repeat until CX=0
MOV CX, 0005 MOV AX, 00H
XOR BX,BX MOV BX,02H
L1: MOV AX,01 MOV CX,03H
ADD AX,BX XYZ: ADD AX,BX
MOV BX,AX LOOP XYZ
19
LOOP L1 HLT
Procedures
A procedure is a group of instructions (subroutine or function) that
usually perform a specific task.
Advantages and Disadvantages of using procedure :
Advantages :
Allows to save memory space.
Program development becomes easier.
Debugging of errors in program become easy.
Reduced size of program
Reusability of procedure.
Disadvantages :
CALL and RET instructions are always required to integrate with
procedures.
Requires the extra time to link procedure and return from it.
For small group of instructions, linking and returning back time more
than the execution time, hence for small group of instructions
20
procedures cannot be preferred.
Procedures
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).
21
Procedures
Format of Procedure
SUMOF PROC
ADD AX,BX ;AX=AX + second number
ADD AX,CX ; AX=AX + third number
RET ;return to caller
SUMOF ENDP
23
The 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.
To invoke a procedure, the call instruction used.
The CALL instruction has the following format
CALL procedure_name
Example on calling the procedure SUMOF
Caller passes actual parameters in AX,BX, and CX before calling procedure
SUMOF
MOV AX,NUM1 ;Pass first parameter in AX
MOV BX,NUM2 ;Pass second parameter in BX
MOV CX,NUM3 ;Pass third parameter in CX
CALL SUMOF ;result is in AX
MOV SUM,AX ;Save result in variable SUM
24 CALL SUMOF will call the procedure SUMOF
CALL (CALL A PROCEDURE)
The CALL instruction is used to transfer execution to a subprogram or a
procedure. There two basic type of calls near and far.
A near call is a call to a procedure, which is in the same code segment as
the CALL instruction.
When the 8086 executes a near CALL instruction, it decrements the stack
pointer by 2 and copies the offset of the next instruction after the CALL into
the stack.
This offset saved in the stack is referred to as the return address, because
this is the address that execution will return to after the procedure is
executed.
A near CALL instruction will also load the instruction pointer with the
offset of the first instruction in the procedure.
A RET instruction at the end of the procedure will return execution to the
offset saved on the stack which is copied back to IP.
25
CALL (CALL A PROCEDURE)
A far call is a call to a procedure, which is in a different segment
from the one that contains the CALL instruction.
When the 8086 executes a far call, it decrements the stack pointer
by 2 and copies the content of the CS register to the stack. It then
decrements the stack pointer by 2 again and copies the offset of the
instruction after the CALL instruction to the stack.
Finally, it loads CS with the segment base of the segment that
contains the procedure, and loads IP with the offset of the first
instruction of the procedure in that segment.
A RET instruction at the end of the procedure will return execution
to the next instruction after the CALL by restoring the saved values
of CS and IP from the stack.
26
CALL (CALL A PROCEDURE)
Difference between Near CALL and Far Call
27
RET instruction
RET (RETURN EXECUTION FROM PROCEDURE TO
CALLING PROGRAM)
The RET instruction will return execution from a 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.
The word from the top of the stack is the offset of the next
instruction after the CALL. This offset was pushed into the stack as
part of the operation of the CALL instruction.
The stack pointer will be incremented by 2 after the return address is
popped off the stack.
28
RET instruction
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.
This word is the offset part of the return address put there
by the CALL instruction. 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 word is the segment base part
of the return address that was pushed onto the stack by a
far call operation.
After this, the stack pointer is again incremented by 2.
29
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.
A RET instruction can be followed by a number, for example, RET
6. In this case, the stack pointer will be incremented by an
additional six addresses after the IP when the IP and CS are popped
off the stack. This form is used to increment the stack pointer over
parameters passed to the procedure on the stack.
The RET instruction does not affect any flag.
30
RET instruction
Example: CALL MULT
This is a direct within segment (near or intra segment) call. MULT is the name of
the procedure. The assembler determines the displacement of MULT from the
instruction after the CALL and codes this displacement in as part of the
instruction.
Example: CALL BX
This is an indirect within-segment (near or intra-segment) call. BX contains the
offset of the first instruction of the procedure. It replaces content of IP with
content of register BX.
Example: CALL FAR DIVIDE
This is a direct call to another segment (far or inter-segment call). DIVIDE is the
name of the procedure. The procedure must be declared far with DIVIDE PROC
FAR at its start. The assembler will determine the code segment base for the
segment that contains the procedure and the offset of the start of the procedure. It
will put these values in as part of the instruction code.
31
How a procedure call/return work?
How does a procedure Know where to return?
There can be multiple calls to same procedure in a program
Procedure has to return differently for different calls
It knows by saving the return address(RA) on the stack
This is the address of next instruction after CALL
The CALL instruction calls procedure
PUSH IP(Push offset of next instruction on the stack)
JMP procedure_address(copies the address of the called
procedure into IP)
The RET(return) instruction does the following
POP IP(POP top of stack into IP)
32
How a procedure call/return work?
Example:
main PROC
MOV AX,1000H ;Pass first parameter in AX main proc
MOV BX,2000H ;Pass second parameter in BX MOV AX,2H
MOV CX,3000H ;Pass third parameter in CX CALL SUB1
CALL SUMOF ;result is in AX MOV BX,3H
MOV DX,AX ;Save result in DX MAIN ENDP
HLT SUB1 PROC
MAIN ENDP MOV AX,5H
MOV CX,6H
SUMOF PROC
ADD AX,BX ;AX=AX + second number RET
ADD AX,CX ; AX=AX + third number SUB1 ENDP
RET ;return to caller
SUMOF ENDP
33
Procedures
Difference between JMP and CALL instruction
34
INTERRUPTS
An interrupt is an event that requests the CPU to suspend the program
execution briefly and run a subroutine called Interrupt Service Routine
(ISR).
After executing the ISR, the CPU resumes the program execution from
the point it left the program.
37
INTERRUPTS
38
INTERRUPTS
39
INTERRUPTS
Hardware Interrupts
Hardware interrupt is caused by any peripheral device by sending a signal
through a specified pin to the microprocessor.
The 8086 has two hardware interrupt pins, i.e. NMI and INTR.
NMI is a non-maskable interrupt and INTR is a maskable interrupt having lower
priority. One more interrupt pin associated is INTA called interrupt acknowledge.
NMI: Non-maskable interrupt ,used for major system faults such as parity errors
and power failures.
highest priority interrupts.
Non Maskable Interrupt input pin which means that any interrupt request at NMI
input cannot to masked or disabled by any means.
Can’t be disabled or ignored by the CPU.
Used for critical applications, e.g., in case of loss of power, the CPU can save
the contents of the registers in a nonvolatile memory when the voltage drops
below a threshold.
40
INTERRUPTS
Hardware Interrupts
INTR: Interrupt Request
The INTR is a maskable interrupt because the microprocessor will be interrupted
only if interrupts are enabled using set interrupt flag instruction. It can be masked
using the Interrupt Flag (IF).
This is a low priority interrupt
Activated by a peripheral device to interrupt the processor
The INTR interrupt is activated by an I/O port.
maskable interrupts: The program can ask the CPU to respond or ignore them,
i.e., can be enabled and disabled.
CPU does not execute the ISR if the interrupt is disabled .
Most of the interrupts are maskable
INTA: interrupt acknowledgement :activated by the processor to inform the
interrupting device that the interrupt request(INTR) is accepted.(IF checked)
41
INTERRUPTS
Software Interrupts: Software interrupts (exceptions) using the INT instruction
The software interrupts are program instructions. These instructions are inserted
The software interrupt instruction is INT n, where n is the type number in the
range 0 to 255.
42
INTERRUPTS
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
43 number. (exception: INTO and INT3 both are 1-byte instruction)
8086 INTERRUPTS types
256 interrupts of 8086 are divided in to 3 groups
1. TYPE 0 TO TYPE 4 INTERRUPTS: these are used for fixed
operations and hence are called Dedicated interrupts
2. TYPE 5 TO TYPE 31 INTERRUPTS: not used by 8086,reserved
for higher processors like 80286,80386 etc. Reserved interrupts
are also some interrupts which are not available for the users.
Then they are kept for higher processors.
3. TYPE 32 TO 255 INTERRUPTS: available for user, called user
defined interrupts.
Available interrupts : They range from INT 32- INT255 and are
software interrupts. Users can define these interrupts as well.
The INT n instructions can invoke these interrupts.
This interrupt also has the ISR location of nx4 in the interrupt
44
vector table.
INTERRUPTS
PREDEFINED INTERRUPTS(Dedicated interrupts ) (0 TO 4)
45
INTERRUPTS
Dedicated interrupts of 8086: The following are the various types of
interrupts: -
Type 0 interrupts: This interrupt is also known as the divide by zero
interrupt. For cases where the quotient becomes particularly large to be
placed / adjusted an error might occur. When the quotient from either a
DIV or IDIV instruction is too large to fit in the result register; 8086
Interrupt Types will automatically execute type 0 interrupt. Quotient is
large can’t fit in AL/AX or divide by zero.
Type 1 interrupts: Once TF (Trap Flags) is set to one, the 8086
automatically generates a TYPE 1 interrupt after execution of each
instruction. This is also known as the single step interrupt. This type of
interrupt is primarily used for debugging purposes in assembly language.
Type 2 interrupts: also known as the non-maskable interrupts (NMI ).
The non-maskable interrupt is initiated via the 8086 NMI pin. These type
of interrupts are used for emergency scenarios such as power failure. As
46
the name suggests, this interrupt cannot be disabled by any software instruction.
INTERRUPTS
Type 3 interrupts: These type of interrupts are also known as
breakpoint interrupts. When this interrupt occurs a program would
execute up to its break point.
When a break point interrupt inserted (it is inserted by INT 3
instruction), the system executes the instruction up to break point.
Unlike the single step feature which stops execution after each
instruction, the break point features executes all the instruction up
to the inserted breakpoint and then stop execution.
It is a 1-byte instruction.
Type 4 interrupts: Also known as overflow interrupts is generally
existent after an arithmetic operation was performed.
47
INTERRUPTS
For example, if you add the 8-bit signed number 0111 1000 (+ 120
decimal) and the 8 bit signed number 0110 1010 (+ 106 decimal),
result is 1110 0010 (- 98 decimal).
In signed numbers, MSB (Most significant Bit) is reserved for sign
and other bits represent magnitude of the number. In the previous
example, after addition of two 8-bit signed numbers result is
negative, since it is too large to fit in 7 bits.
To detect this condition in the program, you can put interrupt on
overflow instruction, INTO, immediately after the arithmetic
instruction in the program. If the overflow flag is not set when the
8086 executes the INTO instruction, the instruction will simply
function as an NOP (no operation).
However, if the overflow flag is set, indicating an overflow error, the
8086 will execute a type 4 interrupt after executing the INTO
48
instruction.
INTERRUPTS
Interrupt Vectors
In an Interrupt Structure of 8086 system the first 1 Kbyte of memory from
00000H to 003FFH is reserved for storing the starting addresses of Interrupt
Service Procedures(ISP).
This block of memory is often called the Interrupt Vector Table in 8086 or
the interrupt pointer table. Since 4 bytes are required to store the CS and IP
values for each interrupt service procedure, the table can hold the starting
addresses for 256 interrupt service routines.
Interrupt vector is the 4 byte long (CS:IP) address of interrupt service
procedure(ISP) 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.
Instruction: INT nn ; where nn indicates interrupt vector number.
49
INTERRUPTS
8086 Interrupt Vector Table
The starting address of an ISP is often called the Interrupt Vector or Interrupt
Pointer. There fore the table is referred as Interrupt Vector Table.
Fig. 1 shows how the 256 interrupt pointers are arranged in the memory table.
50
INTERRUPTS
Finding address of ISP
8086 interrupt vector table
For the interrupt type nn(Instruction INT nn), /pointer table
the table address for IP=4×nn and
the table address for CS=4×nn+2.
51
INTERRUPTS
Exercise: Find the physical address of interrupt service procedure( for the
following interrupt instructions:
(a)INT 01H
(b)INT FFH Interrupt vector table is given.
Interrupt vector table is given.
Solution (a)
Address for IP = 4 × 1 = 00004H
Address for CS = 4 × 1 + 2 = 00006H
So, IP= AC5EH and CS= C83AH
Physical address = CS × 10+IP = (C83AH × 10+AC5EH) =
D2FFEH
Solution (b)
Address for IP = 4 × FF = 003FCH
Address for CS = 4 × FF + 2 = 003FEH
So, IP= 5A99H and CS= 9800H
Physical address = CS × 10+IP
= (5A99H × 10+9800H)
= 64190H
52
INTERRUPTS
Interrupt Vectors
53
Interrupt Instructions
INT
IRET
INTO
INT
255.
Each INT instruction is 2 bytes long. The first byte contains the
opcode and the 2nd byte contains the vector type number.(only
the exception to this is INT 3. It is a one –byte special interrupt
instruction used for break points.)
It is common to insert an INT-3 instruction in a program to
60
END
62