8086 micro processer control programing

30
8088/8086 MICROPROCESSOR PROGRAMMI NG – CONTROL FLOW INSTRUCTIONS AND PROGRAM STRUCTURES    國立台灣大生物機電系 林達德 611 37100   微處理機原理與應用 Lecture 06-2 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

Transcript of 8086 micro processer control programing

Page 1: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 1/30

8088/8086 MICROPROCESSOR

PROGRAMMING – CONTROLFLOW INSTRUCTIONS AND

PROGRAM STRUCTURES 

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-2

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 Instructions6.4 Subroutines and Subroutine-Handling

Instructions

6.5 The Loop and the Loop-HandlingInstructions

6.6 String and String-Handling Instructions

Page 2: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 2/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-3

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-4

6.1 Flag-Control Instructions

IF(IF)  1Set interrupt flagSTI

IF(IF)  0Clear interrupt flagCLI

CF(CF)  NOT (CF)Complement carry flagCMC

CF(CF)  1Set carry flagSTC

CF(CF)  0Clear carry flagCLC

SF,ZF,AF,PF,CF(Flags)  (AH)Store AH into flagsSAHF

None(AH)  (Flags)Load AH from flagsLAHF

Flags affectedOperationMeaningMnemonic

SF ZF - AF PF- - CF

7 0

AH

SF = Sign flag

ZF = Zero flag

AF = Auxiliary

PF = Parity flag

CF = Carry flag

- = Undefined (do not use)

Page 3: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 3/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-5

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 ofthe 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-6

6.1 Flag-Control Instructions

Page 4: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 4/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-7

6.1 Flag-Control Instructions

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-8

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.

Page 5: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 5/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-9

6.1 Flag-Control Instructions

EXAMPLE

Verify the operation of the following instructions that affect the

carry flag,

CLCSTC

CMC

by executing them with the DEBUG program. Start with CF flag set

to 1 (CY).

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-10

6.1 Flag-Control Instructions

Page 6: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 6/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-11

6.2 Compare Instruction

The Compare Instruction:

The compare operation enable us to determine

the relationship between two numbers.

CF, AF, OF, PF, SF,ZF

(D)-(S) is used in settingor resetting the flags

CMP D, SCompareCMP

Flags affectedOperationFormatMeaningMnemonic

ImmediateAccumulator 

ImmediateMemory

ImmediateRegister 

Register Memory

MemoryRegister 

Register Register 

SourceDestination

 Allowed operands for compare instruction

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-12

6.2 Compare Instruction

EXAMPLEDescribe 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

Page 7: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 7/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-13

6.2 Compare Instruction

The Compare Instruction:

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-14

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

Page 8: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 8/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-15

6.3 Control Flow and Jump

Instructions Unconditional and conditional jump

Part I

JMP AA

Part II

AA XXXXXX

Part III

Unconditional jumpinstruction

Locations skipped dueto jump

Next instruction

executed

Unconditional jump program sequence

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-16

6.3 Control Flow and Jump

Instructions Unconditional and conditional jump

Part I

Jcc AA

XXXXXX

Part II

AA XXXXXX

Part III

Conditional jump

instruction

Locations skipped due

to jump

Next instructionexecuted if 

condition met

Conditional jump program sequence

Next instruction executed

If condition not met

Condition

met?

 Yes

No

Page 9: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 9/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-17

6.3 Control Flow and Jump

Instructions Unconditional jump instruction

NoneJump is initiated

to the addressspecified by the

operand

JMP OperandUnconditional

 jump

JMP

Flags

affected

OperationFormatMeaningMnemonic

Memptr32

Regptr16

Memptr16

Far-label

Near-label

Short-label

Operands

 Allowed operands for JMP instruction

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-18

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:

Page 10: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 10/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-19

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-20

6.3 Control Flow and Jump

Instructions

Conditional jump instruction

NoneIf the specified

condition cc is true

the jump to the

address specifiedby the operand is

initiated; otherwise

the next instructionis executed

Jcc OperandConditional

 jump

Jcc

Flags

affected

OperationFormatMeaningMnemonic

Page 11: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 11/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-21

6.3 Control Flow and Jump

Instructions Conditional jump instruction

CF=0 and ZF=0Not below nor equalJNBE

CF=0Not belowJNB

CF=1Not above nor equalJNAE

CF=1 or ZF=1Not aboveJNA

Less or equal

Less

Greater or equal

Greater 

Equal

CX register is zero

Carry

Below or equalBelow

Above or equal

Above

Meaning

((SF xor OF) or ZF)=1JLE

(SF xor OF)=1JL

SF=OFJGE

ZF=0 and SF=OFJG

ZF=1JE

(CF or ZF)=0JCXZ

CF=1JC

CF=1 or ZF=1JBECF=1JB

CF=0JAE

CF=0 and ZF=0JA

ConditionMnemonic

Type of conditional jump instructions

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-22

6.3 Control Flow and Jump

Instructions Conditional jump instruction

ZF=1ZeroJZ

SF=1SignJS

PF=0Parity oddJPO

