30
8088/8086 MICROPROCESSOR PROGRAMMING – 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

8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

1

8088/8086 MICROPROCESSOR PROGRAMMING – 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 Instructions6.2 Compare Instructions6.3 Control Flow and Jump Instructions6.4 Subroutines and Subroutine-Handling

Instructions6.5 The Loop and the Loop-Handling

Instructions6.6 String and String-Handling Instructions

Page 2: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

2

國立台灣大學生物機電系林達德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 flagSTIIF(IF)←0Clear interrupt flagCLICF(CF)←NOT (CF)Complement carry flagCMCCF(CF)←1Set carry flagSTCCF(CF)←0Clear carry flagCLCSF,ZF,AF,PF,CF(Flags)←(AH)Store AH into flagsSAHFNone(AH)←(Flags)Load AH from flagsLAHF

Flags affectedOperationMeaningMnemonic

SF ZF - AF PF- - CF

7 0

AH

SF = Sign flagZF = Zero flagAF = AuxiliaryPF = Parity flagCF = Carry flag- = Undefined (do not use)

Page 3: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

3

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

6.1 Flag-Control Instructions

EXAMPLEWrite 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 flagsMOV [MEM1], AH ; Move content of AH to MEM1MOV AH, [MEM2] ; Load AH from MEM2SAHF ; Store content of AH into flags

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

6.1 Flag-Control Instructions

Page 4: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

4

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

6.1 Flag-Control Instructions

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

6.1 Flag-Control Instructions

EXAMPLEOf 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 CMCSTC ⇔ CLC followed by a CMC

Therefore, only CMC is the independent instruction.

Page 5: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

5

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

6.1 Flag-Control Instructions

EXAMPLEVerify the operation of the following instructions that affect the

carry flag,CLCSTCCMC

by executing them with the DEBUG program. Start with CF flag setto 1 (CY).

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

6.1 Flag-Control Instructions

Page 6: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

6

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

6.2 Compare InstructionThe 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 setting or resetting the flags

CMP D, SCompareCMPFlags affectedOperationFormatMeaningMnemonic

ImmediateAccumulatorImmediateMemoryImmediateRegisterRegisterMemoryMemoryRegisterRegisterRegister

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, 1234HMOV BX, 0ABCDHCMP AX, BX

Solution:(AX) = 123416 = 00010010001101002

(BX) = ABCD16 = 10101011110011012(AX) – (BX) = 00010010001101002 - 10101011110011012

= 01100110011001112Therefore, ZF = 0, SF = 0, OF = 0, PF = 0

CF = 1, AF = 1

Page 7: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

7

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

6.2 Compare InstructionThe Compare Instruction:

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

6.3 Control Flow and Jump Instructions

Unconditional jump instructionConditional jump instructionBranching structure – IF-THENLoop program structure – REPEAT-UNTIL and WHILE-DOApplications using the loop and branch software structures

Page 8: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

8

國立台灣大學生物機電系林達德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 instructionexecuted

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 jumpinstruction

Locations skipped dueto jumpNext instructionexecuted ifcondition met

Conditional jump program sequence

Next instruction executedIf condition not met

Conditionmet?

Yes

No

Page 9: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

9

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

6.3 Control Flow and Jump Instructions

Unconditional jump instruction

NoneJump is initiated to the address specified by the operand

JMP OperandUnconditional jump

JMP

Flags affected

OperationFormatMeaningMnemonic

Memptr32

Regptr16

Memptr16

Far-labelNear-labelShort-label

Operands

Allowed operands for JMP instruction

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

6.3 Control Flow and Jump Instructions

EXAMPLEVerify the operation of the instruction JMP BX using the

DEBUG program. Let the contents of BX be 001016.

Solution:

Page 10: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

10

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

6.3 Control Flow and Jump Instructions

EXAMPLEUse 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 specified by the operand is initiated; otherwise the next instruction is executed

Jcc OperandConditional jump

Jcc

Flags affected

OperationFormatMeaningMnemonic

Page 11: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

11

國立台灣大學生物機電系林達德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 equal

Below

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=1JBE

CF=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: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

12

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

6.3 Control Flow and Jump Instructions

Branch program structure – IF-THEN

CMP AX, BXJE 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, 04HJNZ 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: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

13

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

6.3 Control Flow and Jump Instructions

Branch program structure – IF-THEN

MOV CL, 03HSHR AL, CLJC 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 repeatcount

