Microsoft PowerPoint - C15_LECTURE_NOTE_06

Similar documents
Microsoft PowerPoint - C15_LECTURE_NOTE_06

,768 32,767 32K JMP Jnnn (386+) LOOP CALL [Label:] JMP short/near/far address L10: jmp jmp L20: L10 L20

Microsoft PowerPoint - C15_LECTURE_NOTE_05.ppt

untitled

Microsoft PowerPoint - C15_LECTURE_NOTE_05.ppt

Microsoft PowerPoint - C15_LECTURE_NOTE_04.ppt

Microsoft PowerPoint - C15_LECTURE_NOTE_04.ppt

幻灯片 1

<4D F736F F D20C7B6C8EBCABDCFB5CDB3C9E8BCC6CAA6B0B8C0FDB5BCD1A75FD1F9D5C22E646F63>

Microsoft PowerPoint - C15_LECTURE_NOTE_11


学习MSP430单片机推荐参考书

(2) Function 0BH: Function 0CH: (pixel, picture element) Function 0DH: Function 0FH: Function 13H:

幻灯片 1

Microsoft PowerPoint - os_4.ppt

(Guangzhou) AIT Co, Ltd V 110V [ ]! 2

2/80 2

els0xu_zh_nf_v8.book Page Wednesday, June, 009 9:5 AM ELS-0/0C.8

1 CPU

Microsoft PowerPoint - CA_02 Chapter5 Part-I_Single _V2.ppt

Microsoft PowerPoint - Lecture7II.ppt

目 录

1 TPIS TPIS 2 2

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis

untitled

K7VT2_QIG_v3

Microsoft PowerPoint - STU_EC_Ch08.ppt

lan03_yen

Bus Hound 5

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Panaboard Overlayer help

微型计算机原理及应用试题 机电96

穨control.PDF

AL-M200 Series

第 15 章 程 式 編 写 語 言 15.1 程 式 編 写 語 言 的 角 色 程 式 編 寫 語 言 是 程 式 編 寫 員 與 電 腦 溝 通 的 界 面 語 法 是 一 組 規 則 讓 程 式 編 寫 員 將 字 詞 集 合 起 來 電 腦 是 處 理 位 元 和 字 節 的 機 器, 與

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

06721 main() lock pick proc() restart() [2][4] MINIX minix2.0 GDT, IDT irq table[] CPU CPU CPU CPU (IDTR) idt[] CPU _hwint00:! Interrupt

三維空間之機械手臂虛擬實境模擬


入學考試網上報名指南

PowerPoint Presentation

Gerolor Motors Series Dimensions A,B C T L L G1/2 M8 G1/ A 4 C H4 E

Microsoft Word - template.doc

WinMDI 28

<4D F736F F F696E74202D20B5DAD2BBD5C228B4F2D3A1B0E6292E BBCE6C8DDC4A3CABD5D>

1.ai

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

untitled

Microsoft PowerPoint - notes3-Simple-filled12

國 立 政 治 大 學 教 育 學 系 2016 新 生 入 學 手 冊 目 錄 表 11 國 立 政 治 大 學 教 育 學 系 博 士 班 資 格 考 試 抵 免 申 請 表 論 文 題 目 申 報 暨 指 導 教 授 表 12 國 立 政 治 大 學 碩 博 士 班 論

Guide to Install SATA Hard Disks

3.1 num = 3 ch = 'C' 2


ENGG1410-F Tutorial 6

(Load Project) (Save Project) (OffLine Mode) (Help) Intel Hex Motor

RS-232C [11-13] 1 1 (PLC) (HMI) Visual Basic (PLC) 402

Microsoft PowerPoint - C15_LECTURE_NOTE_15

中国人民大学商学院本科学年论文

<4D F736F F D C4EAC0EDB9A4C0E04142BCB6D4C4B6C1C5D0B6CFC0FDCCE2BEABD1A15F325F2E646F63>

4. 每 组 学 生 将 写 有 习 语 和 含 义 的 两 组 卡 片 分 别 洗 牌, 将 顺 序 打 乱, 然 后 将 两 组 卡 片 反 面 朝 上 置 于 课 桌 上 5. 学 生 依 次 从 两 组 卡 片 中 各 抽 取 一 张, 展 示 给 小 组 成 员, 并 大 声 朗 读 卡

