--- tags : 微算機與組合語言 --- ###### 國立臺北大學資工系江宥旻 8088/8086 programming - control flow instructions and program structures === Introducion --- This chapter will cover following instructions + Flag-Control instructions + Compare instructions + Control Flow and the Jump instructions + Subroutines and subroutine-handling instructions + Loop and Loop-Handling instructions + String and string-Handling instructions Flag-control instructions --- ![](https://i.imgur.com/9lwqgWe.png =500x) ![](https://i.imgur.com/1gfeXTh.png =300x) > LAHF和SAHF在OS很重要 Example Save flag to MEM1 and reload the flags with the contents of MEM2 ```asm LAHF MOV [MEM1], AH MOV AH, [MEM2] SAHF ``` :::info OS執行兩個程式(程式A和程式B) A執行1ms後,要換B時 $\rightarrow$把A存在memory裡,換B執行 ::: ![](https://i.imgur.com/5MWiFcP.png =400x) > SR:Status Register Compare instrution --- CMP + Determine the relationship between two numbers + No operands are affected excepts for flags + 只會改變Flag不會更改到操作數 + Basically a subtraction operation ![](https://i.imgur.com/guzz0Cp.png =400x) Example Please show that all of flags ```asm MOV AX, 1234H MOV BX, 0ABCDH CMP AX, BX ``` AX = 0001001000110100 BX = 1010101111001101 CMP = 0110011001100111 (CMP是把AX減去BX來看AX和BX的大小關係,不會存在任何地方) ![](https://i.imgur.com/EpHqK41.png =400x) ![](https://i.imgur.com/cEUJ2D1.png =500x) Control flow and the JUMP instructions --- Jump instrutions + Unconditions jump + Jmp instruction + ![](https://i.imgur.com/uTb3BNX.png =300x) + Condtion jump + Jxx instruction (有很多,用xx代替) + The condition refers to specific status flag + ![](https://i.imgur.com/qFijSrx.png =300x) ![](https://i.imgur.com/5IA29H6.png =400x) 1. When the jump instruction is executed, **IP is reload** with a new value + IP+2 plus the signed displacement 2. Intrasegment jump(同區段跳躍) v.s. intersegment jump(跳區段跳躍) + ![](https://i.imgur.com/7JMi7xB.jpg =100x) + Short-label, near-label, Memptr 16 or register 16 operand + 若無法被64K涵蓋就需要跳區段 + ```asm JMP 1234H JMP BX JMP [BX] ``` 3. Condition jump instruction ![](https://i.imgur.com/VcyhaCY.jpg =500x) 4. Condition jump + JC Lable `offset is specified` + JB Lable ![](https://i.imgur.com/tBYh8NY.png) 5. Branch program structure ```asm IF xxx THEN then statement ELSE else statement ``` ![](https://i.imgur.com/JhP5fOG.png =400x) ![](https://i.imgur.com/n14BaHz.png =300x) ![](https://i.imgur.com/IkkMzW6.jpg =300x) The loop program structure - Repeat-until and While-Do --- ```asm REPEAT Statement 1; Statement 2; ... Statement N; UNTIL <condition> ``` ![](https://i.imgur.com/0cDXbvs.png =400x) ![](https://i.imgur.com/VCioi6L.png =200x)![](https://i.imgur.com/pEP0DjN.jpg =300x) ```asm While <expression> Statement 1; Statement 2; ... Statement N; ``` ![](https://i.imgur.com/eYHwbJZ.png =400x)![](https://i.imgur.com/iadKZD4.png =200x) Applications + Block-move program (區塊移動) ![](https://i.imgur.com/M0XrOkR.png =300x)![](https://i.imgur.com/zSgL9Hs.png =200x) :::success ADDR:address ```asm mov si, BLK1ADDR mov di, BLK2ADDR ``` BLK1到BLK2移動了n DS * 16 + SI $\rightarrow$ DS * 16 + DI ::: ![](https://i.imgur.com/Gox1u89.png =200x)一個一個Byte移動 Subroutines and subroutine-handling instructions --- 1. Subroutine + Procedure 2. Call subroutine 1. IP or CS and IP must be modified 2. Call instruction + Go to subroutine 3. Ret instruction + Return from subroutine ![](https://i.imgur.com/0NscN1f.jpg =300x)![](https://i.imgur.com/RwGtA6k.png =300x) 1. Intrasegment call (in same segment) + modify IP only 2. Intersegment call (cross over different segment) + Modify IP and CS 3. RET + return from subroutine Example ```asm call 1234h ;Near-proc -> modify IP call bx ;Near-proc -> modify IP call [bx] ;Near-proc -> modify IP call DWROD PTR [di] ;Far-proc -> modify IP and CS ``` ![](https://i.imgur.com/6A0eViB.png =400x) **Push and Pop instruction** + Compiler自己做的 + 防止主程式call到副程式時,更改到Reg的value + 副程式會用到的Reg,主程式會先存原本的值,避免被更改到 ![](https://i.imgur.com/IeMKhVX.png =200x) ![](https://i.imgur.com/wjaeZpa.png =400x) --- ```asm ;Subroutine: SQUARE ;Description: BX = squre of BL SQUARE PORC NEAR PUSH AX ;Save the register to be used MOV AL, BL ;Placce the number in AL IMUL BL ;Multiply with itself MOV BX, AX ;Svae the result POP AX ;Restore the register used RET SQUARE ENDP ``` + Push ax 1. sp - 1 <- ah 2. sp - 2 <- al 3. sp <- sp - 2 + Pop ax 1. al <- sp 2. ah <- sp + 1 3. sp <- sp + 2 --- 針對Flag的Push and Pop + PUSHF + Push flag register on the top of the stack + POPF + Pop flag register from the top of the stack ![](https://i.imgur.com/bMGqjBx.png =500x) LOOPS and LOOP-handling instructions --- ![](https://i.imgur.com/T5ANr3J.png) :::success CX:Count Register LOOP $\rightarrow$ CX != 0 LOOPE/LOOPZ $\rightarrow$ CX != 0 && ZF = 1 LOOPNE/LOOPNE $\rightarrow$ CX != 0 && ZF = 0 ::: Example (比較Block-move prog) ![](https://i.imgur.com/MOBgWCc.png =400x) > 這裡用的是LOOP,所以沒有DEC CX,因為LOOP會自己幫CX減一。 Example ![](https://i.imgur.com/1mNVn34.png =200x) Strings and String-handling instructions --- **Basic string insruction** ![](https://i.imgur.com/piAhLK5.png) ![](https://i.imgur.com/kFOaDyy.jpg =300x) + block move program using the move-string instruction ![](https://i.imgur.com/rGlxzNd.png =300x) > 一二行就是得這樣寫 + block scan operation using the SCASB instruction ![](https://i.imgur.com/I79FDX3.png =300x) > 字元比對:在一個區塊找有沒有AL的值 + Initialing a block of memory with a store string operation ![](https://i.imgur.com/te8JGmt.png =300x) > String的每一個Byte都設為05 + REP – repeat instruction ![](https://i.imgur.com/w6a9LaC.png =300x) + Initialing a block of memory by repeating the STOSB instruction ```asm MOV AX, 0 MOV DS, AX MOV ES, AX MOV AL, 05 MOV DI, 0A000H MOV CX, 0FH CLD REP STOSB ;重複CX次 ``` REP STOSB 1. AGAIN STOSB 2. LOOP AGAIN ## **程式可以去參考emu86的sample code** 參考資料 + Barry B. Bery, “The Intel Microprocessors,” 8th Edition, 2009, Prentice Hall. + Walter A. Triebel, Avtar Singh, “The 8088 and 8086 Microprocessors – Programming, Interfacing, Software, Hardware, and Applications,” 4th Edition, 2003, Prentice Hall. + 國立臺北大學資工系張玉山教授ppt