Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A program is a set of instructions. We know that the processor executes each instruction of the program. A processor executes instructions one by one. Whatever we write in our program is executed in the same order as it appears in the program.
Instructions are a set of commands stored in the computer's consecutive memory locations.
These instructions are fetched from successive memory locations for processing and execution. The change in the content of the program counter can cause a break in the instruction execution. However, the program control instructions control the flow of program execution and can branch to different program segments.
Most computer Instructions can be classified into three categories:
In this blog, we will have a look at Program Control Instructions.
What are Program Control Instructions?
Program control instructions modify or change the flow of a program. It is the instruction that alters the sequence of the program's execution, which means it changes the value of the program counter, due to which the execution of the program changes.
Features of Program Control Instructions
These instructions cause a change in the sequence of the execution of the instruction.
This change can be through a condition or sometimes unconditional.
Flags represent the conditions.
Flag-Control Instructions.
Control Flow and the Jump Instructions include jumps, calls, returns, interrupts, and machine control instructions.
Subroutine and Subroutine-Handling Instructions.
Loop and Loop-Handling Instructions.
Program Control Instructions
Description
Branch (BR)
Branch which means it is an unconditional jump. It is unconditional branching wherever we specify the address we need to branch.
Skip (SKP)
Skip instructions is used to skip one(next) instruction. It can be conditional or unconditional. It does not need an address field. In the case of conditional skip instruction, the combination of conditional skip and an unconditional branch can be used as a replacement for the conditional branch.
Jump (JMP)
The jump instruction transfers the program sequence to the memory address given in the operand based on the specified flag.
Compare (CMP)
The Compare instruction performs a comparison via a subtraction, with difference not retained. CMP compares register sized values, with one exception.
CALL and RETURN
The CALL and RETURN instructions interrupt the flow of a program by passing control to an internal or external subroutine. An external subroutine is another program. The RETURN instruction returns control from a subroutine back to the calling program and optionally returns a value.
TEST
TEST instructions perform the AND of two operands without retaining the result, and so on.
Now let us move further, these all are unconditional jumps or unconditional branching, which means there is no certain condition based on which they can jump or alter the execution sequence.
Types of Program Control Instructions
There are various types of program control instruction:
Status Bit Conditions
To check different conditions for branching instructions like CMP (compare) or TEST can be used. Certain status bit conditions are set as a result of these operations.
V
Z
S
C
Status bits mean that the value will be either 0 or 1 as it is a bit. We have four status bits:
"V" stands for Overflow
"Z" stands for Zero
"S" stands for the Sign bit
"C" stands for Carry.
Now, these will be set or reset based on the ALU (Arithmetic Logic Unit) operation carried out into the CPU. Let us discuss these bits before understanding the operation.
Overflow(V) is based on certain bits, i.e., if extra bits are generated into our operation. Then we have Zero (Z).
If the output of the ALU(Arithmetic Logic Unit) is 0, then the Z flag is set to 1, otherwise, it is set to 0.
If the number is positive, the Sign(S) flag is 0, and if the number is negative, the Sign flag is 1.
We have Carry(C), if the output of the thirst ALU operation generates Carry, then C is set to 1, else C is set to 0.
Let's see how these flags are affected. You can see in the figure this is an 8-bit ALU that performs arithmetic or logic operations on our data. Suppose we have two operands A and B of 8-bits on which we are performing certain arithmetic or logic operations. Arithmetic operation-addition is performed on A and B. We know that an extra Carry bit may be generated, which means eight Carry bits are generated. If the addition operation is performed on operands A and B, then the carry bits C0 to C7 may be generated. But we know that extra Carry may also be generated, which we term as C8. If C8 is generated, i.e., Carry is generated, reflecting our Carry flag, which results in the C flag or C status bit being set to 1.
Let's move further to the last two carries, C7 and C8. If the XOR of these two carries comes out to be 1, then we can say that the Overflow condition has happened and the V flag is set to 1; otherwise, it's set to 0. This is the most occurring case when we have negative numbers represented in 2's complement form.
Now moving on to the next flag which is the sign flag(S), the sign flag is set to 1 or 0 based on the output of the 8-bit ALU. As we know if the number is positive, then the most significant bit of a number is represented to be 0, which means if F7 is 0 then we can say that the number is positive and if F7 is 1 then we say that the number is negative.
Lastly, we have a Zero flag, Zero status bit Z, "Z" is said to be 1 if the output of all the bits from F0 to F7 is 0, then we can say that the zero flag is SET.
Now all these four bits that are "V", "Z", "S" and "C" are reflected based on the arithmetic or logic operation carried out on the 8-bit ALU.
Conditional Branch Instructions
A conditional branch instruction is basically used to examine the values that are stored in the condition code register to examine whether the specific condition exists and to branch if it does.
Conditional branch instructions such as ‘branch if zero’ or ‘branch if positive’ specify the condition to transfer the execution flow. The branch address will be loaded in the program counter when the condition is met.
Each conditional branch instruction tests for a different combination of Status bits for a condition.
Comparison Branch Instructions
Remember A≥B is the complement of A<B and A≤B is the complement of A<B, which means if we know the condition of status bits for one, the condition for the other complementary relation is obtained by complement.
Subroutines
Subroutines serve as essential components in programming, facilitating modular design and code reuse. When a subroutine is invoked, program control transfers to the subroutine's instructions executes them, and then returns to the point in the program from which it was called. This enables developers to break down complex tasks into smaller, manageable units and organize code more efficiently. In languages like Python, subroutines are defined using the def keyword, allowing functions to be called multiple times throughout the program.
Interrupt Instructions
interrupt instructions provide a mechanism for handling unexpected events or signals during program execution. When an interrupt occurs, the processor suspends the current program, saves its context, and transfers control to an interrupt handler routine. This allows the program to respond to external stimuli, such as user inputs or hardware interrupts, without disrupting its normal flow. In embedded systems programming, interrupts are commonly used to manage real-time events like sensor readings or communication signals.
Halting Instructions
Halting instructions offer a deliberate means of terminating program execution. When a halting instruction is encountered, the processor ceases operation, and the program concludes. This can be useful for ending program execution under specific conditions or implementing graceful shutdown procedures. In assembly language, halting instructions like HLT halt the processor, while in higher-level languages such as Python, the sys.exit() function can be employed to terminate the program. Together, these program control instructions enable developers to manage the flow and behavior of their programs effectively, handling both expected and unexpected scenarios with precision and control.
Frequently Asked Questions
What are the main elements of control?
Elements of a Good Control System are Feedback, Flexible controls, Prompt reporting of deviations, Hierarchical suitability, Economic control, and Strategic control points. The control must also be forward-looking, simple to understand and should focus on workers.
What are the four types of control instructions?
There are four types of control instructions: → Decision control instruction → Iterative control instruction → Switch-Case control instruction → Goto control instruction
What are the four basic elements of a control system?
The four essential elements of a typical motion control system are the amplifier, controller, feedback, and actuator. The complexity of these elements will change depending on the types of applications for which they are built and designed.
Conclusion
In this blog, we have discussed, Program control instructions along with its Status bit conditions, conditional and comparison branch instructors.