Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 Flag-Control Instructions
Mnemonic
8088/8086 MICROPROCESSOR
PROGRAMMING CONTROL
FLOW INSTRUCTIONS AND
PROGRAM STRUCTURES
Meaning
Operation
(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
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
Flags affected
LAHF
CF
Solution:
LAHF
MOV [MEM1], AH
MOV AH, [MEM2]
SAHF
Meaning
Compare
Format
CMP D, S
Operation
Flags affected
Solution:
CLC STC followed by a CMC
STC CLC followed by a CMC
Therefore, only CMC is the independent instruction.
Destination
Source
Register
Register
Register
Memory
Memory
Register
Register
Immediate
Memory
Immediate
Accumulator
Immediate
EXAMPLE
EXAMPLE
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
Condition
met?
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
Mnemonic
Meaning
JMP
Unconditional
jump
Format
and WHILE-DO
Operation
Flags
affected
None
Operands
Short-label
Near-label
software structures
Far-label
Memptr16
Regptr16
Memptr32
AA
XXXXXX
Part III
Unconditional jump
instruction
Solution:
EXAMPLE
Mnemonic
Meaning
Condition
JNC
Not carry
CF=0
JNE
Not equal
ZF=0
Solution:
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
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
CMP AX, BX
JE
EQUAL
----; Next instruction if (AX)(BX)
.
.
EQUAL: ----; Next instruction if (AX)=(BX)
.
.
-----
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
MOV
SHR
JC
--.
.
BIT2_ONE: --.
.
---
CL, 03H
AL, CL
BIT2_ONE
--; Next instruction if B2 of AL=0
Initialize repeat
count
AGAIN
Loop done?
YES
NO
---
Loop program
statements
---
Decrement
repeat count
Repeat
WHILE-DO program sequence
611 37100 Lecture 06-28
NEXT
Start
Initialize repeat
count
AGAIN:
AGAIN
Loop program
statements
Decrement
repeat count
NO
Loop done?
(cc=false)
(cc=true)
YES
NEXT:
Continue
REPEAT-UNTIL program sequence
611 37100 Lecture 06-26
AGAIN:
Initialization
Set up a counter for the points
to be removed
NXTPT
NO
Data movement
All points
Moved?
YES
Stop
Update
Test
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
.
.
.
Subroutine A
Call subroutine A
First instruction
Next instruction
.
.
.
.
.
.
.
.
.
.
.
Call subroutine A
Next instruction
Return
.
.
.
Subroutine concept
EXAMPLE
Solution:
Mnemonic
Meaning
Format
Operation
CALL
Subroutine
call
CALL Operand
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
Flags
affected
None
Operands
Near-proc
Far-proc
Memptr16
Regptr16
Memptr32
Mnemonic
RET
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
EXAMPLE
EXAMPLE
,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
EXAMPLE
PUSH XX
PUSH YY
PUSH ZZ
.
.
.
.
.
.
.
POP ZZ
POP YY
POP XX
RET
Structure of a subroutine
611 37100 Lecture 06-41
EXAMPLE
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
EXAMPLE
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
NEXT:
Mnemonic
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
NXTPT:
EXAMPLE
Mnemonic
LOOP
LOOPE
LOOPZ
LOOPNE
LOOPNZ
Meaning
Format
Operation
Loop
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
AGAIN:
MOV DL, 05
MOV AX, 0A00H
MOV DS, AX
MOV SI, 0
MOV CX, 0FH
INC
SI
CMP [SI], DL
LOOPNE AGAIN
EXAMPLE
NXTPT:
EXAMPLE
AGAIN:
NEXT:
Format
Operation
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
Flags
affected
AGAIN:
MOV AX, 0
MOV DS, AX
MOV ES, AX
MOV AL, 05
MOV DI, 0A000H
MOV CX, 0FH
CLD
STOSB
LOOP AGAIN
EXAMPLE
Prefix
Used with:
Meaning
REP
MOVS
STOS
REPE/REPZ
CMPS
SCAS
REPNE/REPNZ
CMPS
SCAS
CLD
MOV AX, DATA_SEGMENT
MOV DS, AX
MOV AX, EXTRA_SEGMENT
MOV ES, AX
MOV CX, 20H
MOV SI, OFFSET MASTER
MOV DI, OFFSET COPY
REPZMOVSB
EXAMPLE
EXAMPLE
Mnemonic
Meaning
Format
Operation
Flags
affected
CLD
Clear DF
CLD
(DF)0
DF
STD
Set DF
STD
(DF)1
DF
10