# 微算機與組合語言 Microprocessor & Assembly language
# Chapter 1
## Computers Evolution
- Mainframe-> minicomputers-> workstation-> Microcomputers -> mobile devices
- Microcomputers
- Apple-> Apple II-> IBM PC/XT-> IBM PC/AT-> IBM Compatible PC(PC)-> notebook & tablet PC -> PDA & cell phone
## CPU Evolution
- Microprocessors (Intel)
- 4004 ->8008-> 8080-> 8085-> 8086-> 8088-> 80286-> 80386-> 80486-> Pentium-> Pentium Pro-> Pentium II-> Pentium III-> Pentium IV
- 4bits-> 8bits-> 16bits-> 32bits-> 64bits
- Single core
- Due Core (2)
- Quad Core (4)
- Hexa Core (6)
- Octa Core (8)
- Deca Core (10)
- Dodeca Core (12)
- Hexadeca Core (16)




## Computers Evolution
- Classification of microprocessors
- Reprogrammable microprocessor
- For general purpose microcomputer
- Embedded microprocessor
- For special purpose microcomputer
- In general, implemented as a single chip
- 8051, ARM

## General Architecture
- Microcomputer & microprocessor
- IC (integrated circuit)
- SSI, MSI, LSI, VLSI, ULSI, ...


- 8086/8088 Family
- Upward compatible
- 80286 can execute 8088/8086 code
- Memory management, protection, and multitasking



# 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`
{"metaMigratedAt":"2023-06-15T01:21:55.985Z","metaMigratedFrom":"Content","title":"微算機與組合語言 Microprocessor & Assembly language","breaks":true,"description":"Performing external bus operations","contributors":"[{\"id\":\"6433d5ea-912d-453d-b2a4-610be7d6aedd\",\"add\":139,\"del\":8},{\"id\":\"3b5d6e35-351e-4b36-b6ec-81392694c5a4\",\"add\":67,\"del\":57},{\"id\":\"b5ca89f9-80a9-4943-b967-6c1a94af3a0f\",\"add\":4202,\"del\":496},{\"id\":\"86dca64a-1bc3-4446-b4de-a815d8a02c61\",\"add\":7115,\"del\":96},{\"id\":\"907c04fb-f955-47c5-b98b-db093166a44a\",\"add\":1524,\"del\":0}]"}
微算機與組合語言 Microprocessor & Assembly language
Chapter 1
Computers Evolution
CPU Evolution
Computers Evolution
General Architecture
Chapter 2
Internal architecture
BIU (Bus Interface Unit)
EU (Execution Unit)
BIU prefetch instructions into instruction queue
Register
ALU (Arithmetic logic unit)
Flags
Register
Instruction Pointer
Segment Register
Data Register
Pointer & Index
Status Register
Status flags
CF (Carry Flag):
PF (Parity Flag):
AF (Auxiliary Flag):
ZF (Zero Flag):
SF (Signed Flag):
OF (Overload Flag):
Control flags
TF (Trap Flag):
DF (Direction Flag):
IF (Interrupt Flag):
Chapter 3
Native language is machine language
A program written in language is often called machine code
An instruction can be divided into two parts (Operation code)
Each opcode is assigned a unique letter combination called a menmonic
Advantages of assembly program than high-level program
Small code size
Short execution time
Low level control
Addressing mode
Specify an operand
Register operand addressing mode
Immediate operand addressing mode
Memory operand addressing mode
Direct addressing mode
Register indirect addressing mode
Based addressing mode
Indexed addressing mode
Based-Indexed addressing mode
Chapter5
Data transfer instructions
MOV
XCHG
XLAT
LEA
LDS
LES
Arithmetic instructions
Addition instruction
ADD
ADC
INC
AAA
DAA
Subtraction instructions
SUB
SBB
DEC
AAS
DAS
NEG
Multiplication and Division instrucion
MUL
DIV
AX / S(8bits)
DX:AX / S(16bits)
IMUL
Logic instructions
AND
OR
XOR
NOT
Shift instructions
SAR/SAL
SHL/SHR
Rotate instructions
ROL/ROR
RCL/RCR
tags:CSnote