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


> 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執行
:::

> SR:Status Register
Compare instrution
---
CMP
+ Determine the relationship between two numbers
+ No operands are affected excepts for flags
+ 只會改變Flag不會更改到操作數
+ Basically a subtraction operation

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的大小關係,不會存在任何地方)


Control flow and the JUMP instructions
---
Jump instrutions
+ Unconditions jump
+ Jmp instruction
+ 
+ Condtion jump
+ Jxx instruction (有很多,用xx代替)
+ The condition refers to specific status flag
+ 

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(跳區段跳躍)
+ 
+ Short-label, near-label, Memptr 16 or register 16 operand
+ 若無法被64K涵蓋就需要跳區段
+ ```asm
JMP 1234H
JMP BX
JMP [BX]
```
3. Condition jump instruction

4. Condition jump
+ JC Lable `offset is specified`
+ JB Lable

5. Branch program structure
```asm
IF xxx THEN
then statement
ELSE
else statement
```



The loop program structure - Repeat-until and While-Do
---
```asm
REPEAT
Statement 1;
Statement 2;
...
Statement N;
UNTIL <condition>
```


```asm
While <expression>
Statement 1;
Statement 2;
...
Statement N;
```

Applications
+ Block-move program (區塊移動)

:::success
ADDR:address
```asm
mov si, BLK1ADDR
mov di, BLK2ADDR
```
BLK1到BLK2移動了n
DS * 16 + SI $\rightarrow$ DS * 16 + DI
:::
一個一個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

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

**Push and Pop instruction**
+ Compiler自己做的
+ 防止主程式call到副程式時,更改到Reg的value
+ 副程式會用到的Reg,主程式會先存原本的值,避免被更改到
 
---
```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

LOOPS and LOOP-handling instructions
---

:::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)

> 這裡用的是LOOP,所以沒有DEC CX,因為LOOP會自己幫CX減一。
Example

Strings and String-handling instructions
---
**Basic string insruction**


+ block move program using the move-string instruction

> 一二行就是得這樣寫
+ block scan operation using the SCASB instruction

> 字元比對:在一個區塊找有沒有AL的值
+ Initialing a block of memory with a store string operation

> String的每一個Byte都設為05
+ REP – repeat instruction

+ 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