# 組合語言 Assembly Language
:::info
:::spoiler Click to Open TOC
[TOC]
:::
---
## Chapter 1 Basic Concepts
### 【**Binary Numbers**】
- MSB - Most Significant Bit
- LSB - Least Significant Bit

### 【**Singed Integer**】:label:
> The highest bit indicates the sign.
> `1 = negative, 0 = positive`
If the **highest** digit of a hexadecimal integer is > 7,
the value is negative. `Examples: 8A, C5, A2, 9D`

#### 【Ranges of Signed Integers】

### 【**Forming the Two's Complement**】
<font color="#FFAF60">【Hint】</font>
**因為相加 = 0 即是互補**
**全部相反再加一**

#### 【Binary Subtraction】
##### **When subtracting A – B**
`1. Convert B to its two's complement`
`2. Add A to (–B)`
### 【**Boolean Operations**】
#### 【Boolean Algebra】

#### 【Truth Tables】
`Example:`

---
## Chapter 3 Assembly Language Fundamentals
### 【**Intrinsic Data Types**】
- BYTE, SBYTE <font color="red">8-bit</font>
- WORD, SWORD <font color="red">16-bit</font>
- DWORD, SDWORD <font color="red">32-bit</font>
- QWORD <font color="red">64-bit</font>
- TWORD <font color="red">80-bit</font>

#### 【**Defining BYTE and SBYTE Data**】:label:
```javascript=
value1 BYTE 'A' ; character constant
value3 BYTE 255 ; largest unsigned byte
value4 SBYTE -128 ; smallest signed byte
value6 BYTE ? ; uninitialized byte
```
#### 【**DUP Operator**】
```javascript=
var1 BYTE 20 DUP(0) ; 20 bytes, all equal to zero
var2 BYTE 20 DUP(?) ; 20 bytes, uninitialized
var3 BYTE 4 DUP("STACK") ; 20 bytes: "STACKSTACKSTACKSTACK"
```
### 【**Coding Style**】
:::spoiler code
```javascript=
TITLE Add and Subtract, Version 2 (AddSub2r.asm)
INCLUDE Irvine32.inc ; 宣告切齊不空格
.data
val1 DWORD 10000h ; 變數要 Meaningful(廢話)
val2 DWORD 40000h ; 保留字才大寫
.code ; 以上切齊左側
Main PROC ; Main 後才 4 Spaces
.
L1: mov eax,val1 ; Label 切齊不空格
add eax,val2 ; 指令後 1 Space
sub eax,val3
mov finalVal,eax
.
jmp L1
.
Main ENDP
END main
```
:::
### 【**Equal-Sign Directive**】
- name = expression
- expression is a <font color="red">32-bit</font> integer(expression or constant)
- name is called a <font color="red">symbolic constant</font>
:::spoiler code
```javascript=
COUNT = 500
.
mov ax,COUNT
```
:::
### 【**Calculating the Size of a Array**】:label:
- current location counter: `$`
- subtract address of list
- difference is the number of bytes
```javascript=
list WORD 1000h,2000h,3000h,4000h
ListSize = ($ - list) / 2 ; 除 2 的原因是 WORD's SIZE = 2*BYTE
```

---
## Chapter 4 Data Transfers, Addressing, and Arithmetic
### 【**MOV Instruction**】
#### 【**MOV**】
- 等價搬移 `8->8` `16->16`
- 至少其中一個是 Register ~~`mov val1,val2`~~
- 重要(特定)佔存器也不能放
<font color="red">**`MOV不影響Flag`**</font>
#### 【**MOVZX - Zero Extension**】
:::spoiler code
```javascript=
mov bl,10001111b
movzx ax, bl ; zero-extension
```
:::

#### 【**MOVSX - Sign Extension**】
:::spoiler code
```javascript=
mov bl,10001111b
movsx ax, bl ; sign-extension
```
:::
**補齊的數字依照 ==Sign Bit== 決定**

