# 微算機與組合語言 Microprocessor & Assembly language
# Chapter 2
## Internal architecture
### BIU (Bus Interface Unit)
* Performing external bus operations
* e.g. Fetch Instruction and data
### EU (Execution Unit)
* perform operations

### BIU prefetch instructions into instruction queue
### Register
* segment
* pointer
* data
### ALU (Arithmetic logic unit)
### Flags
* status
* control

## Register
32 bits: 80286 above
### Instruction Pointer
### Segment Register
* CS (Code Segment) + IP (Instruction Pointer)
* DS (Data Segment) + SI (Source Index)
* ES (Extra Segment) + DI (Destination Index)
* SS (Stack Segment) + SP (Stack Pointer)/BP (Base Pointer)
### Data Register
* AX (Accumulator)
* BX (Base)
* CX (Counter)
* DX (Data)
### Pointer & Index
* BP (Base Pointer)
* SP (Stack Pointer)
* SI (Source Index)
* DI (Destination Index)
### Status Register
#### Status flags
##### CF (Carry Flag):
* is set if there is a carry-out or a borrow-in for the most significant bit of the result
* 最高位借進位時,CF=1
##### PF (Parity Flag):
* is set if the result produced by the instruction has even parity.
* If the parity is odd, PF reset
* 結果有偶數個位元=1時,PF=1
##### AF (Auxiliary Flag):
* Is set if there is a carry-out from the low nibble into the high nibble. Otherwise AF is reset.
* 將暫存器長度切為一半 (nibble),若低 nibble 有進位至高 nibble,AF=1
* 比如 16-bit register,從第7位進位到第8位時(最低位為第0位),AF=1
##### ZF (Zero Flag):
* Is set if the result produced by an instruction is zero. Otherwise, ZF is reset
* 結果是0,ZF=1
##### SF (Signed Flag):
* The MSB (Most Significant Bit) of the result is copied into SF.
* Is set if the result is a negative, reset if it is positive
* 結果最高位(負數)=1,SF=1
##### OF (Overload Flag):
* Is set if the signed result out of range, otherwise remains reset
* 溢位時,OF=1
#### Control flags
##### TF (Trap Flag):
* If TF is set, 8088 goes into the single-step mode.
* Very useful for debugging programs
* 開下去會逐步執行,所以常拿來除錯
##### DF (Direction Flag):
* Determine the direction in which string operations will occur. When set, the string instruction automatically decrements the address;
* if reset the string address will be incremented
* 如果 DF=1,將由高位元讀到低位元
##### IF (Interrupt Flag):
* For the 8088 to recognize maskable interrupt requests at its interrupt (INT) input, the IF must be set
* When IF is reset, requests at INT are ignored and the maskable interrupt interface is disabled.
* Maskable interrupt v.s. Non-maskable interrupt (highest priority of interrupt)
# Chapter 3
## Native language is machine language
### A program written in language is often called machine code
* encoded using 0, 1
### An instruction can be divided into two parts (Operation code)
* operation code (opcode)
* operands
### Each opcode is assigned a unique letter combination called a menmonic
* MOV AX, BX
* move bx register to ax register
## Advantages of assembly program than high-level program
### Small code size
* Useful for limited memory space
### Short execution time
* Useful for real-time app. or time-sensitive app.
### Low level control
* Device service routines
## Addressing mode
Access different type of operands, the 8088 is provided with various addressing modes
### Specify an operand
* register addressing mode
* immediate addressing mode
* memory addressing mode (direct, register indirect, based, indexed, based-indexed)
### Register operand addressing mode
* 所指定的就是 register 裡面的內容
* MOV AX, BX
### Immediate operand addressing mode
* 後面那個就是值,帶進去前面的暫存器就好
* MOV AX, 12h
### Memory operand addressing mode
Physical address (PA) is computed from a segment base address (SBA) and effective address (EA)
PA = SBA : EA
PA = Segment base : Base + Index + Displacement
Five memory operand addressing modes
* Displacement: direct addressing mode
* BX, BP, SI, DI: register indirect
* BX or BP + displacement: based
* SI or DI + displacement: Indexed
* BX, BP + SI, DI + displacement: Based-indexed
#### Direct addressing mode
* MOV CX, [1234h]
* Physical address(PA) = {CS, DS, SS, ES} * 16 + {Direct address}
#### Register indirect addressing mode
* MOV AX, [SI]
* PA = {CS, DS, SS, ES} * 16 + {BX, BP, SI, DI}
#### Based addressing mode
* MOV [BX]+1234H, AL
* PA = {CS, DS, SS, ES} * 16 + {BX, BP} + {8/16-bit displacement}
#### Indexed addressing mode
* MOV AL, [SI]+1234H
* PA = {CS, DS, SS, ES} * 16 + {SI, DI} + {8/16-bit displacement}
#### Based-Indexed addressing mode
* MOV AH, [BX][SI]+1234H
* PA = {CS, DS, SS, ES} * 16 + {BX, BP} + {SI, DI} + {8/16-bit displacement}
# Chapter5
D:Destination 目的地
S:Source 來源
Q:Quotient 商
R:Remainder 餘數
## Data transfer instructions
### MOV
- 賦值
- MOV D, S
- S -> D
```
MOV AX, 1234H
```
### XCHG
- 交換
- XCHG D, S
- D <-> S
```
MOV AX, 1234H
MOV BX, 4567H
XCHG AX, BX
```
### XLAT
- 查表轉換
- AL+BX+DX -> AL
- 會查 AL+BX+DX 然後放到 AL
```
MOV BX, 1000H
MOV DX, 1000H
MOV [DX+BX+0BH], 42H
MOV AL, 0BH
XLAT
;AL = 42H
```
### LEA
- Load register with effective address
- LEA Reg16, EA
- EA -> Reg16
- 跟 C++ 取址符號 & 做的事情一樣
```
MOV BX, 100H
MOV BX+100H, 1234H
MOV AX, BX+100H
;AX = 1234H
LEA AX, BX+100H
;AX = 200H
MOV AX, [200H]
;AX = 1234H
```
### LDS
- Load register and data segment register
- LDS Reg16, EA
- EA -> Reg16
- EA + 2 -> DS
- MOV + 更改DS
```
MOV BX, 100H
MOV BX+100H, 1234H
MOV BX+102H, 5678H
LDS AX, BX+100H
;AX = 1234H, DS = 5678H
```
### LES
- Load register and extra segment register
- LES Reg16, EA
- EA -> Reg16
- EA + 2 -> ES
- MOV + 更改ES
```
MOV BX, 100H
MOV BX+100H, 1234H
MOV BX+102H, 5678H
LES AX, BX+100H
;AX = 1234H, ES = 5678H
```
## Arithmetic instructions
### Addition instruction
#### ADD
* addition
* ADD D, S
* S + D -> D
* Carry -> CF
* 加完後的值放在靠近 ADD 的register
```
MOV AX, 1234H
MOV BX, 4567H
ADD AX, BX
```
#### ADC
* add with carry flag
* ADC D, S
* S + D + CF -> D
* Carry -> CF
```
MOV DL, 0
MOV AL, 0FFH
ADD AL, 0FFH
ADC DL, 0
;DL = 1H
```
#### INC
* 加一
* INC D
* D + 1 -> D
```
MOV AX, 1H
INC AX
```
#### AAA

