0% found this document useful (0 votes)
25 views

LECTURE_5 Program Control Instructions

Uploaded by

Amanuel Tamirat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

LECTURE_5 Program Control Instructions

Uploaded by

Amanuel Tamirat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

WACHEMO UNIVERSITY

DURAME CAMPUS

COLLEGE OF ENGINEERING AND TECHNOLOGY


SCHOOL OF COMPUTING AND INFORMATICS
DEPARTMENT OF COMPUTER SCIENCE

MICROPROCESSOR AND ASSEMBLY LANGUAGE PROGRAMMING

Course Code: CoSc 3025

Selamu S. (MSC)
LECTURE_5

PROGRAM CONTROL INSTRUCTION


PROGRAM CONTROL INSTRUCTIONS
 Program Control Instructions are the machine code that are used by machine or in
assembly language by user to command the processor act accordingly.
 These instructions are of various types.These are used in assembly language by user also.
 The program control instructions direct the flow of a program and allow the flow to
change.
 A change in flow often occurs after a decision made with the CMP or TEST instruction is
followed by a conditional jump instruction.
 The control transfer instructions transfer the flow of execution of the program to a new
address specified in the instruction directly or indirectly.
 Program Control Instructions can be classified into two categories as follows:-
1) Unconditional Transfer Instructions
 There are instructions under this category is
 CALL RET JMP
2) Conditional Transfer instructions
 There are instructions under this category is JMP Cond
 JA / JNBE  JNS  JCXZ
 JL / JNGE
 JAE / JB/ JNC  JLE / JNG  JP / JPE
 JB / JC / JNAE  JNE / JNZ  JO
 JGE / JNL  JS  JNO
THE JUMP GROUP
 Jump Instructions are used for changing the flow of execution of instructions in the
processor.
 If we want jump to any instruction in between the code, then this can be achieved by these
instructions.
 There are two types of Jump instructions:-
1) Unconditional Jump Instructions
2) Conditional Jump Instructions
UNCONDITIONAL JUMP (JMP)
 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 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.
CONDITIONAL JUMPS & CONDITIONAL SETS
A Conditional jump instruction allows the programmer to make decisions based upon
numerical tests.
The results of numerical tests are held in the flag bits, which are then tested by conditional
jump instructions.
Conditional jump instructions are always short jumps in the 8086 microprocessors.
This limits the range of the jump to within +127 bytes and -128 bytes from the location
following the conditional jump.
This allows these microprocessors to use a conditional jump to any location within the
current code segment.
The conditional jump instructions test the following flag bits: sign (S), zero (Z), carry (C),
parity (P), and overflow (0).
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.
CONDITIONAL SET INSTRUCTIONS.
 In addition to the conditional jump instructions, the 8086 through the processors also
contain conditional set instructions.
 The conditions tested by conditional jumps are put to work with the conditional set
instructions.
 The conditional set instructions set a byte to either 01H or clear a byte to 00H, depending
on the outcome of the condition under test.
CONTROLLING THE FLOW OF THE PROGRAM
 Controlling the program flow is a very important thing, this is where your program can
make decisions according to certain conditions.
 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.
 These statements always indicate a special assembly language command to (Microsoft
Macro Assembler) MASM.
 Note that the control flow assembly language statements beginning with a period are only
available to MASM version 6.xx, and not to earlier versions of the assembler.
LOOP
 The LOOP instruction is a combination of a decrement CX and the JNZ conditional jump.
 LOOP (JUMP TO SPECIFIED LABEL IF CX * 0 AFTER AUTO DECREMENT)
 This instruction is used to repeat a series of instructions some number of times.
 The number of times the instruction sequence is to be repeated is loaded into CX.
 Each time the LOOP instruction executes, CX is automatically decremented by 1.
 If CX is not 0, execution will jump to a destination specified by a label in the instruction.
 If CX = 0 after the auto decrement, execution will simply go on to the next instruction
after LOOP.
 The destination address for the jump must be in the range of -128 bytes to +127 bytes
from the address of the instruction after the LOOP instruction.
 The LOOP instruction executes the group of instructions a number of times and it uses
relative addressing mode.
 The number of iterations will depend on the condition to be satisfied.
 The CX register will perform the LOOP operation on the instructions to be iterated.
 For every execution of LOOP instruction, the CX is automatically decremented by one
without affecting flags and performs a short jump to the target address. This loop will
continue until the CX becomes zero. When CX = 0, the execution of the loop will stop
and the instructions after the LOOP will start execution.
CONDITIONAL LOOPS
LOOP instruction also has conditional forms: LOOPE and LOOPNE
LOOPE (Loop while Equal) instruction jumps if CX != 0 while an equal condition
exists.
 Will exit loop if the condition is not equal or the CX register decrements to 0