iml88-0v C / 8W T Tube EVM - pplication Notes. IC Description The iml88 is a Three Terminal Current Controller (TTCC) for regulating the current flowi

<4D F736F F D203033BDD7A16DA576B04FA145A4ADABD2A5BBACF6A16EADBAB6C0ABD2A4A7B74EB8712E646F63>

. v dx v d () () l s dl s d (_) d () v s v s () a dv a d (_) ( ) ( ) x- = v- = = v 0 = m/s a = = m/s 2 a- = ( ) x- v- a- Page 2 of 20

IP505SM_manual_cn.doc

12232A LED LED LED EL EL CCFL EL CCF

(Microsoft Word - Motion Program \270\305\264\272\276\363 \307\245\301\366 \271\327 \270\361\302\367.doc)

Gerotor Motors Series Dimensions A,B C T L L G1/2 M G1/ A 4 C H4 E

東吳大學

Cadence SPB 15.2 VOICE Cadence SPB 15.2 PC Cadence 3 (1) CD1 1of 2 (2) CD2 2of 2 (3) CD3 Concept HDL 1of 1

T stg -40 to 125 C V cc 3.8V V dc RH 0 to 100 %RH T a -40 to +125 C -0.3 to 3.6V V -0.3 to VDD+0.3 V -10 to +10 ma = 25 = 3V) VDD

A Preliminary Implementation of Linux Kernel Virus and Process Hiding

第1章 簡介

C/C++ 语言 - 循环

C++ 程式設計

TX-NR3030_BAS_Cs_ indd

OVLFx3C7_Series_A3_bgry-KB.pub

第五章 重叠、流水和现代处理器技术

Chn 116 Neh.d.01.nis

2015 Chinese FL Written examination

Microsoft Word - HC20138_2010.doc

VASP应用运行优化

ebook121-20

綜合社會保障援助指引

Microsoft PowerPoint - C15_LECTURE_NOTE_09


ansoft_setup21.doc

科学计算的语言-FORTRAN95

iml v C / 0W EVM - pplication Notes. IC Description The iml8683 is a Three Terminal Current Controller (TTCC) for regulating the current flowin

iml v C / 4W Down-Light EVM - pplication Notes. IC Description The iml8683 is a Three Terminal Current Controller (TTCC) for regulating the cur

Microsoft Word doc

