Chapter 5 program Control Instruction
Chapter 5 program Control Instruction
1
Program control instruction
Instructions of the computer are always stored in consecutive memory locations. These instructions
are fetched from successive memory locations for processing and executing.
Data transfer and manipulation instructions specify the conditions for data processing operations,
whereas the program control instructions specify the conditions that can alter the content of the
program counter.
The change in the content of the program counter can cause an interrupt/break in the instruction
execution. However, the program control instructions control the flow of program execution and
are capable of branching to different program segments
Controlling the program flow is a very important thing; this is where your program can make
decisions according to certain conditions.
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.
These instructions are used to jump on a particular location unconditionally, i.e. there is no need to satisfy any
condition for the jump to take place. There are three types of procedures used for unconditional jump. They are:
i. NEAR – This procedure targets within the same code segment. (Intra-segment)
ii. SHORT - This procedure also targets within the same code segment, but the offset is 1 byte long. (Intra-
segment)
iii. FAR - In this procedure, the target is outside the segment and the size of the pointer is double word. (Inter-
segment)
1
The basic syntax of JMP instruction:
2
JMP label
To declare a label in your program, just type its name and add ":" to the end, label can be any character
combination but it cannot start with a number, for example here are legal label definitions:
x1:
MOV AX, 1 OR x2: MOV AX, 2
here's an example of JMP instruction:
mov ax, 5 ; set ax to 5.
mov bx, 2 ; set bx to 2.
jmp calc ; go to 'calc'.
back: jmp stop ; go to 'stop'.
calc:
add ax, bx ; add bx to ax.
jmp back ; go 'back'.
stop:
ret ; return to operating system.
2) Conditional Jumps
In these types of instructions, the processor must check for the particular condition. If it is true, then only the jump
takes place else the normal flow in the execution of the statements is maintained.
The ALU operations set flags in the status word (Flag register). The conditional jump statements tests the flag and
jump is the flag is set.
2
MOV AX,1234H
MOV CX,4444H 3
STC ; sets carry flag to 1
JC P ; jump is CF is 1
MOV BX,0456H ; this will not executed
P:
push AX
hlt ; stops execution
The first instruction, loop (LOOP), works with respect to the contents of the CX register. CX must be preloaded
with a count that represents the number of times the loop is to repeat. Whenever LOOP is executed, the contents of
CX are first decremented by one and then checked to determine if they are equal to zero. If equal to zero, the loop
is complete and the instruction following LOOP is executed; otherwise, control is returned to the instruction at the
label specified in the loop instruction. In this way, we see that LOOP is a single instruction that functions the same
as a decrement CX instruction followed by a JNZ instruction.
For example, the LOOP instruction sequence shown in Fig.-1-(a) causes the part of the program from label NEXT
through the instruction LOOP to repeat a number of times equal to the value stored in CX.
3
4
For example, if CX contains 000AH, the sequence of instructions included in the loop executes 10 times.
The other two loop instructions in Fig.-a- operate in a similar way except that check for two conditions.
The instruction loop while equal (LOOPE) / loop while zero (LOOPZ) checks the contents of both CX and the
zero flag (ZF). Each time the loop instruction is executed, CX decrements by 1 without affecting the flags, its
contents are checked for 0, and the state of ZF that results from execution of the previous instruction is tested for
1. If CX is not equal to 0 and ZF equals 1, a jump is initiated to the location specified with the Short-label
operand and the loop continues. If either CX or ZF is 0, the loop is complete, and the instruction following the
loop instruction is executed.
Instruction loop while not equal (LOOPNE) / loop while not zero (LOOPNZ) works in a similar way to the
LOOPE/LOOPZ instruction. The difference is that it checks ZF and CX looking for ZF equal to 0 together with
CX not equal to 0. If these conditions are met, the jump back to the location specified with the Short-label
operand is performed and the loop continues.
Ex: Explain what happens as the following sequence of instructions is executed.
MOV DL , 05H
MOV AX, 0A00H
MOV DS, AX
MOV SI, 0H MOV
CX, 0FH
AGAIN: INC SI
CMP [SI] , DL
LOOPNE AGAIN
Solution:
The first five instructions are for initializing internal registers. Data register DL is loaded with 05H; data segment
register DS is loaded via AX with the value 0A00 H; source index register SI is loaded with 0000 H; and count
register CX is loaded with 0FH (15 Dec.).after initialization, a data segment is set up at physical address 0A000H,
and SI points to the memory location at offset 0000H in this data segment. DL contains the data 5H and the CX
register contains the loop count 15 Dec.
The part of the program that starts at the label AGAIN and ends with the LOOPNE instruction is a software loop.
The first instruction in the loop increments the value in SI by 1. Therefore, the first time through the loop SI points
4
to the memory address 0A001H. The next instruction compares the contents of this memory location with the
5
content of DL, 5Dec.. If the data held at 0A001H are 05H, the zero flag is set; otherwise, it is reset . the LOOPNE
instruction then decrements CX (making it 0EH) and then checks for CX = 0 or ZF = 1. If neither of these two
conditions is satisfied, program control is returned to the instruction with the label AGAIN. This causes the
comparison to be repeated for the examination of the contents of the next byte in memory. On the other hand, if
either condition is satisfied, the loop is complete. In this way, we see that the loop is repeated until either a number
5Dec. is found or all locations in the address range 0A001H through 0A00FH are tested and found not to contain 5Dec.
Procedures
Procedure is a part of code that can be called from your program in order to make some specific task. Procedures
make program more structural and easier to understand. Generally procedure returns to the same point from where
it was called.
The syntax for procedure declaration:
name PROC
; here goes the code of the procedure ...
RET
name ENDP
name - is the procedure name, the same name should be in the top and the bottom, this is used to check correct
closing of procedures.
Probably, you already know that RET instruction is used to return to operating system. The same instruction is
used to return from procedure (actually operating system sees your program as a special procedure).
PROC and ENDP are compiler directives, so they are not assembled into any real machine code. Compiler just
remembers the address of procedure.
CALL instruction is used to call a procedure.
Here is an example:
ORG 100h
CALL m1
MOV AX, 2
RET ; return to operating system.
m1 PROC
MOV BX, 5
RET ; return to caller.
m1 ENDP
END
The above example calls procedure m1, does MOV BX, 5, and returns to the next instruction
after CALL: MOV AX, 2.
5
There are several ways to pass parameters to procedure, the easiest way to pass parameters is by using registers,
6
here is another example of a procedure that receives two parameters in AL and BL registers, multiplies these
parameters and returns the result in AX register:
ORG 100h
MOV AL, 1
MOV BL, 2
CALL m2
CALL m2
CALL m2
CALL m2
RET ; return to operating system.
m2 PROC
MUL BL ; AX = AL * BL.
RET ; return to caller.
m2 ENDP
END
In the above example value of AL register is update every time the procedure is called, BL register stays
unchanged, so this algorithm calculates 2 in power of 4,
so final result in AX register is 16 (or 10h)
Here goes another example,
that uses a procedure to print a Hello World! message:
stop:
RET ; return to caller.
print_me ENDP
; ==========================================================
msg DB 'Hello World!', 0 ; null terminated string.
END
"b." - prefix before [SI] means that we need to compare bytes, not words. When you need to compare words add
"w." prefix instead. When one of the compared operands is a register it's not required because compiler knows
the size of each register.
6
Variables 7
Variable is a memory location. For a programmer it is much easier to have some value be kept in a variable.
name DB value
name DW value
name - can be any letter or digit combination, though it should start with a letter. It's possible to declare
unnamed variables by not specifying the name (this variable will have an address but no name).
value - can be any numeric value in any supported numbering system (hexadecimal, binary, or decimal), or
"?" symbol for variables that are not initialized.
Example
MOV AL, var1
MOV BX, var2
RET ; stops the program.
VAR1 DB 7
var2 DW 1234h
7
Machine Control Instructions in Microprocessor 8
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.These
instructions alter the different type of operations executed in the processor. In this topic, we will discuss the
various types of machine control instructions found in microprocessors and their significance in controlling the
microprocessor’s operations .