Loop programstatements

Decrementrepeat count

Loop done?

Continue

AGAIN

NO(cc=false)

(cc=true) YES

Page 14: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

14

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

6.3 Control Flow and Jump Instructions

Loop program structures – REPEAT-UNTIL

MOV CL, COUNT ; Set loop repeat countAGAIN: --- --- ; First instruction of loop

--- --- ; Second instruction of loop . ...

--- --- ; nth instruction of loopDEC CL ; Decrement repeat count by 1JNZ 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 repeatcount

Loop programstatements

Decrementrepeat count

Loop done?

AGAIN

NO

YES

Repeat NEXT

Page 15: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

15

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

6.3 Control Flow and Jump Instructions

Loop program structures – WHILE-DO

MOV CL, COUNT ; Set loop repeat countAGAIN: JZ NEXT ; Loop is complete is CL=0 (ZF=1)

--- --- ; First instruction of loop--- --- ; Second instruction of loop

.

.

.--- --- ; nth instruction of loopDEC CL ; Decrement repeat count by 1JMP 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 programStart

Establish the data segment, source blockand destination block

All pointsMoved?

NXTPT

NO

YES

Set up a counter for the pointsto 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: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

16

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

6.3 Control Flow and Jump Instructions

Example – The block-move program

MOV AX, DATASEGADDRMOV DS, AXMOV SI, BLK1ADDRMOV DI, BLK2ADDRMOV CX, N

NXTPT: MOV AH, [SI]MOV [DI], AHINC SIINC DIDEC CXJNZ NXTPTHLT

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

6.3 Control Flow and Jump Instructions

EXAMPLEImplement an instruction sequence that calculates the absolute

difference between the contents of AX and BX and places it in DX.

Solution:CMP AX, BXJC DIFF2

DIFF1: MOV DX, AXSUB DX, BX ; (DX)=(AX)-(BX)JMP DONE

DIFF2: MOV DX, BXSUB DX, AX ; (DX)=(BX)-(AX)

DONE: NOP

Page 17: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

17

國立台灣大學生物機電系林達德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 instructionsPUSH 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: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

18

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

6.4 Subroutines and Subroutine-Handling Instructions

The CALL instruction

NoneExecution continues from the 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 OperandSubroutine call

CALL

Flags affected

OperationFormatMeaningMnemonic

Memptr32

Regptr16

Memptr16Far-procNear-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 restoring IP (and CS for far-proc). If Operand is present, it is added to the contents of SP

RET orRET Operand

ReturnRET

Flags affected

OperationFormatMeaningMnemonic

Disp16

NoneOperands

Allowed operands for RET instruction

Page 19: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

19

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

6.4 Subroutines and Subroutine-Handling Instructions

EXAMPLE TITLE EXAMPLE 6.10PAGE ,132

STACK_SEG SEGMENT STACK 'STACK'DB 64 DUP(?)

STACK_SEG ENDSCODE_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 DSMOV AX, 0PUSH AX

;Following code implements Example 6.10CALL SUMRET

SUM PROC NEARMOV DX, AXADD DX, BX ; (DX)=(AX)+(BX)RET

SUM ENDPEX610 ENDPCODE_SEG ENDS

END EX610

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

6.4 Subroutines and Subroutine-Handling Instructions

EXAMPLE

Page 20: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

20

國立台灣大學生物機電系林達德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: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

21

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

6.4 Subroutines and Subroutine-Handling Instructions

The PUSH and POP instructionsTo save registers and

parameters on the stack

Main body of the subroutine

To restore registers andParameters from the stack

PUSH XXPUSH YYPUSH ZZ

Return to main program

.

.

.

.

.

.

.

POP ZZPOP 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)

RegisterOperands (S or D)

Allowed operands for PUSH and POP instruction

Page 22: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

22

國立台灣大學生物機電系林達德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 BXSolution:

;Subroutine: SQUARE;Description: (BX)=square of (BL)SQUARE PROC NEAR

PUSH AX ; Save the register to be usedMOV AX, BX ; Place the number in ALIMUL BL ; Multiply with itselfMOV BX, AX ; Save the resultPOP AX ; Restore the register usedRET

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 stackPUSHFFlags affectedOperationMeaningMnemonic

Page 23: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

23

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

6.5 The Loop and the Loop-Handling Instructions

The LOOP instructions

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

