You are on page 1of 30

8088/8086 MICROPROCESSOR

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 林達德

1
6.1 Flag-Control Instructions

„ The flag-control instructions, when executed,


directly affect the state of the flags. These
instructions include:
™LAHF (Load AH from flags)
™SAHF (Store AH into flags)
™CLC (Clear carry)
™STC (Set carry)
™CMC (Complement carry)
™CLI (Clear interrupt)
™STI (Set interrupt)
國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-3 林達德

6.1 Flag-Control Instructions


Mnemonic Meaning Operation Flags affected
LAHF Load AH from flags (AH)←(Flags) None
SAHF Store AH into flags (Flags)←(AH) SF,ZF,AF,PF,CF
CLC Clear carry flag (CF)←0 CF
STC Set carry flag (CF)←1 CF
CMC Complement carry flag (CF)←NOT (CF) CF
CLI Clear interrupt flag (IF)←0 IF
STI Set interrupt flag (IF)←1 IF

7 0

AH SF ZF - AF - PF - CF

SF = Sign flag
ZF = Zero flag
AF = Auxiliary
PF = Parity flag
CF = Carry flag
- = Undefined (do not use) 國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-4 林達德

2
6.1 Flag-Control Instructions
EXAMPLE
Write an instruction sequence to save the current contents of
the 8088’s flags in the memory location at offset MEM1 of the
current data segment and then reload the flags with the contents of
the storage location at offset MEM2.
Solution:
LAHF ; Load AH from flags
MOV [MEM1], AH ; Move content of AH to MEM1
MOV AH, [MEM2] ; Load AH from MEM2
SAHF ; Store content of AH into flags

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-5 林達德

6.1 Flag-Control Instructions

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-6 林達德

3
6.1 Flag-Control Instructions

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-7 林達德

6.1 Flag-Control Instructions


EXAMPLE
Of the three carry flag instructions CLC, STC, and CMC, only
one is really independent instruction. That is, the operation that it
provides cannot be performed by a series of the other two
instructions. Determine which one of the carry instruction is the
independent instruction.
Solution:
CLC ⇔ STC followed by a CMC
STC ⇔ CLC followed by a CMC
Therefore, only CMC is the independent instruction.

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-8 林達德

4
6.1 Flag-Control Instructions
EXAMPLE
Verify the operation of the following instructions that affect the
carry flag,
CLC
STC
CMC
by executing them with the DEBUG program. Start with CF flag set
to 1 (CY).

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-9 林達德

6.1 Flag-Control Instructions

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-10 林達德

5
6.2 Compare Instruction
„The Compare Instruction:
The compare operation enable us to determine
the relationship between two numbers.
Mnemonic Meaning Format Operation Flags affected
CMP Compare CMP D, S (D)-(S) is used in setting CF, AF, OF, PF, SF,
or resetting the flags ZF

Destination Source
Register Register
Register Memory
Memory Register
Register Immediate
Memory Immediate
Accumulator Immediate

Allowed operands for compare instruction


國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-11 林達德

6.2 Compare Instruction


EXAMPLE
Describe what happens to the status flags as the sequence of
instructions that follows is executed.
MOV AX, 1234H
MOV BX, 0ABCDH
CMP AX, BX
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

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-12 林達德

6
6.2 Compare Instruction
„The Compare Instruction:

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-13 林達德

6.3 Control Flow and Jump


Instructions
„ Unconditional jump instruction
„ Conditional jump instruction
„ Branching structure – IF-THEN
„ Loop program structure – REPEAT-UNTIL
and WHILE-DO
„ Applications using the loop and branch
software structures

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-14 林達德

7
6.3 Control Flow and Jump
Instructions
„ Unconditional and conditional jump

Part I
Unconditional jump
JMP AA instruction

Part II Locations skipped due


to jump

AA XXXXXX Next instruction


executed
Part III

Unconditional jump program sequence

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-15 林達德

6.3 Control Flow and Jump


Instructions
„ Unconditional and conditional jump

