PROGRAMMING CONTROL
FLOW INSTRUCTIONS AND
PROGRAM STRUCTURES
8088/8086 MICROPROCESSOR
PROGRAMMING CONTROL FLOW
INSTRUCTIONS AND PROGRAM
STRUCTURES
6.1 Flag-Control Instructions
6.2 Compare Instructions
6.3 Control Flow and Jump Instructions
6.4 Subroutines and Subroutine-Handling
Instructions
6.5 The Loop and the Loop-Handling
Instructions
6.6 String and String-Handling Instructions
611 37100 Lecture 06-2
Meaning
Operation
Flags affected
LAHF
(AH)(Flags)
None
SAHF
(Flags)(AH)
SF,ZF,AF,PF,CF
CLC
(CF)0
CF
STC
(CF)1
CF
CMC
(CF)NOT (CF)
CF
CLI
(IF)0
IF
STI
(IF)1
IF
7
AH
SF
0
ZF
AF
PF
SF = Sign flag
ZF = Zero flag
AF = Auxiliary
PF = Parity flag
CF = Carry flag
- = Undefined (do not use)
611 37100 Lecture 06-4
CF
Solution:
LAHF
MOV [MEM1], AH
MOV AH, [MEM2]
SAHF
Solution:
CLC STC followed by a CMC
STC CLC followed by a CMC
Therefore, only CMC is the independent instruction.
Meaning
Compare
Format
CMP D, S
Operation
Flags affected
Destination
Source
Register
Register
Register
Memory
Memory
Register
Register
Immediate
Memory
Immediate
Accumulator
Immediate
Solution:
(AX) = 123416 = 00010010001101002
(BX) = ABCD16 = 10101011110011012
(AX) (BX) = 00010010001101002 - 10101011110011012
= 01100110011001112
Therefore, ZF = 0, SF = 0, OF = 0, PF = 0
CF = 1, AF = 1
and WHILE-DO
Applications using the loop and branch
software structures
AA
XXXXXX
Part III
Unconditional jump
instruction
Locations skipped due
to jump
Next instruction
executed
No
XXXXXX
Yes
Part II
AA
XXXXXX
Part III
Conditional jump
instruction
Next instruction executed
If condition not met
Locations skipped due
to jump
Next instruction
executed if
condition met
Meaning
JMP
Unconditional
jump
Format
Operation
Flags
affected
None
Operands
Short-label
Near-label
Far-label
Memptr16
Regptr16
Memptr32
Allowed operands for JMP instruction
611 37100 Lecture 06-17
Solution:
Solution:
Meaning
Conditional
jump
Format
Operation
Jcc Operand
If the specified
condition cc is true
the jump to the
address specified
by the operand is
initiated; otherwise
the next instruction
is executed
Flags
affected
None
10
Meaning
Condition
JA
Above
JAE
Above or equal
CF=0
JB
Below
CF=1
JBE
Below or equal
CF=1 or ZF=1
JC
Carry
CF=1
JCXZ
CX register is zero
(CF or ZF)=0
JE
Equal
ZF=1
JG
Greater
JGE
Greater or equal
SF=OF
JL
Less
JLE
Less or equal
JNA
Not above
CF=1 or ZF=1
JNAE
CF=1
JNB
Not below
CF=0
JNBE
Meaning
Condition
JNC
Not carry
CF=0
JNE
Not equal
ZF=0
JNG
Not greater
JNGE
JNL
Not less
SF=OF
JNLE
JNO
Not overflow
OF=0
JNP
Not parity
PF=0
JNS
Not sign
SF=0
JNZ
Not zero
ZF=0
JO
Overflow
OF=1
JP
Parity
PF=1
JPE
Parity even
PF=1
JPO
Parity odd
PF=0
JS
Sign
SF=1
JZ
Zero
ZF=1
11
12
CL, 03H
AL, CL
BIT2_ONE
--; Next instruction if B2 of AL=0
---
---
13
AGAIN:
YES
NO
Loop program
statements
Decrement
repeat count
Repeat
WHILE-DO program sequence
611 37100 Lecture 06-28
NEXT
14
AGAIN:
NEXT:
Initialization
Set up a counter for the points
to be removed
NXTPT
NO
Data movement
All points
Moved?
YES
Stop
Update
Test
15
NXTPT:
MOV
MOV
MOV
MOV
MOV
MOV
MOV
INC
INC
DEC
JNZ
HLT
AX, DATASEGADDR
DS, AX
SI, BLK1ADDR
DI, BLK2ADDR
CX, N
AH, [SI]
[DI], AH
SI
DI
CX
NXTPT
Solution:
CMP AX, BX
JC
DIFF2
DIFF1: MOV DX, AX
SUB DX, BX ; (DX)=(AX)-(BX)
JMP DONE
DIFF2: MOV DX, BX
SUB DX, AX ; (DX)=(BX)-(AX)
DONE: NOP
16
Subroutine A
Call subroutine A
First instruction
Next instruction
.
.
.
.
.
.
.
.
.
.
.
Call subroutine A
Next instruction
Return
.
.
.
Subroutine concept
17
Meaning
Format
Operation
CALL
Subroutine
call
CALL Operand
Flags
affected
None
Operands
Near-proc
Far-proc
Memptr16
Regptr16
Memptr32
Allowed operands for CALL instruction
611 37100 Lecture 06-35
Meaning
Return
Format
RET or
RET Operand
Operation
Return to the main
program by restoring
IP (and CS for farproc). If Operand is
present, it is added to
the contents of SP
Flags
affected
None
Operands
None
Disp16
18
,132
SEGMENT
DB
ENDS
STACK 'STACK'
64 DUP(?)
STACK_SEG
CODE_SEG SEGMENT
'CODE'
EX610
PROC
FAR
ASSUME CS:CODE_SEG, SS:STACK_SEG
;To return to DEBUG program put return address on the
stack
PUSH
DS
MOV
AX, 0
PUSH
AX
;Following code implements Example 6.10
CALL
SUM
RET
SUM
PROC
NEAR
MOV
DX, AX
ADD
DX, BX
; (DX)=(AX)+(BX)
RET
SUM
ENDP
EX610
ENDP
CODE_SEG ENDS
END
EX610
19
20
PUSH XX
PUSH YY
PUSH ZZ
.
.
.
.
.
.
.
POP ZZ
POP YY
POP XX
RET
Structure of a subroutine
611 37100 Lecture 06-41
Meaning
PUSH
PUSH S
((SP))(S)
(SP) (SP)-2
None
POP D
(D)((SP))
(SP) (SP)+2
None
POP
Format
Operation
Flags
affected
Operands (S or D)
Register
Seg-reg (CS illegal)
Memory
Allowed operands for PUSH and POP instruction
21
Solution:
;Subroutine: SQUARE
;Description: (BX)=square of (BL)
SQUARE PROC NEAR
PUSH AX
; Save the register to be used
MOV AX, BX ; Place the number in AL
IMUL BL
; Multiply with itself
MOV BX, AX ; Save the result
POP AX
; Restore the register used
RET
SQUARE ENDP
Meaning
PUSHF
((SP))(Flags)
(SP) (SP)-2
None
(Flags)((SP))
(SP) (SP)+2
OF,DF,IF,TF,SF,ZF,
AF,PF,CF
POPF
Operation
Flags affected
22
LOOPE
LOOPZ
LOOPNE
LOOPNZ
Format
Operation
Loop
Meaning
LOOP Short-label
(CX) (CX)-1
Jump is initiated to
location defined by shortlabel if (CX)0; otherwise,
execute next sequential
instruction
LOOPE/LOOPZ
Short-label
(CX) (CX)-1
Jump to location defined
by short-label if (CX)0
and (ZF)=1; otherwise,
execute next sequential
instruction
(CX) (CX)-1
Jump to location defined
by short-label if (CX)0
and (ZF)=0; otherwise,
execute next sequential
instruction
NEXT:
23
NXTPT:
AGAIN:
MOV DL, 05
MOV AX, 0A00H
MOV DS, AX
MOV SI, 0
MOV CX, 0FH
INC
SI
CMP [SI], DL
LOOPNE AGAIN
24
25
Meaning
Format
MOVS
Move string
MOVSB
MOVSW
((ES)0+(DI))((DS)0+(SI))
(SI) (SI)1 or 2
(DI) (DI)1 or 2
None
CMPS
Compare string
CMPSB
CMPSW
CF,PF,AF,
ZF,SF,OF
SCAS
Scan string
SCASB
SCASW
CF,PF,AF,
ZF,SF,OF
LODS
Load string
LODSB
LODSW
(AL or AX)-((DS)0+(SI))
(SI) (SI)1 or 2
None
STOS
Store string
STOSB
STOSW
None
Operation
Flags
affected
NXTPT:
26
AGAIN:
NEXT:
AGAIN:
MOV AX, 0
MOV DS, AX
MOV ES, AX
MOV AL, 05
MOV DI, 0A000H
MOV CX, 0FH
CLD
STOSB
LOOP AGAIN
27
Used with:
Meaning
REP
MOVS
STOS
REPE/REPZ
CMPS
SCAS
REPNE/REPNZ
CMPS
SCAS
28
Meaning
Format
Operation
Flags
affected
CLD
Clear DF
CLD
(DF)0
DF
STD
Set DF
STD
(DF)1
DF
29
30