PF=1Parity evenJPE

PF=1ParityJP

Overflow

Not zero

Not sign

Not parity

Not overflow

Not less or nor equal

Not less

Not greater nor equal

Not greater 

Not equal

Not carry

Meaning

OF=1JO

ZF=0JNZ

SF=0JNS

PF=0JNP

OF=0JNO

ZF=0 and SF=OFJNLE

SF=OFJNL

(SF xor OF)=1JNGE

((SF xor OF) or ZF)=1JNG

ZF=0JNE

CF=0JNC

ConditionMnemonic

Page 12: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 12/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-23

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-24

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

Page 13: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 13/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-25

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-26

6.3 Control Flow and Jump

Instructions Loop program structures  – REPEAT-UNTIL

REPEAT-UNTIL program sequence

Start

Initialize repeat

count

Loop program

statements

Decrement

repeat count

Loop done?

Continue

AGAIN

NO

(cc=false)(cc=true)  YES

Page 14: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 14/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-27

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-28

6.3 Control Flow and Jump

Instructions Loop program structures  – WHILE-DO

WHILE-DO program sequence

Start

Initialize repeat

count

Loop program

statements

Decrement

repeat count

Loop done?

AGAIN

NO

 YES

Repeat NEXT

Page 15: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 15/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-29

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-30

6.3 Control Flow and Jump

Instructions Example  – The block-move program

Start

Establish the data segment, source block

and destination block

All points

Moved?

NXTPT

NO

 YES

Set up a counter for the points

to be removed

Move the next source point to

the accumulator 

Move the accumulator to the

next destination point

Update counter, source pointer,

and destination pointer 

Stop

NO

Initialization

Data movement

Update

Test

Page 16: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 16/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-31

6.3 Control Flow and Jump

Instructions Example  – The block-move program

MOV AX, DATASEGADDR

MOV DS, AXMOV 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-32

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 DIFF2DIFF1: MOV DX, AX

SUB DX, BX ; (DX)=(AX)-(BX)

JMP DONE

DIFF2: MOV DX, BX

SUB DX, AX ; (DX)=(BX)-(AX)

DONE: NOP

Page 17: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 17/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-33

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-34

6.4 Subroutines and Subroutine- 

Handling Instructions

.

.

.

Main program

Subroutine concept

Call subroutine A

Next instruction

.

.

.

Call subroutine A

Next instruction

.

.

.

Subroutine A

First instruction

.

.

.

.

.

.

.

.

Return

Page 18: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 18/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-35

6.4 Subroutines and Subroutine- 

Handling Instructions

The CALL instruction

NoneExecution continues fromthe address of the

subroutine specified by

the operand. Information

required to return back to

the main program such as

IP and CS are saved on

the stack

CALL OperandSubroutinecall

CALL

Flags

affected

OperationFormatMeaningMnemonic

Memptr32

Regptr16

Memptr16

Far-proc

Near-proc

Operands

 Allowed operands for CALL instruction

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-36

6.4 Subroutines and Subroutine- 

Handling Instructions

The RET instruction

NoneReturn to the main

program by restoringIP (and CS for far-

proc). If Operand is

present, it is added to

the contents of SP

RET or 

RET Operand

ReturnRET

Flags

affected

OperationFormatMeaningMnemonic

Disp16

None

Operands

 Allowed operands for RET instruction

Page 19: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 19/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-37

6.4 Subroutines and Subroutine- 

Handling Instructions

EXAMPLETITLE 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-38

6.4 Subroutines and Subroutine- 

Handling Instructions

EXAMPLE

Page 20: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 20/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-39

6.4 Subroutines and Subroutine- 

Handling Instructions

EXAMPLE

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-40

6.4 Subroutines and Subroutine- 

Handling Instructions

EXAMPLE

Page 21: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 21/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-41

6.4 Subroutines and Subroutine- 

Handling Instructions

The PUSH and POP instructions

To save registers and

parameters on the stack

Main body of the subroutine

To restore registers and

Parameters from the stack

PUSH XX

PUSH YY

PUSH ZZ

Return to main program

.

.

.

.

.

.

.

POP ZZ

POP YYPOP XX

RET

Structure of a subroutine

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-42

6.4 Subroutines and Subroutine- 

Handling Instructions

The PUSH and POP instructions

None(D)  ((SP))

(SP) (SP)+2

POP DPop word off stackPOP

None((SP))  (S)

(SP) (SP)-2

PUSH SPush word onto stackPUSH

Flags

affected

OperationFormatMeaningMnemonic

Memory

Seg-reg (CS illegal)

Register 

Operands (S or D)

 Allowed operands for PUSH and POP instruction

Page 22: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 22/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-43

6.4 Subroutines and Subroutine- 

Handling Instructions

EXAMPLEWrite 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-44

6.4 Subroutines and Subroutine- 

Handling Instructions

The PUSHF and POPF instructions

OF,DF,IF,TF,SF,ZF,AF,PF,CF

(Flags)  ((SP))(SP) (SP)+2

Pop word off stackPOPF