Part I
Conditional jump
Jcc AA instruction
Condition No Next instruction executed
met? XXXXXX If condition not met
Yes Part II Locations skipped due
to jump
AA XXXXXX Next instruction
executed if
Part III condition met

Conditional jump program sequence

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-16 林達德

8
6.3 Control Flow and Jump
Instructions
„ Unconditional jump instruction
Mnemonic Meaning Format Operation Flags
affected
JMP Unconditional JMP Operand Jump is initiated None
jump to the address
specified by the
operand

Operands
Short-label
Near-label
Far-label
Memptr16
Regptr16
Memptr32
Allowed operands for JMP instruction
國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-17 林達德

6.3 Control Flow and Jump


Instructions
EXAMPLE
Verify the operation of the instruction JMP BX using the
DEBUG program. Let the contents of BX be 001016.
Solution:

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-18 林達德

9
6.3 Control Flow and Jump
Instructions
EXAMPLE
Use the DEBUG program to observe the operation of the
instruction JMP [BX].
Solution:

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-19 林達德

6.3 Control Flow and Jump


Instructions
„ Conditional jump instruction

Mnemonic Meaning Format Operation Flags


affected
Jcc Conditional Jcc Operand If the specified None
jump condition cc is true
the jump to the
address specified
by the operand is
initiated; otherwise
the next instruction
is executed

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-20 林達德

10
6.3 Control Flow and Jump
Instructions
„ Conditional jump instruction
Mnemonic Meaning Condition
JA Above CF=0 and ZF=0
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 ZF=0 and SF=OF
JGE Greater or equal SF=OF
JL Less (SF xor OF)=1
JLE Less or equal ((SF xor OF) or ZF)=1
JNA Not above CF=1 or ZF=1
JNAE Not above nor equal CF=1
JNB Not below CF=0
JNBE Not below nor equal CF=0 and ZF=0
Type of conditional jump instructions
國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-21 林達德

6.3 Control Flow and Jump


Instructions
„ Conditional jump instruction
Mnemonic Meaning Condition
JNC Not carry CF=0
JNE Not equal ZF=0
JNG Not greater ((SF xor OF) or ZF)=1
JNGE Not greater nor equal (SF xor OF)=1
JNL Not less SF=OF
JNLE Not less or nor equal ZF=0 and SF=OF
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
國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-22 林達德

11
6.3 Control Flow and Jump
Instructions
„ Branch program structure – IF-THEN

CMP AX, BX
JE EQUAL
--- --- ; Next instruction if (AX)≠(BX)
.
.
EQUAL: --- --- ; Next instruction if (AX)=(BX)
.
.
--- ---

IF-THEN branch program structure using a flag-condition test

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-23 林達德

6.3 Control Flow and Jump


Instructions
„ Branch program structure – IF-THEN

AND AL, 04H


JNZ BIT2_ONE
--- --- ; Next instruction if B2 of AL=0
.
.
BIT2_ONE: --- --- ; Next instruction if B2 of AL=1
.
.
--- ---

IF-THEN branch program structure using register-bit test

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-24 林達德

12
6.3 Control Flow and Jump
Instructions
„ Branch program structure – IF-THEN

MOV CL, 03H


SHR AL, CL
JC BIT2_ONE
--- --- ; Next instruction if B2 of AL=0
.
.
BIT2_ONE: --- --- ; Next instruction if B2 of AL=1
.
.
--- ---

IF-THEN branch program structure using an alternative register-bit test

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-25 林達德

6.3 Control Flow and Jump


Instructions
„ Loop program structures – REPEAT-UNTIL
Start

Initialize repeat
count
AGAIN
Loop program
statements

Decrement
repeat count

NO
Loop done?
(cc=false)
(cc=true) YES
Continue

REPEAT-UNTIL program sequence


國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-26 林達德

13
6.3 Control Flow and Jump
Instructions
„ Loop program structures – REPEAT-UNTIL

MOV CL, COUNT ; Set loop repeat count


