You are on page 1of 11

Microprocessor Course

LECTURE(05)

Program Control Instructions

1
Chapter Contents
❑ Procedures
❑ Loop instruction
❑ Unconditional jump instruction
❑ Conditional jump instructions

2
Procedures
❑ In assembly language functions are called procedures or subroutines.
❑ The CALL instruction links to a procedure and the RET instruction returns from a
procedure.
❑ Procedures use a PROC directive (followed by the type of the procedure: near or
far) to start and the ENDP directive to end the construct.
❑ CALL pushes the return address onto the stack and RET retrieves the return
address from the stack. The stack allows procedures to be nested.
❑ The USES statement allows any number of registers to be automatically pushed
into the stack.
GETIT PROC NEAR USES EAX
MOV EAX,99H
RET
GETIT ENDP

3
Procedures (cont’d)
❑ Write an assembly near procedure named display that prints a character
passed to it through DL into the screen. Then, use this procedure to
print OK onto the screen.
❑ Note:
▪ You could use function 6 to print the letters from DL to the screen

4
Procedures (cont’d)
.MODEL tiny ; Single tiny code segment
.CODE ; Start of code segment
.STARTUP ; Start of program
MOV DL, ‘O’ ; Copy letter ‘O’ to the DL
CALL display ; Call the procedure
MOV DL, ‘K’ ; Copy letter ‘K’ to the DL
CALL display ; Call the procedure
.EXIT ; Exit to DOS

display PROC NEAR ; Procedure to display the ASCI character in DL


MOV AH, 6 ; Select DOS function 6
INT 21h ; DOS function call
RET ; Return to the calling program
display ENDP ; End of procedure
END ; End of program
5
Loop Instruction
❑ Similar to the high level for loop in which the number of iteration is known apriori
❑ Before the beginning of the loop, the number of iterations should be stored in CX

MOV SI, OFFSET ARR1 ; Copy 1st address in the source to SI


MOV DI, OFFSET ARR2 ; Copy 1st address in the destination to DI
MOV CX, 5 ; Load counter register with 5 (5 iterations)
Again: MOV AL, [SI] ; Copy element from the source, addressed by SI to AL
MOV [DI], AL ; Copy AL to the destination element, addressed by DI
INC SI ; Increment SI (to next element)
INC DI ; Increment DI (to next element)
LOOP Again ; Repeat 5 times

6
Unconditional jump (JMP)
❑ The JMP instruction is a GOTO that is found in all computer languages.
JMP passes program flow to another part of the program.
❑ The JMP instruction comes in 3 forms, short, near, and far.
❑ Short and near are intra-segment jumps, whereas far is an intersegment
jump
❑ Usually, labels are used as jump targets. These labels should be
followed by colons

7
Unconditional jump (Cont’d)
❑ JMP has direct and indirect addressing modes
❑ Direct jumps use labels
JMP Again
❑ Indirect jumps take two forms. One jumps to the offset location
addressed by a register and the other jumps to the location addressed
by a memory pointer.
JMP CX
JMP EDX
JMP NEAR PTR [BX] ;16 bit
JMP FAR PTR [ECX] ;32 bit

8
Conditional Jumps
❑ Conditional jump instructions are always short jumps.
❑ Conditional jump instructions, for the most part, test the flag bits.
❑ Some check a single flag bit, while some check multiple flag bits.
❑ One set of conditional jumps test unsigned numbers (JZ, JNZ, JA, JB,
JAE, or JBE).
❑ One set of conditional jumps test signed number (JZ, JNZ, JG, JL, JGE,
and JLE).
❑ Conditional jumps usually follow a CMP or TEST instructions

9
Jump commands
❑ JZ: Jump if the result is zero
❑ JNZ: Jump if the result is not zero
❑ JC: Jump if there is a carry
❑ JS: Jump if result is negative
❑ JO: jump if there is Overflow
❑ JA: Jump if above (CF = 0 and ZF = 0)
❑ JAE: Jump if above or equal (CF = 0) Unsigned Numbers (CF)
❑ JB: Jump if below (CF = 1)
❑ JBE: Jump if below or equal (CF = 1 or ZF = 1)
❑ JG: Jump if greater than (ZF = 0 and SF = OF)
❑ JGE: Jump if greater than or equal (SF = OF) Signed Numbers (SF)
❑ JL: Jump if less than (SF ≠ OF)
❑ JLE: Jump if less than or equal (ZF = 0 and SF ≠ OF)

10
Thank you
for
your attention
11

You might also like