LOOPNE (Loop while Not Equal) jumps if CX != 0 while a not-equal condition exists.
 Will exit loop if the condition is equal or the CX register decrements to 0
These instructions are used to execute the given instructions for number of times.
Following is the list of instructions under this group:-
1) LOOP:- Used to loop a group of instructions until the condition satisfies, i.e., CX = 0
2) LOOPE/LOOPZ:- Used to loop a group of instructions till it satisfies CX = 0 & ZF = 0
3) LOOPNE/LOOPNZ:- Used to loop a group of instructions till it satisfies CX = 0 & ZF =
0
LOOPE/ LOOPZ Instruction :
This instruction is similar to LOOP except that it checks for both CX and ZF conditions to be satisfied
before the jump can take place.
Firstly the count register CX is loaded with a value of the number of times the instructions are to be
repeated.
For every iteration or every time the LOOPE/LOOPZ instruction executes, it decrements the CX register
by one without affecting flags and performs a short jump to the target address (i.e., to the address with
signed 8-bit relative displacement in the range of -128 bytes to +127 bytes from the instruction address
after LOOPE/LOOPZ instruction) until the condition CX ≠ 0 and ZF = 1 is maintained.
If CX equals 0, or if the zero flag gets cleared within the loop, the loop will terminate.
In other words, the two ways to exit the loop are CX = 0 or ZF = 0, then after the processor starts
executing the next instruction present after LOOPE/LOOPZ.
LOOPNE/ LOOPNZ Instruction :
These instructions are the opposite of LOOPE/LOOPZ. It decrements the CX register by one without
affecting flags and performs a short jump to the target address if CX ≠ 0 and ZF = 0.
In other words, the number of iterations is loaded into the count register CX and after every time the
instruction LOOPE/LOOPZ executes the CX will be auto decremented by one.
Until the conditions, CX ≠ 0 and ZF = 0 satisfies, the execution performs a short jump to the target
address given in the instruction.
When either of the condition CX ≠ 0 or ZF = 0 fails to satisfies i.e., zero flag (ZF) is set or CX becomes
zero after auto decrement, the execution of the loop will exit, and the instruction after LOOPE/LOOPZ
in the program will be executed.
A group of instructions can be repeated a number of times (i.e., until CX = 0 and ZF ≠ 0) by using
LOOPE/LOOPZ.
REPEAT
REPEAT-UNTIL Loops
 A series of instructions is repeated until some condition occurs.
 The .REPEAT statement defines the start of the loop.
 end is defined with the .UNTIL statement, which contains a condition
 An .UNTILCXZ instruction uses the LOOP instruction to check CX for a repeat loop.
 .UNTILCXZ uses the CX register as a counter to repeat a loop a fixed number of times
PROCEDURES
 A procedure is a set of code that can be branched to and returned from in such a way that
the code is as if it were inserted at the point from which it is branched to.
 The branch to procedure is referred to as the call, and the corresponding branch back is
known as the return.
 The return is always made to the instruction immediately following the call regardless of
where the call is located.
 A procedure is a group of instructions that usually performs one task.
Subroutine, method, or function is an important part of any system’s architecture
 A procedure is a reusable section of the software stored in memory once, used as often as
necessary.
Saves memory space and makes it easier to develop software
 Disadvantage of procedure is time it takes the computer to link to, and return from
it.
 CALL links to the procedure; the RET (return) instruction returns from the procedure
 RET instruction removes an address from the stack so the program returns to the
instruction following the CALL.
CALL INSTRUCTION
 It is used to transfer execution to a subprogram or procedure.
Transfers the flow of the program to the procedure.
 CALL instruction differs from the jump instruction because a CALL saves a return address
on the stack.
 The return address returns control to the instruction that immediately follows the CALL
in a program when a RET instruction executes.
 CALL pushes the address of the instruction following the CALL (return address) on the
stack.
 The stack stores the return address when a procedure is called during a program
There are two basic types 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 two and copies the offset of the next instruction after the CALL on
the stack.
 It loads IP with the offset of the first instruction of the procedure in the same
segment.
 A far CALL is a call to a procedure which is in different segment from that which contains
the CALL instruction.
 When the 8086 executes a far CALL it decrements the stack pointer by two and copies the
contents of the CS register to the stack.
 It then decrements the stack pointer by two again and copies the offset of the instruction
after the CALL to the stack.
 Finally, it loads CS with the segment base of the segment which contains the procedure
and IP with the offset of the first instruction of the procedure in that segment.
The CALL instruction is used whenever we need to make a call to some procedure or a
subprogram.
Whenever a CALL is made, the following process takes place inside the microprocessor:-
1) The address of the next instruction that exists in the caller program (after the program CALL
instruction) is stored in the stack.
2) The instruction queue is emptied for accommodating the instructions of the procedure.
3) Then, the contents of the instruction pointer (IP) is changed with the address of the first
instruction of the procedure.
4) The subsequent instructions of the procedure are stored in the instruction queue for execution.
RET INSTRUCTION
 The RET instruction will return execution from a procedure to the next instruction after the