AGAIN: --- --- ; First instruction of loop
--- --- ; Second instruction of loop .
.
.
.
--- --- ; nth instruction of loop
DEC CL ; Decrement repeat count by 1
JNZ AGAIN ; Repeat from AGAIN if (CL) ≠00H and (ZF)=0
--- --- ; First instruction executed after the loop is
;complete, (CL) =00H and (ZF)=1

Typical REPEAT-UNTIL instruction sequence

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-27 林達德

6.3 Control Flow and Jump


Instructions
„ Loop program structures – WHILE-DO
Start

Initialize repeat
count
AGAIN
YES
Loop done?
NO
Loop program
statements

Decrement
repeat count

Repeat NEXT

WHILE-DO program sequence 國立台灣大學


生物機電系
611 37100 微處理機原理與應用 Lecture 06-28 林達德

14
6.3 Control Flow and Jump
Instructions
„ Loop program structures – WHILE-DO

MOV CL, COUNT ; Set loop repeat count


AGAIN: JZ NEXT ; Loop is complete is CL=0 (ZF=1)
--- --- ; First instruction of loop
--- --- ; Second instruction of loop
.
.
.
--- --- ; nth instruction of loop
DEC CL ; Decrement repeat count by 1
JMP AGAIN ; Repeat from AGAIN
NEXT: --- --- ; First instruction executed after the
; loop is complete

Typical WHILE-DO instruction sequence


國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-29 林達德

6.3 Control Flow and Jump


Instructions
„ Example – The block-move program
Start

Establish the data segment, source block


and destination block
Initialization
Set up a counter for the points
to be removed

NXTPT

Move the next source point to


NO the accumulator
Data movement
Move the accumulator to the
next destination point

Update counter, source pointer,


and destination pointer
Update

NO All points Test


Moved?
YES
Stop
國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-30 林達德

15
6.3 Control Flow and Jump
Instructions
„ Example – The block-move program

MOV AX, DATASEGADDR


MOV DS, AX
MOV SI, BLK1ADDR
MOV DI, BLK2ADDR
MOV CX, N
NXTPT: MOV AH, [SI]
MOV [DI], AH
INC SI
INC DI
DEC CX
JNZ NXTPT
HLT

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-31 林達德

6.3 Control Flow and Jump


Instructions
EXAMPLE
Implement an instruction sequence that calculates the absolute
difference between the contents of AX and BX and places it in DX.
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

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-32 林達德

16
6.4 Subroutines and Subroutine-
Handling Instructions
„ A subroutine is a special program that can be
called for execution from any point in a
program.
„ A subroutine is also known as a procedure.
„ A return instruction must be included at the
end of the subroutine to initiate the return
sequence to the main program environment.
„ CALL and RET instructions
„ PUSH and POP instructions

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-33 林達德

6.4 Subroutines and Subroutine-


Handling Instructions
Main program

.
.
. Subroutine A

Call subroutine A First instruction

Next instruction .
.
. .
. .
. .
.
Call subroutine A .
.
Next instruction
Return
.
.
.

Subroutine concept
國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-34 林達德

17
6.4 Subroutines and Subroutine-
Handling Instructions
„ The CALL instruction
Mnemonic Meaning Format Operation Flags
affected
CALL Subroutine CALL Operand Execution continues from None
the address of the
call subroutine specified by
the operand. Information
required to return back to
the main program such as
IP and CS are saved on
the stack

Operands
Near-proc
Far-proc
Memptr16

Regptr16

Memptr32
Allowed operands for CALL instruction 國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-35 林達德

6.4 Subroutines and Subroutine-


Handling Instructions
„ The RET instruction

Mnemonic Meaning Format Operation Flags


affected
RET Return RET or Return to the main None
program by restoring
RET Operand
IP (and CS for far-
proc). If Operand is
present, it is added to
the contents of SP

Operands
None

Disp16

Allowed operands for RET instruction

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-36 林達德

