4 Branch Instructions
4 Branch Instructions
ORGANIZATION(PCO303C)
UNIT III
Register & Control Unit Organization
Topic: Program Control
Contents
• Introduction to Program Control
• Basic Control Structures in High-Level Languages
• Machine-Level Representation of Control
• Unconditional Branching
• Conditional Branching
• Subroutines and Function Calls
1. Program Control
Program Control is the mechanism that determines the sequence in which
instructions in a program are executed.
• At its core, programming is not just about performing computations but also
about controlling the order of these computations. This is where program control
comes into play.
If the PC starts at address 1000, it will increment to 1001 after the ADD
operation, then to 1002 after the SUB operation, ensuring sequential
execution of instructions.
Branching & Jumping:
• Sometimes, sequential execution is not desired, such as in loops or
conditional statements.
• In such cases, the PC's value can be changed arbitrarily, causing it to
"jump" to a different address.
Memory Address Value Comment
1000 CMP ; Compare two numbers
1001 JEQ 1004 ; Jump to address 1004 if numbers are equal
1002 ADD ; Add two numbers (executed if numbers are not equal)
1003 STOP ; End of program
1004 MUL ; Multiply two numbers (executed if numbers are equal)
Pseudo-Assembly Representation:
• In the above pseudo-assembly, the JMP JumpDestination instruction means the flow of execution
will skip the next PRINT instruction and jump straight to the label JumpDestination.
Example:PC Initial State
• PC = Address of the first instruction
PRINT "Before the jump" <-- PC is here
JMP JumpDestination
PRINT "This won't be printed"
JumpDestination:
PRINT "After the jump“
• So, the PC started at the top, incremented by one to move to the JMP instruction, and then due
to the JMP, it jumped to the address labeled JumpDestination, skipping over the next PRINT.
4. Unconditional Branching
• Its primary purpose is to divert the normal flow of execution to a different part of the program without checking any
condition.
Unconditional Branching with Examples:
1. Basic Jump in Assembly:
• In assembly, the straightforward unconditional jump is often represented by the JMP instruction.
JMP loopStart
int main() {
printf("Start\n");
goto end;
printf("Middle\n"); // This line will be skipped
end:
printf("End\n");
return 0;
}
• When executed, this program will print:
Start
End
3. Jump Tables in Assembly:
• Unconditional jumps are often used in jump tables, which are efficient ways to implement switch-case
structures in low-level code.
MOV AX, [userChoice] ; Let's assume userChoice is 2
JMP [jumpTable + AX*4]
jumpTable:
DW option1
DW option2
DW option3
option1:
; ... Code for option 1 ...
RET
option2:
; ... Code for option 2 ...
RET
option3:
; ... Code for option 3 ...
RET
• In this scenario, based on the value of userChoice, the program jumps to the appropriate label.
4. Looping with Unconditional Branching:
MOV CX, 5
loopStart:
; ... loop body ...
DEC CX
; At this point, CX is decremented.
TEST CX, CX ; This sets the ZF (zero flag) if CX is 0, and clears it otherwise.
JZ endOfLoop ; Jump to endOfLoop if CX is zero (i.e., if ZF is set).
JMP loopStart ; This is the unconditional jump. If we reach this point, we will always jump back to loopStart.
endOfLoop:
; Rest of the program or function.
5. Early Function Exit in High-Level Languages:
• In embedded systems programming, it's common to have a reset routine that is jumped to
unconditionally upon certain error conditions or watchdog timer events.
; Some operation
CMP AX, BX
JE resetRoutine
resetRoutine:
; ... Reset the system ...
The code compares AX and BX.
• If they are equal, the program jumps to resetRoutine.
• If they are not equal, the program continues with the next operations.
• The resetRoutine is a set of operations to reset or recover the system, executed only if AX
equals BX.
5. Conditional Branching
• Making a decision in code to execute a certain block of statements
based on a condition.
• Comparison with Unconditional Branching: Unlike unconditional
jumps, which always divert the flow of execution to a specific point,
conditional jumps depend on a certain condition being true or false.
I. Basic Comparison Instructions:
CMP (Compare):
• The CMP instruction is used to compare two operands.
• It works by performing a subtraction between the operands but
doesn't save the result; instead, it modifies the flags based on the
result of the subtraction.
Flags Affected:
• Zero Flag (ZF): Set if the result is zero.
• Sign Flag (SF): Set if the result is negative. Using these flags, one could take
• Overflow Flag (OF): Set if there was an overflow. decisions like:
• Carry Flag (CF): Set if there was a carry (for
unsigned numbers).
Example: • JG AX_is_greater ; Jumps to the
MOV AX, 10 label if AX > BX
MOV BX, 5
CMP AX, BX ; Here AX - BX is 5, but the result isn't
stored.
; Only flags are set based on the outcome.