Edge-Triggered Rising Edge-Triggered ( Falling Edge-Triggered ( Unit 11 Latches and Flip-Flops 3 Timing for D Flip-Flop (Falling-Edge Trigger) Unit 11

微處理機期末專題

高中英文科教師甄試心得

構 築 4 列 牌 陣 從 剩 餘 的 牌 庫 頂 抽 4 張 牌, 面 朝 上 排 列 在 桌 子 中 央 這 4 張 牌 就 是 牌 陣 的 起 始 牌, 包 括 這 張 起 始 牌 在 內, 每 一 列 最 多 只 能 容 納 5 張 牌 將 剩 餘 的 牌 暫 時 置 於 一 旁, 在 下

2005 5,,,,,,,,,,,,,,,,, , , 2174, 7014 %, % 4, 1961, ,30, 30,, 4,1976,627,,,,, 3 (1993,12 ),, 2

untitled

Preface This guide is intended to standardize the use of the WeChat brand and ensure the brand's integrity and consistency. The guide applies to all d

User ID 150 Password - User ID 150 Password Mon- Cam-- Invalid Terminal Mode No User Terminal Mode No User Mon- Cam-- 2

Transcription:

8088/8086 MICROPROCESSOR PROGRAMMING CONTROL FLOW INSTRUCTIONS AND PROGRAM STRUCTURES 8088/8086 MICROPROCESSOR PROGRAMMING CONTROL FLOW INSTRUCTIONS AND PROGRAM STRUCTURES 61 Flag-Control 62 Compare 63 Control Flow and Jump 64 Subroutines and Subroutine-Handling 65 The Loop and the Loop-Handling 66 String and String-Handling 611 37100 微處理機原理與應用 Lecture 06-2

61 Flag-Control 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 61 Flag-Control Mnemonic Meaning Operation Flags affected LAHF SAHF CLC STC CMC CLI STI Load AH from flags Store AH into flags Clear carry flag Set carry flag Complement carry flag Clear interrupt flag Set interrupt flag (AH) (Flags) (Flags) (AH) (CF) 0 (CF) 1 (CF) NOT (CF) (IF) 0 (IF) 1 None SF,ZF,AF,PF,CF CF CF CF IF IF 7 0 AH SF ZF - AF - PF - CF 611 37100 微處理機原理與應用 Lecture 06-4 SF = Sign flag ZF = Zero flag AF = Auxiliary PF = Parity flag CF = Carry flag - = Undefined (do not use)

61 Flag-Control 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 MOV [MEM1], AH MOV AH, [MEM2] SAHF ; Load AH from flags ; Move content of AH to MEM1 ; Load AH from MEM2 ; Store content of AH into flags 611 37100 微處理機原理與應用 Lecture 06-5 61 Flag-Control 611 37100 微處理機原理與應用 Lecture 06-6

61 Flag-Control 611 37100 微處理機原理與應用 Lecture 06-7 61 Flag-Control 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

61 Flag-Control 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 61 Flag-Control 611 37100 微處理機原理與應用 Lecture 06-10

62 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 or resetting the flags CF, AF, OF, PF, SF, 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 林達德 62 Compare Instruction 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) = 1234 16 = 0001001000110100 2 (BX) = ABCD 16 = 1010101111001101 2 (AX) (BX) = 0001001000110100 2-1010101111001101 2 = 0110011001100111 2 Therefore, ZF = 0, SF = 0, OF = 0, PF = 0 CF = 1, AF = 1 611 37100 微處理機原理與應用 Lecture 06-12

62 Compare Instruction The Compare Instruction: 611 37100 微處理機原理與應用 Lecture 06-13 63 Control Flow and Jump 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

63 Control Flow and Jump Unconditional and conditional jump Part I JMP AA Unconditional jump instruction Part II Locations skipped due to jump AA XXXXXX Part III Next instruction executed Unconditional jump program sequence 611 37100 微處理機原理與應用 Lecture 06-15 63 Control Flow and Jump Unconditional and conditional jump Condition met? Yes No Part I Jcc AA XXXXXX Part II Conditional jump instruction Next instruction executed If condition not met Locations skipped due to jump AA XXXXXX Part III Next instruction executed if condition met Conditional jump program sequence 611 37100 微處理機原理與應用 Lecture 06-16

63 Control Flow and Jump Unconditional jump instruction Mnemonic Meaning Format Operation Flags affected JMP Unconditional jump JMP Operand Jump is initiated to the address specified by the operand None Operands Short-label Near-label Far-label Memptr16 Regptr16 Memptr32 Allowed operands for JMP instruction 611 37100 微處理機原理與應用 Lecture 06-17 63 Control Flow and Jump Verify the operation of the instruction JMP BX using the DEBUG program Let the contents of BX be 0010 16 Solution: 611 37100 微處理機原理與應用 Lecture 06-18

63 Control Flow and Jump Use the DEBUG program to observe the operation of the instruction JMP [BX] Solution: 611 37100 微處理機原理與應用 Lecture 06-19 63 Control Flow and Jump Conditional jump instruction Mnemonic Meaning Format Operation Flags affected Jcc Conditional jump 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 None 611 37100 微處理機原理與應用 Lecture 06-20

63 Control Flow and Jump 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 林達德 63 Control Flow and Jump Conditional jump instruction Mnemonic Meaning JNC Not carry JNE Not equal JNG Not greater JNGE Not greater nor equal JNL Not less JNLE Not less or nor equal JNO Not overflow JNP Not parity JNS Not sign JNZ Not zero JO Overflow JP Parity JPE Parity even JPO Parity odd JS Sign JZ Zero 611 37100 微處理機原理與應用 Lecture 06-22 Condition CF=0 ZF=0 ((SF xor OF) or ZF)=1 (SF xor OF)=1 SF=OF ZF=0 and SF=OF OF=0 PF=0 SF=0 ZF=0 OF=1 PF=1 PF=1 PF=0 SF=1 ZF=1

63 Control Flow and Jump 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 63 Control Flow and Jump 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

63 Control Flow and Jump 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 63 Control Flow and Jump 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

63 Control Flow and Jump 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 63 Control Flow and Jump Loop program structures WHILE-DO Start Initialize repeat count AGAIN Loop done? NO Loop program statements YES Decrement repeat count Repeat NEXT 611 37100 微處理機原理與應用 Lecture 06-28 WHILE-DO program sequence

63 Control Flow and Jump 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 林達德 63 Control Flow and Jump Example The block-move program Start Establish the data segment, source block and destination block Set up a counter for the points to be removed Initialization NO NXTPT Move the next source point to the accumulator Move the accumulator to the next destination point Data movement Update counter, source pointer, and destination pointer Update NO All points Moved? YES Stop 611 37100 微處理機原理與應用 Lecture 06-30 Test

63 Control Flow and Jump Example The block-move program NXTPT: MOV AX, DATASEGADDR MOV DS, AX MOV SI, BLK1ADDR MOV DI, BLK2ADDR MOV CX, N MOV AH, [SI] MOV [DI], AH INC SI INC DI DEC CX JNZ NXTPT HLT 611 37100 微處理機原理與應用 Lecture 06-31 63 Control Flow and Jump 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

64 Subroutines and Subroutine- Handling 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 64 Subroutines and Subroutine- Handling Main program 611 37100 微處理機原理與應用 Lecture 06-34 Call subroutine A Next instruction Call subroutine A Next instruction Subroutine concept Subroutine A First instruction Return

64 Subroutines and Subroutine- Handling The CALL instruction Mnemonic CALL Meaning Subroutine call Format CALL Operand Operands Near-proc Far-proc Memptr16 Regptr16 Memptr32 Operation Execution 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 Flags affected None Allowed operands for CALL instruction 611 37100 微處理機原理與應用 Lecture 06-35 64 Subroutines and Subroutine- Handling The RET instruction Mnemonic Meaning Format Operation Flags affected RET Return RET or RET Operand Return to the main program by restoring IP (and CS for farproc) If Operand is present, it is added to the contents of SP None Operands None Disp16 Allowed operands for RET instruction 611 37100 微處理機原理與應用 Lecture 06-36

64 Subroutines and Subroutine- Handling TITLE 610 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 610 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 64 Subroutines and Subroutine- Handling 611 37100 微處理機原理與應用 Lecture 06-38

64 Subroutines and Subroutine- Handling 611 37100 微處理機原理與應用 Lecture 06-39 64 Subroutines and Subroutine- Handling 611 37100 微處理機原理與應用 Lecture 06-40

64 Subroutines and Subroutine- Handling 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 POP ZZ POP YY POP XX Return to main program RET 611 37100 微處理機原理與應用 Lecture 06-41 Structure of a subroutine 64 Subroutines and Subroutine- Handling The PUSH and POP instructions Mnemonic Meaning Format Operation Flags affected PUSH Push word onto stack PUSH S ((SP)) (S) (SP) (SP)-2 None POP Pop word off stack POP D (D) ((SP)) (SP) (SP)+2 None Operands (S or D) Register Seg-reg (CS illegal) Memory Allowed operands for PUSH and POP instruction 611 37100 微處理機原理與應用 Lecture 06-42

64 Subroutines and Subroutine- Handling 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 64 Subroutines and Subroutine- Handling The PUSHF and POPF instructions Mnemonic Meaning Operation Flags affected PUSHF Push flag onto stack ((SP)) (Flags) (SP) (SP)-2 None POPF Pop word off stack (Flags) ((SP)) (SP) (SP)+2 OF,DF,IF,TF,SF,ZF, AF,PF,CF 611 37100 微處理機原理與應用 Lecture 06-44

65 The Loop and the Loop-Handling The LOOP instructions Mnemonic LOOP LOOPE LOOPZ LOOPNE LOOPNZ Meaning Loop Loop while equal Loop while zero Loop while not equal Loop while not zero Format LOOP Short-label LOOPE/LOOPZ Short-label LOOPNE/LOOPNZ Short-label Operation (CX) (CX)-1 Jump is initiated to location defined by shortlabel if (CX) 0; otherwise, execute next sequential instruction (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 611 37100 微處理機原理與應用 Lecture 06-45 65 The Loop and the Loop-Handling 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

65 The Loop and the Loop-Handling Example The block-move program NXTPT: MOV AX, DATASEGADDR MOV DS, AX MOV SI, BLK1ADDR MOV DI, BLK2ADDR MOV CX, N MOV AH, [SI] MOV [DI], AH INC SI INC DI LOOP NXTPT HLT 611 37100 微處理機原理與應用 Lecture 06-47 65 The Loop and the Loop-Handling 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

65 The Loop and the Loop-Handling 611 37100 微處理機原理與應用 Lecture 06-49 65 The Loop and the Loop-Handling 611 37100 微處理機原理與應用 Lecture 06-50

66 String and String-Handling The basic string instructions Mnemonic MOVS CMPS SCAS LODS STOS Meaning Move string Compare string Scan string Load string Store string Format MOVSB MOVSW CMPSB CMPSW SCASB SCASW LODSB LODSW STOSB STOSW Operation ((ES)0+(DI)) ((DS)0+(SI)) (SI) (SI)±1 or 2 (DI) (DI)±1 or 2 Set flags as per ((DS)0+(SI))-((ES)0+(DI)) (SI) (SI)±1 or 2 (DI) (DI)±1 or 2 Set flags as per (AL or AX)-((ES)0+(DI)) (DI) (DI)±1 or 2 (AL or AX)-((DS)0+(SI)) (SI) (SI)±1 or 2 ((ES)0+(DI)) (AL or AX) ±1 or 2 (DI) (DI)±1 or 2 Flags affected None CF,PF,AF, ZF,SF,OF CF,PF,AF, ZF,SF,OF None None 611 37100 微處理機原理與應用 Lecture 06-51 66 String and String-Handling Move string MOVSB, MOVSW Example The block-move program using the move-string instruction NXTPT: MOV AX, DATASEGADDR MOV DS, AX MOV ES, AX MOV SI, BLK1ADDR MOV DI, BLK2ADDR MOV CX, N CLD MOVSB LOOP NXTPT HLT 611 37100 微處理機原理與應用 Lecture 06-52

66 String and String-Handling Compare string and scan string CMPSB/CMPSW, SCASB/SCASW Example Block scan operation using the SCASB instruction AGAIN: NEXT: MOV AX, DATASEGADDR MOV DS, AX MOV ES, AX MOV AL, 05 MOV DI, 0A000H MOV CX, 0FH CLD SCASB LOOPNE AGAIN 611 37100 微處理機原理與應用 Lecture 06-53 66 String and String-Handling Load and store string LODSB/LODSW, STOSB/STOSW Example Initializing a block of memory with a store string instruction AGAIN: MOV AX, 0 MOV DS, AX MOV ES, AX MOV AL, 05 MOV DI, 0A000H MOV CX, 0FH CLD STOSB LOOP AGAIN 611 37100 微處理機原理與應用 Lecture 06-54

66 String and String-Handling REP string REP (repeat prefixes) Prefix REP REPE/REPZ REPNE/REPNZ Used with: MOVS STOS CMPS SCAS CMPS SCAS Meaning Repeat while not end of string CX 0 Repeat while not end of string and strings are equal CX 0 and ZF=1 Repeat while not end of string and strings are not equal CX 0 and ZF=0 611 37100 微處理機原理與應用 Lecture 06-55 66 String and String-Handling 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

66 String and String-Handling Autoindexing for string instruction CLD and STD instructions Mnemonic Meaning Format Operation Flags affected CLD STD Clear DF Set DF CLD STD (DF) 0 (DF) 1 DF DF 611 37100 微處理機原理與應用 Lecture 06-57 66 String and String-Handling 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

66 String and String-Handling 611 37100 微處理機原理與應用 Lecture 06-59 66 String and String-Handling 611 37100 微處理機原理與應用 Lecture 06-60