18
6.4 Subroutines and Subroutine-
Handling Instructions
EXAMPLE TITLE EXAMPLE 6.10
PAGE ,132
STACK_SEG SEGMENT STACK 'STACK'
DB 64 DUP(?)
STACK_SEG ENDS
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

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-37 林達德

6.4 Subroutines and Subroutine-


Handling Instructions
EXAMPLE

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-38 林達德

19
6.4 Subroutines and Subroutine-
Handling Instructions
EXAMPLE

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-39 林達德

6.4 Subroutines and Subroutine-


Handling Instructions
EXAMPLE

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-40 林達德

20
6.4 Subroutines and Subroutine-
Handling Instructions
„ The PUSH and POP instructions
PUSH XX
To save registers and
PUSH YY
parameters on the stack PUSH ZZ
.
.
.
.
Main body of the subroutine .
.
.

POP ZZ
To restore registers and POP YY
Parameters from the stack POP XX

Return to main program RET

Structure of a subroutine
國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-41 林達德

6.4 Subroutines and Subroutine-


Handling Instructions
„ The PUSH and POP instructions

Mnemonic Meaning Format Operation Flags


affected
PUSH Push word onto stack PUSH S ((SP))←(S) None
(SP) ←(SP)-2

POP Pop word off stack POP D (D)←((SP)) None


(SP) ←(SP)+2

Operands (S or D)
Register

Seg-reg (CS illegal)

Memory

Allowed operands for PUSH and POP instruction


國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-42 林達德

21
6.4 Subroutines and Subroutine-
Handling Instructions
EXAMPLE
Write a procedure named SQUARE that squares the contents of
BL and places the result in BX
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
國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-43 林達德

6.4 Subroutines and Subroutine-


Handling Instructions
„ The PUSHF and POPF instructions

Mnemonic Meaning Operation Flags affected


PUSHF Push flag onto stack ((SP))←(Flags) None
(SP) ←(SP)-2

POPF Pop word off stack (Flags)←((SP)) OF,DF,IF,TF,SF,ZF,


(SP) ←(SP)+2 AF,PF,CF

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-44 林達德

22
6.5 The Loop and the Loop-Handling
Instructions
„ The LOOP instructions
Mnemonic Meaning Format Operation
LOOP Loop LOOP Short-label (CX) ←(CX)-1
Jump is initiated to
location defined by short-
label if (CX)≠0; otherwise,
execute next sequential
instruction

LOOPE Loop while equal LOOPE/LOOPZ (CX) ←(CX)-1


Short-label Jump to location defined
LOOPZ Loop while zero by short-label if (CX)≠0
and (ZF)=1; otherwise,
execute next sequential
instruction

LOOPNE Loop while not equal LOOPNE/LOOPNZ (CX) ←(CX)-1


LOOPNZ Short-label Jump to location defined
Loop while not zero by short-label if (CX)≠0
and (ZF)=0; otherwise,
execute next sequential
instruction

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-45 林達德

6.5 The Loop and the Loop-Handling


Instructions
„ The LOOP instructions

MOV CX, COUNT ; Load count for the number of repeats


NEXT: .
.
. ; Body of routine that is repeated
.
.
LOOP NEXT ; Loop back to label NEXT if count not zero

Typical loop routine structure

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-46 林達德

23
6.5 The Loop and the Loop-Handling
Instructions
„ Example – The block-move program

MOV AX, DATASEGADDR


MOV DS, AX
MOV SI, BLK1ADDR
MOV DI, BLK2ADDR
MOV CX, N
NXTPT: MOV AH, [SI]
MOV [DI], AH
INC SI
INC DI
LOOP NXTPT
HLT

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-47 林達德

6.5 The Loop and the Loop-Handling


Instructions
EXAMPLE
Given the following sequence of instructions, explain what
happens as they are executed.

MOV DL, 05
MOV AX, 0A00H
MOV DS, AX
MOV SI, 0
MOV CX, 0FH
AGAIN: INC SI
CMP [SI], DL
LOOPNE AGAIN

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-48 林達德