None((SP))  (Flags)

(SP) (SP)-2

Push flag onto stackPUSHF

Flags affectedOperationMeaningMnemonic

Page 23: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 23/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-45

6.5 The Loop and the Loop-Handling

Instructions

The LOOP instructions

(CX) (CX)-1

Jump to location defined

by short-label if (CX)≠0and (ZF)=0; otherwise,

execute next sequential

instruction

LOOPNE/LOOPNZ

Short-labelLoop while not equal

Loop while not zero

LOOPNE

LOOPNZ

(CX) (CX)-1

Jump to location defined

by short-label if (CX)≠0

and (ZF)=1; otherwise,

execute next sequential

instruction

LOOPE/LOOPZ

Short-labelLoop while equal

Loop while zero

LOOPE

LOOPZ

(CX) (CX)-1

Jump is initiated to

location defined by short-

label if (CX)≠0; otherwise,

execute next sequential

instruction

LOOP Short-labelLoopLOOP

OperationFormatMeaningMnemonic

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-46

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

Page 24: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 24/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-47

6.5 The Loop and the Loop-Handling

Instructions

Example  – The block-move program

MOV AX, DATASEGADDRMOV 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-48

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, 0A00HMOV DS, AX

MOV SI, 0

MOV CX, 0FH

 AGAIN: INC SI

CMP [SI], DL

LOOPNE AGAIN

Page 25: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 25/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-49

6.5 The Loop and the Loop-Handling

Instructions

EXAMPLE

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-50

6.5 The Loop and the Loop-Handling

Instructions

EXAMPLE

Page 26: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 26/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-51

6.6 String and String-Handling

Instructions The basic string instructions

None((ES)0+(DI))  (AL or AX)

1 or 2

(DI) (DI) 1 or 2

STOSB

STOSW

Store stringSTOS

None(AL or AX)-((DS)0+(SI))

(SI) (SI) 1 or 2LODSB

LODSW

Load stringLODS

CF,PF,AF,

ZF,SF,OF

Set flags as per 

(AL or AX)-((ES)0+(DI))

(DI) (DI) 1 or 2

SCASB

SCASW

Scan stringSCAS

CF,PF,AF,

ZF,SF,OF

Set flags as per 

((DS)0+(SI))-((ES)0+(DI))

(SI) (SI) 1 or 2

(DI) (DI) 1 or 2

CMPSB

CMPSW

Compare stringCMPS

None((ES)0+(DI))  ((DS)0+(SI))

(SI) (SI) 1 or 2

(DI) (DI) 1 or 2

MOVSB

MOVSW

Move stringMOVS

Flags

affected

OperationFormatMeaningMnemonic

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-52

6.6 String and String-Handling

Instructions

Move string  – MOVSB, MOVSWExample  – The block-move program using the move-string

instruction

MOV AX, DATASEGADDR

MOV DS, AXMOV ES, AX

MOV SI, BLK1ADDR

MOV DI, BLK2ADDR

MOV CX, N

CLD

NXTPT: MOVSB

LOOP NXTPT

HLT

Page 27: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 27/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-53

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-54

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, 0MOV DS, AX

MOV ES, AX

MOV AL, 05

MOV DI, 0A000H

MOV CX, 0FH

CLD

AGAIN: STOSB

LOOP AGAIN

Page 28: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 28/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-55

6.6 String and String-Handling

Instructions

REP string  – REP (repeat prefixes)

Repeat while not end of string and

strings are not equal

CX≠0 and ZF=0

CMPS

SCAS

REPNE/REPNZ

Repeat while not end of string and

strings are equal

CX≠0 and ZF=1

CMPS

SCAS

REPE/REPZ

Repeat while not end of string

CX≠0

MOVS

STOS

REPMeaningUsed with:Prefix

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-56

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, 0MOV DS, AX

MOV ES, AX

MOV AL, 05

MOV DI, 0A000H

MOV CX, 0FH

CLD

REPSTOSB

Page 29: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 29/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-57

6.6 String and String-Handling

Instructions

 Autoindexing for string instruction  – 

CLD and STD instructions

DF(DF)  1STDSet DFSTD

DF(DF)  0CLDClear DFCLD

Flags

affected

OperationFormatMeaningMnemonic

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-58

6.6 String and String-Handling

Instructions

EXAMPLEDescribe what happen as the following sequence of instruction

is executed.

CLD

MOV AX, DATA_SEGMENT

MOV DS, AXMOV AX, EXTRA_SEGMENT

MOV ES, AX

MOV CX, 20H

MOV SI, OFFSET MASTER

MOV DI, OFFSET COPY

REPZMOVSB

Page 30: 8086 micro processer control programing

8/20/2019 8086 micro processer control programing

http://slidepdf.com/reader/full/8086-micro-processer-control-programing 30/30

   國立台灣大學

生物機電系

林達德611 37100  微處理機原理與應用 Lecture 06-59

6.6 String and String-Handling

Instructions

EXAMPLE

灣 學

6.6 String and String-Handling

Instructions

EXAMPLE