CHAPTER 5:
PROGRAM CONTROL INSTRUCTIONS
Learning Objectives:
Upon completion of this chapter, you will be able to:
Differentiate between unconditional and conditional jump instructions.
Use loop and repeat constructs to implement iterative operations.
Understand the mechanism of calling and returning from procedures.
Explain the basic concept of interrupts and their control.
Identify the purpose of various machine control instructions.
5.1 The Jump Group
Program control instructions change the normal sequential flow of instructions in a program. Computers aren't
just calculators; they make decisions. Jump instructions are the assembly language equivalent of if/else
statements. They allow the program to choose different paths based on data values or conditions, making
programs dynamic and intelligent rather than just linear sequences.
Why it Matters: Without jumps, every program would run exactly the same way every time. Jumps enable
everything from simple data validation (if age < 0 then error) to complex AI algorithms.
5.1.1 Unconditional Jump (JMP)
The JMP instruction transfers control unconditionally to a target label or address. The processor immediately
begins fetching and executing instructions from the new location.
Syntax: JMP target_label
Operation: IP ← target_address (The Instruction Pointer is loaded with the address of the target label).
Example:
asm
MOV AX, 0005H
MOV BX, 0002H
JMP CALCULATION ; Unconditionally jump to the label CALCULATION
ADD AX, BX ; This instruction is SKIPPED!
CALCULATION:
MUL BX ; AX = AX * BX (Result: AX = 0005H * 0002H = 000AH)
In this example, the ADD AX, BX instruction is never executed because the JMP instruction diverts the flow
directly to CALCULATION.
5.1.2 Conditional Jumps and Conditional Sets
Conditional jumps transfer control only if a specific condition is true, which is determined by the status flags
in the Flags Register.
Syntax: Jcc target_label (where cc indicates the condition).
These instructions typically follow a CMP (Compare) or TEST instruction, which set the flags without altering
the operands.
CMP A, B performs A - B and sets the flags accordingly but does not store the result.
TEST A, B performs a logical AND between A and B and sets the flags (SF, ZF, PF) accordingly.
Common Conditional Jumps:
Instruction Condition Description Flags Checked
JE/JZ Jump if Equal / Zero Jumps if ZF = 1 ZF (Zero Flag)
JNE/JNZ Jump if Not Equal / Not Zero Jumps if ZF = 0 ZF (Zero Flag)
JG/JNLE Jump if Greater / Not Less or Equal Jumps for signed comparison if (SF=OF and ZF=0) SF, OF, ZF
JL/JNGE Jump if Less / Not Greater or Equal Jumps for signed comparison if (SF ≠ OF) SF, OF, ZF
JA/JNBE Jump if Above / Not Below or Equal Jumps for unsigned comparison if (CF=0 and ZF=0) CF, ZF
JB/JNAE Jump if Below / Not Above or Equal Jumps for unsigned comparison if (CF=1) CF (Carry Flag)
JC Jump if Carry Jumps if CF = 1 CF (Carry Flag)
JNC Jump if No Carry Jumps if CF = 0 CF (Carry Flag)
Example 1: Checking for Equality
asm
MOV AL, 10
MOV BL, 10
CMP AL, BL ; Compare AL and BL (AL - BL). Sets ZF=1 because they are equal.
JE EQUAL ; Because ZF=1, the jump is taken.
MOV CX, 0 ; This instruction is skipped.
EQUAL:
MOV CX, 1 ; CX will be set to 1.
Example 2: Finding the Larger Number (Signed)
asm
MOV AL, -5 ; -5 is a signed number (FBH in two's complement)
MOV BL, 2
CMP AL, BL ; Is AL > BL? (Is -5 > 2?)
JG AL_IS_GREATER ; This jump will NOT be taken because -5 is not greater than 2.
MOV DL, BL ; DL will hold the larger value (2).
JMP END
AL_IS_GREATER:
MOV DL, AL ; This block is skipped.
END:
Conditional Set Instructions (SETcc):
These instructions set a byte operand to 01H if the condition is true, or to 00H if false. They are useful for
storing the result of a condition in a flag.
Syntax: SETcc destination (8-bit register or memory location)
Example: SETZ AL sets AL to 1 if ZF=1, else sets AL to 0.
5.2 Controlling the Flow of the Program
These instructions provide a structured way to implement loops.
5.2.1 LOOP
The LOOP instruction is used to repeat a block of code a specific number of times. The count is stored in the
CX register.
Operation: It decrements CX by 1 and then checks if CX is not zero. If CX ≠ 0, it jumps to the target label.
Syntax: LOOP target_label
Example: Calculate 5! (5 factorial)
asm
MOV AX, 0001H ; Initialize result (AX) to 1
MOV CX, 0005H ; Set loop counter to 5
FACTORIAL:
MUL CX ; AX = AX * CX
LOOP FACTORIAL ; Decrement CX, jump if CX != 0
; After loop, AX contains 0120H (1*5*4*3*2 = 120 in decimal)
5.2.2 REPEAT
REPE (Repeat While Equal) and its synonym REPZ (Repeat While Zero) are prefixes used with string
instructions like CMPS (Compare String) or SCAS (Scan String).
Operation: The repeated instruction is executed as long as CX is not zero and the Zero Flag (ZF) is set (i.e.,
the comparison is equal).
Syntax: REPE CMPSB or REPZ SCASB
Example: Compare two strings until a mismatch is found.
asm
LEA SI, STRING1 ; Load effective address of STRING1 into SI
LEA DI, STRING2 ; Load effective address of STRING2 into DI
MOV CX, 000AH ; We want to compare up to 10 bytes
CLD ; Clear Direction Flag (DF=0) to increment SI and DI
REPE CMPSB ; Compare bytes at [SI] and [DI], increment pointers, repeat while equal and CX != 0
; After this, if ZF=1, the first 10 bytes of both strings are identical.
; If ZF=0, a mismatch was found before CX reached zero.
5.3 Procedures
Procedures (or subroutines) are blocks of code that perform a specific task and can be called from different
parts of a program. They promote code reusability and modularity.
5.3.1 CALL
The CALL instruction is used to transfer control to a procedure.
Operation:
1. It pushes the address of the next instruction (the return address) onto the stack.
2. It loads the IP (and CS, if it's a far call) with the address of the procedure.
Syntax: CALL procedure_name
5.3.2 RET
The RET (Return) instruction is used at the end of a procedure to return control to the calling program.
Operation: It pops the return address from the stack back into the IP (and CS, if it's a far return).
Syntax: RET or RET n (where n is an optional number of stack bytes to clean up after returning).
Example: A simple procedure to add two numbers.
asm
ORG 100H
MAIN PROC
MOV AX, 05H
MOV BX, 03H
CALL ADD_NUMBERS ; Call the procedure. Return address (next IP) is pushed.
MOV [0300H], AX ; Store the result. This is where RET returns to.
HLT
MAIN ENDP
ADD_NUMBERS PROC
ADD AX, BX ; Perform the addition (AX = AX + BX)
RET ; Pop the return address from stack into IP
ADD_NUMBERS ENDP
END MAIN
Stack Activity:
1. Before CALL: SP points to the top of the stack.
2. During CALL: The return address (e.g., address of MOV [0300H], AX) is pushed. SP decreases by 2.
3. During RET: The address is popped from the stack into IP. SP increases by 2.
5.4 Introduction to Interrupts
An interrupt is a signal that temporarily suspends the normal execution of a program to run a special routine
called an Interrupt Service Routine (ISR) or interrupt handler. After the ISR finishes, the original program
resumes.
Types:
Hardware Interrupts: Generated by external hardware devices (e.g., keyboard, timer).
Software Interrupts: Triggered by executing an INT instruction.
5.4.1 Interrupt Vectors
The system maintains an Interrupt Vector Table (IVT) at the lowest part of memory (address 0000:0000 to
0000:03FF). This table contains 256 4-byte segment:offset addresses (pointers) to the ISRs.
Interrupt Number: Each interrupt is assigned a number (0 to 255). The INT n instruction uses this number.
Calculating Vector Address: Vector Address = Interrupt_Number * 4
5.4.2 Interrupt Instructions
INT n: Generates a software interrupt n.
o Operation: Pushes Flags (like PUSHF), CS, and IP onto the stack, then transfers control to the ISR
whose address is stored at location n * 4 in the IVT.
o Example: INT 21H is the classic DOS system call interrupt.
IRET (Interrupt Return): Used at the end of an ISR. It pops IP, CS, and Flags from the stack, restoring
the state of the interrupted program.
5.4.3 Interrupt Control
CLI (Clear Interrupt Flag): Disables maskable hardware interrupts by setting IF = 0. Used in critical
sections.
STI (Set Interrupt Flag): Enables maskable hardware interrupts by setting IF = 1.
5.5 Machine Control and Miscellaneous Instructions
These instructions control the processor itself or manage its interaction with the system.
5.5.1 Flag Control Instructions
These instructions manipulate individual flags in the Flags register.
o STC (Set Carry Flag): Sets CF = 1.
o CLC (Clear Carry Flag): Sets CF = 0.
o CMC (Complement Carry Flag): Inverts CF (CF = NOT CF).
o STD (Set Direction Flag): Sets DF = 1, causing string operations to auto-decrement (process backwards).
o CLD (Clear Direction Flag): Sets DF = 0, causing string operations to auto-increment (process forwards).
o STI / CLI: As discussed in interrupt control.
5.5.2 WAIT
The WAIT (or FWAIT) instruction causes the CPU to enter an idle state until an external signal is received on
the TEST pin. It is used to synchronize the CPU with a coprocessor (like the 8087 Math Coprocessor),
ensuring the coprocessor has finished its task before the CPU proceeds.
5.5.3 HLT
The HLT (Halt) instruction stops the processor indefinitely until a valid interrupt (like a hardware reset or
maskable interrupt) occurs. It is often used to gracefully end a program and put the system in a known, stable
state.
Example: HLT is the last instruction in the example under section 5.3.
5.6 NOP (No Operation)
The NOP instruction does nothing. It takes up 1 byte of space and consumes 3 clock cycles.
Uses:
Timing Delays: To create very short, precise delays in time-critical code.
Code Alignment: To align instructions to memory address boundaries for performance.
Placeholder: To reserve space in code for future patches or modifications.
Removing Instructions: During debugging, an instruction can be replaced with a NOP to effectively
"remove" it without reorganizing the entire code block.
Example:
asm
MOV AL, 01H
NOP ; Processor does nothing here for a few cycles.
OUT 00H, AL ; Output value to port 00H
End of Chapter 5 Handout