- ASCII adjust for addition
- 加完後變成BCD
```
MOV AL, '8'
ADD AL, '9'
AAA
;AX = 0107H
```
#### DAA
- decimal adjust for addition
- 如果 AL 的低 4 位大於 9 或 AF=1,則 AL+06H,並將 AF 置 1;
如果 AL 的高 4 位大於 9 或 CF=1,則 AL+60H,且將 CF 置 1。
如果兩個都不滿足,則將AF,CF清零。
- 16進位加法變成10進位
```
MOV AL, 37H
MOV BL, 35H
ADD AL, BL
DAA
;AL = 72H
```
### Subtraction instructions
#### SUB
* subtract
* 減完後的值放在靠近 ADD 的register
* SUB D, S
* D - S -> D
* Borrow -> CF
```
MOV AX, 4567H
MOV BX, 1234H
SUB AX, BX
;AX = 3333H
```
#### SBB
* subtract with borrow
* SBB D, S
* D - S - CF -> D
```
MOV AX, 43H
MOV BX, 49H
SUB AX, BX
;AX = FFFAH, CF=1
SBB AX, 9H
;AX = FFF0H
```
#### DEC
* 減一
* DEC D
* D - 1 -> D
```
MOV AX, 43H
DEC AX
```
#### AAS
- ASCII adjust for subtraction
- 把BCD拿來減
```
MOV AX, 0103H
MOV BX, 4H
SUB AL, BL
;AL = FFH
AAS
;AX = 0009H
```
#### DAS
- decimal adjust for subtraction
- 如果 AL 的低四位大於 9 或 AF=1,則 AL-06H,並將 AF 置 1;
如果 AL 的高四位大於 9 或 CF=1,則 AL-60H,並將 CF 置 1;
如果兩個都不滿足,則將 AF , CF 清零。
- 16進位減法變成10進位
```
MOV AX, 32H
MOV BX, 25H
SUB AL, BL
;AL = 0DH
DAS
;AL = 07H
```
#### NEG
* 取二補數
* NEG D
* 0 - D -> D
* 1 -> CF
```
MOV AX, 003AH
NEG AX
;AX = FFC6H
```
### Multiplication and Division instrucion
#### MUL
- Multiply ( unsigned )
- MUL S
- AL * S(8bits) -> AX
- AX * S(16bits) -> DX,AX
```
MOV AX, 1234H
MOV BX, 1234H
MUL BX
;DX,AX = 014BH,5A90H
```
#### DIV
- Division ( unsigned )
- DIV S
- AX / S(8bits)
- Q -> AL
- R -> AH
- DX:AX / S(16bits)
- Q -> AX
- R -> DX
```
MOV AX, 1234H
MOV BX, 3H
DIV BX
;DX:AX = 0001H:0611H
```
#### IMUL
- Integer multiply ( signed )
- IMUL S
- AL * S(8bits) -> AX
- AX * S(16bits) -> DX,AX
## Logic instructions
### AND
- Logical AND
- AND D, S
### OR
- Logical OR
- OR D, S
### XOR
- Logical XOR
- XOR D, S
### NOT
- Logical NOT
- NOT D
## Shift instructions
### SAR/SAL
- Shift arithmetic left/right
- SAR/SAL D, Count
- 最高位bit不動,其他的往左/右移 Count bits
- 會將從L(SAL)/R(SAR)數過來第 Count 的 Bit 放到 CF
- SAR的空出的bits會補上最高位的bit,SAL會補0
```
MOV AX, 891AH
MOV CL, 2H
SAR AX, CL
;AX = E246, CF = 1
```
### SHL/SHR
- Shift logical left/right
- SHR/SHL D, Count
- 往左/右移 Count bits
- 會將從L(SHL)/R(SHR)數過來第 Count 的 Bit 放到 CF
- SHR/SHL空出來的bits會補0
```
MOV AX, 091AH
MOV CL, 2H
SHR AX, CL
;AX = 0246, CF = 1
```
## Rotate instructions
### ROL/ROR
- 與SHL/SHR差別於超出去的bits會補到另一端
### RCL/RCR
- 與ROL/ROR差別於移位方向的前端多一格CF
###### tags:`CSnote`