### 【**XCHG**】
<!-- **這個會考嗎????** -->
<!-- 我只是把他順手記下來而已 -->
- 交換 Source, Destination 的 values
- 至少其一是 Register
:::spoiler code
```javascript=
.data
var1 WORD 1000h
var2 WORD 2000h
.code
xchg ax,bx ; exchange 16-bit regs
xchg ah,al ; exchange 8-bit regs
xchg var1,bx ; exchange mem, reg
xchg eax,ebx ; exchange 32-bit regs
xchg var1,var2 ; error: two memory operands
```
:::
### 【**Direct-Offset Operands**】:label:
```javascript=
.data
arrayW WORD 1000h,2000h,3000h
arrayD DWORD 1,2,3,4
.code
mov ax,[arrayW+2] ; AX = 2000h
mov ax,[arrayW+4] ; AX = 3000h
mov eax,[arrayD+4] ; EAX = 00000002h
```
**+ 2 是指記憶體指標往後兩個BYTE,因為 `WORD = 2*BYTE`,DWORD同理**

### 【**INC & DEC**】
**可以是 <font color="red">Register</font> or <font color="red">Memory</font>**
:::spoiler code
```javascript=
.data
myWord WORD 1000h
myDword DWORD 10000000h
.code
inc myWord ; 1001h
dec myWord ; 1000h
inc myDword ; 10000001h
mov ax,00FFh
inc ax ; AX = 0100h
mov ax,00FFh
inc al ; AX = 0000h
```
:::
### 【**ADD & SUB**】
[**與 <font color="red">MOV</font> 的三個規則相同**](#【**MOV**】)
:::spoiler code
```javascript=
.data
var1 DWORD 10000h
var2 DWORD 20000h
.code ; ---EAX---
mov eax,var1 ; 00010000h
add eax,var2 ; 00030000h
add ax,0FFFFh ; 0003FFFFh
add eax,1 ; 00040000h
sub ax,1 ; 0004FFFFh
```
:::
### 【**NEG**】
**可以是 <font color="red">Register</font> or <font color="red">Memory</font>**
:::spoiler code
```javascript=
.data
valB BYTE -1
valW WORD +32767
.code
mov al,valB ; AL = -1
neg al ; AL = +1
neg valW ; valW = -32767
```
:::
#### 【**NEG & Flags**】
**`NEG` 透過 `SUB 0,operand`(internal) 來設定所有非 0 operands 的 `carry flag`**
:::spoiler code
```javascript=
.data
valB BYTE 1,0
valC SBYTE -128
.code
neg valB ; CF = 1, OF = 0
neg [valB + 1] ; CF = 0, OF = 0
neg valC ; CF = 1, OF = 1
```
:::
### 【**Flags Affected by Arithmetic**】:label:
<font color="Red">【Hint】</font> `Flag based on the destination operand.`
:::success
**以下皆為運算後的結果**
- Zero Flag:結果為 0 <font color="red">`ZF = 1`</font>
- Sign Flag:MSB(最高位元) 為 1 <font color="red">`SF = 1`</font>
- Carry Flag:

- Overflow Flag:

:::
#### 【**Overflow Flag**】

---
## Chapter 5 Procedure
### 5-1 Stack Operation
#### 【**Runtime Stack**】
* Use ESP to point the address of stack

>**In Real Memory**
>
* The stack grows downward. The area below ESP is always available (unless the **stack has overflowed**).
* Q: Why **must** each character be **put in EAX** before it is pushed?
`Because only `word (16-bit)` or `**doubleword (32-bit)**`` values can be pushed on the stack.
`
* 不能 push/pop 一個 byte,至少要兩個
### 5-2 Defining and Using Procedure
>If a procedure is called **without its preconditions** satisfied, it will probably **not** produce the expected output.
#### 【**CALL & RET**】
1. CALL:
* **Push**es offset of **next instruction** on the stack
* Copy the address of the **called procedure** into **EIP**
2. RET:
* **Pop** top of stack into **EIP**


#### 【USES】
* 保留了原先進入副程式前的狀態(在主程式的狀況)