LOOPNE/LOOPNZ Short-label

Loop while not equalLoop while not zero

LOOPNE LOOPNZ

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

LOOPE/LOOPZ Short-label

Loop while equalLoop while zero

LOOPELOOPZ

(CX) ←(CX)-1Jump is initiated to location defined by short-label if (CX)≠0; otherwise, execute next sequential instruction

LOOP Short-labelLoopLOOPOperationFormatMeaningMnemonic

國立台灣大學生物機電系林達德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 repeatsNEXT: .

.

. ; Body of routine that is repeated

.

.LOOP NEXT ; Loop back to label NEXT if count not zero

Typical loop routine structure

Page 24: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

24

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

6.5 The Loop and the Loop-Handling Instructions

Example – The block-move program

MOV AX, DATASEGADDRMOV DS, AXMOV SI, BLK1ADDRMOV DI, BLK2ADDRMOV CX, N

NXTPT: MOV AH, [SI]MOV [DI], AHINC SIINC DILOOP NXTPTHLT

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

6.5 The Loop and the Loop-Handling Instructions

EXAMPLEGiven the following sequence of instructions, explain what

happens as they are executed.

MOV DL, 05MOV AX, 0A00HMOV DS, AXMOV SI, 0MOV CX, 0FH

AGAIN: INC SICMP [SI], DLLOOPNE AGAIN

Page 25: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

25

國立台灣大學生物機電系林達德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: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

26

國立台灣大學生物機電系林達德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

STOSBSTOSW

Store stringSTOS

None(AL or AX)-((DS)0+(SI))(SI) ←(SI)±1 or 2

LODSBLODSW

Load stringLODS

CF,PF,AF,ZF,SF,OF

Set flags as per(AL or AX)-((ES)0+(DI))(DI) ←(DI)±1 or 2

SCASBSCASW

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

CMPSBCMPSW

Compare stringCMPS

None((ES)0+(DI))←((DS)0+(SI))(SI) ←(SI)±1 or 2(DI) ←(DI)±1 or 2

MOVSBMOVSW

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, DATASEGADDRMOV DS, AXMOV ES, AXMOV SI, BLK1ADDRMOV DI, BLK2ADDRMOV CX, NCLD

NXTPT: MOVSBLOOP NXTPTHLT

Page 27: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

27

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

6.6 String and String-Handling Instructions

Compare string and scan string –CMPSB/CMPSW, SCASB/SCASWExample – Block scan operation using the SCASB instruction

MOV AX, DATASEGADDRMOV DS, AXMOV ES, AXMOV AL, 05MOV DI, 0A000HMOV CX, 0FHCLD

AGAIN: SCASBLOOPNE AGAIN

NEXT:

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

6.6 String and String-Handling Instructions

Load and store string –LODSB/LODSW, STOSB/STOSWExample – Initializing a block of memory with a store string

instruction

MOV AX, 0MOV DS, AXMOV ES, AXMOV AL, 05MOV DI, 0A000HMOV CX, 0FHCLD

AGAIN: STOSBLOOP AGAIN

Page 28: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

28

國立台灣大學生物機電系林達德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 equalCX≠0 and ZF=0

CMPSSCAS

REPNE/REPNZ

Repeat while not end of string and strings are equalCX≠0 and ZF=1

CMPSSCAS

REPE/REPZ

Repeat while not end of stringCX≠0

MOVSSTOS

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, AXMOV ES, AXMOV AL, 05MOV DI, 0A000HMOV CX, 0FHCLDREPSTOSB

Page 29: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

29

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

6.6 String and String-Handling Instructions

Autoindexing for string instruction –CLD and STD instructions

DF(DF)←1STDSet DFSTDDF(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.

CLDMOV AX, DATA_SEGMENTMOV DS, AXMOV AX, EXTRA_SEGMENTMOV ES, AXMOV CX, 20HMOV SI, OFFSET MASTERMOV DI, OFFSET COPYREPZMOVSB

Page 30: 8088/8086 MICROPROCESSOR PROGRAMMING – CONTROL FLOW INSTRUCTIONS …nova.bime.ntu.edu.tw/~ttlin/Course15/lecture_notes/C15... · 2004. 3. 24. · PROGRAMMING – CONTROL FLOW INSTRUCTIONS

30

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

6.6 String and String-Handling Instructions

EXAMPLE

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

6.6 String and String-Handling Instructions

EXAMPLE