Professional Documents
Culture Documents
MP - Lec 05 - Program Control Instructions
MP - Lec 05 - Program Control Instructions
LECTURE(05)
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
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