24
6.5 The Loop and the Loop-Handling
Instructions
EXAMPLE

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-49 林達德

6.5 The Loop and the Loop-Handling


Instructions
EXAMPLE

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-50 林達德

25
6.6 String and String-Handling
Instructions
„ The basic string instructions
Mnemonic Meaning Format Operation Flags
affected
MOVS Move string MOVSB ((ES)0+(DI))←((DS)0+(SI)) None
(SI) ←(SI)±1 or 2
MOVSW (DI) ←(DI)±1 or 2

CMPS Compare string CMPSB Set flags as per CF,PF,AF,


((DS)0+(SI))-((ES)0+(DI)) ZF,SF,OF
CMPSW (SI) ←(SI)±1 or 2
(DI) ←(DI)±1 or 2

SCAS Scan string SCASB Set flags as per CF,PF,AF,


(AL or AX)-((ES)0+(DI)) ZF,SF,OF
SCASW (DI) ←(DI)±1 or 2

LODS Load string LODSB (AL or AX)-((DS)0+(SI)) None


(SI) ←(SI)±1 or 2
LODSW
STOS Store string STOSB ((ES)0+(DI))← (AL or AX) None
±1 or 2
STOSW (DI) ←(DI)±1 or 2

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-51 林達德

6.6 String and String-Handling


Instructions
„ Move string – MOVSB, MOVSW
Example – The block-move program using the move-string
instruction

MOV AX, DATASEGADDR


MOV DS, AX
MOV ES, AX
MOV SI, BLK1ADDR
MOV DI, BLK2ADDR
MOV CX, N
CLD
NXTPT: MOVSB
LOOP NXTPT
HLT

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-52 林達德

26
6.6 String and String-Handling
Instructions
„ Compare string and scan string –
CMPSB/CMPSW, SCASB/SCASW
Example – Block scan operation using the SCASB instruction

MOV AX, DATASEGADDR


MOV DS, AX
MOV ES, AX
MOV AL, 05
MOV DI, 0A000H
MOV CX, 0FH
CLD
AGAIN: SCASB
LOOPNE AGAIN
NEXT:

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-53 林達德

6.6 String and String-Handling


Instructions
„ Load and store string –
LODSB/LODSW, STOSB/STOSW
Example – Initializing a block of memory with a store string
instruction

MOV AX, 0
MOV DS, AX
MOV ES, AX
MOV AL, 05
MOV DI, 0A000H
MOV CX, 0FH
CLD
AGAIN: STOSB
LOOP AGAIN

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-54 林達德

27
6.6 String and String-Handling
Instructions
„ REP string – REP (repeat prefixes)

Prefix Used with: Meaning


REP MOVS Repeat while not end of string
STOS CX≠0
REPE/REPZ CMPS Repeat while not end of string and
SCAS strings are equal
CX≠0 and ZF=1
REPNE/REPNZ CMPS Repeat while not end of string and
SCAS strings are not equal
CX≠0 and ZF=0

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-55 林達德

6.6 String and String-Handling


Instructions
„ REP string – REP (repeat prefixes)

Example – Initializing a block of memory by repeating the


STOSB instruction

MOV AX, 0
MOV DS, AX
MOV ES, AX
MOV AL, 05
MOV DI, 0A000H
MOV CX, 0FH
CLD
REPSTOSB

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-56 林達德

28
6.6 String and String-Handling
Instructions
„ Autoindexing for string instruction –
CLD and STD instructions

Mnemonic Meaning Format Operation Flags


affected
CLD Clear DF CLD (DF)←0 DF
STD Set DF STD (DF)←1 DF

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-57 林達德

6.6 String and String-Handling


Instructions
EXAMPLE
Describe what happen as the following sequence of instruction
is executed.

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

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-58 林達德

29
6.6 String and String-Handling
Instructions
EXAMPLE

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-59 林達德

6.6 String and String-Handling


Instructions
EXAMPLE

國立台灣大學
生物機電系
611 37100 微處理機原理與應用 Lecture 06-60 林達德

30

You might also like