CALL instruction in the calling program.
 The RET instruction removes an address from the stack so the program returns to the instruction
following the CALL.
 It returns the control from procedure to calling program.
 Every CALL instruction should have a RET.
 The address of the instruction following the CALL (return address) on the stack.
 The return address returns control to the instruction that immediately follows the CALL in a
program when a RET instruction.
 The RET instruction stands for return. This instruction is used at the end of the procedures or
the subprograms.This instruction transfers the execution to the caller program.
Whenever the RET instruction is called, the following process takes place inside the
microprocessor:-
1) The address of the next instruction in the mainline program which was previously stored inside
the stack is now again fetched and is placed inside the instruction pointer (IP).
2) The instruction queue will now again be filled with the subsequent instructions of the mainline
program.
INTRODUCTION TO INTERRUPTS
 Interrupt is the method of creating a temporary halt during program execution and allows
peripheral devices to access the microprocessor.
 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 by some other internal event). At times, an internal interrupt is called an
exception.
 Either type interrupts the program by calling an interrupt service procedure (ISP) or
interrupt handler.
 An interrupt is a condition that halts the microprocessor temporarily to work on a different
task and then returns to its previous task.
INTERRUPT VECTORS
 An interrupt vector table (IVT) is a data structure that associates a list of interrupt
handlers with a list of interrupt requests in a table of interrupt vectors.
 Each entry of the interrupt vector table, called an interrupt vector, is the address of an interrupt
handler.
INTERRUPT INSTRUCTIONS
 These instructions are used to call the interrupt during program execution.
1) INT:- used to interrupt the program during execution and calling service specified.
2) INTO:- used to interrupt the program during execution if OF = 1
3) IRET:- used to return from interrupt service to the main program
INTERRUPT CONTROL
 Two instructions control the INTR pin.
 The set interrupt flag instruction (STI) places 1 in the I flag bit.
 which enables the INTR pin.
 The clear interrupt flag instruction (CLI) places a 0 into the I flag bit.
 which disables the INTR pin.
 In software interrupt service procedure, hardware interrupts are enabled as one of the first steps.
 accomplished by the STI instruction
 Interrupts are enabled early because just about all of the I/O devices in the personal computer
are interrupt-processed.
 if interrupts are disabled too long, severe system problems result.
MACHINE CONTROL AND MISCELLANEOUS INSTRUCTIONS

 Microprocessors are electronic devices that process digital information using instructions
stored in memory.
 Machine control instructions are a type of instruction that control machine functions such
as Halt, Interrupt, or do nothing.
 These instructions alter the different type of operations executed in the processor.
 Machine control instructions have specific features that affect the microprocessor’s
operations.
 Machine control instructions can have limitations that affect the performance and
reliability of microprocessors.
Flag Control Instructions
 The 8086 microprocessor has a set of flags that either monitors the state of executing
instructions or controls options available in its operation.
 The instruction set includes a group of instructions that. when executed, directly affect the
state of the flags.
HALT INSTRUCTION
 The HLT instruction causes the 8086 to stop fetching and executing instructions.
 The 8086 will enter a halt state.
 The different ways to get the processor out of the halt state are with an interrupt signal on
the INTR pin, an interrupt signal on the NMI pin, or a reset signal on the RESET input.
 The HLT instruction halts any further execution of instructions by the microprocessor.
 The HLT instruction can cause the microprocessor to enter a wait state indefinitely, which
can impact the system’s responsiveness.
WAIT (WAIT FOR SIGNAL OR INTERRUPT SIGNAL)
When this instruction is executed, the 8086 enters an idle condition in which it is doing no
processing.
 The 8086 will stay in this idle state until the 8086 TEST input pin is made low or until an
interrupt signal is received on the INTR or the NMI interrupt input pins.
 If a valid interrupt occurs while the 8086 is in this idle state, the 8086 will return to the idle state
after the interrupt service procedure executes.
WAIT does not affect any flag.
 The WAIT instruction is used to synchronize the 8086 with external hardware such as the 8087
Math coprocessor.
NOP (PERFORM NO OPERATION)
 At the time of execution of NOP instruction, no operation is performed except fetch and decode.
 It takes three clock cycles to execute the instruction.
 This instruction simply uses up three clock cycles and increments the instruction pointer to point
to the next instruction
 NOP does not affect any flag.
 This instruction is used to fill in time delays or delete and insert instructions in the programs
while trouble shooting.
 The NOP instruction does not affect the microprocessor’s state.

You might also like