程序員需要了解的硬核知識大全

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們每個程序員或許都有一個夢,那就是成爲大牛,我們或許都沉浸在各種框架中,以爲框架就是一切,以爲應用層纔是最重要的,你錯了。在當今計算機行業中,會應用是基本素質,如果你懂其原理才能讓你在行業中走的更遠,而計算機基礎知識又是重中之重。下面,跟隨我的腳步,爲你介紹一下計算機底層知識。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"CPU"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還不瞭解 CPU 嗎?現在就帶你瞭解一下 CPU 是什麼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CPU 的全稱是 "},{"type":"codeinline","content":[{"type":"text","text":"Central Processing Unit"}]},{"type":"text","text":",它是你的電腦中最"},{"type":"codeinline","content":[{"type":"text","text":"硬核"}]},{"type":"text","text":"的組件,這種說法一點不爲過。CPU 是能夠讓你的計算機叫"},{"type":"codeinline","content":[{"type":"text","text":"計算機"}]},{"type":"text","text":"的核心組件,但是它卻不能代表你的電腦,CPU 與計算機的關係就相當於大腦和人的關係。CPU 的核心是從程序或應用程序獲取指令並執行計算。此過程可以分爲三個關鍵階段:"},{"type":"text","marks":[{"type":"strong"}],"text":"提取,解碼和執行"},{"type":"text","text":"。CPU從系統的主存中提取指令,然後解碼該指令的實際內容,然後再由 CPU 的相關部分執行該指令。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"CPU 內部處理過程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下圖展示了一般程序的運行流程(以 C 語言爲例),可以說了解程序的運行流程是掌握程序運行機制的基礎和前提。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6f/6fd177f5e08f8df95be46bf93b5e7958.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在這個流程中,CPU 負責的就是解釋和運行最終轉換成機器語言的內容。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CPU 主要由兩部分構成:"},{"type":"codeinline","content":[{"type":"text","text":"控制單元"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"算術邏輯單元(ALU)"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"控制單元:從內存中提取指令並解碼執行"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"算數邏輯單元(ALU):處理算數和邏輯運算"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CPU 是計算機的心臟和大腦,它和內存都是由許多晶體管組成的電子部件。它接收數據輸入,執行指令並處理信息。它與輸入/輸出(I / O)設備進行通信,這些設備向 CPU 發送數據和從 CPU 接收數據。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從功能來看,CPU 的內部由"},{"type":"text","marks":[{"type":"strong"}],"text":"寄存器、控制器、運算器和時鐘"},{"type":"text","text":"四部分組成,各部分之間通過電信號連通。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b8/b8ef14bcb21dc696f6821391e1baf25a.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"寄存器"}]},{"type":"text","text":"是中央處理器內的組成部分。它們可以用來暫存指令、數據和地址。可以將其看作是內存的一種。根據種類的不同,一個 CPU 內部會有 20 - 100個寄存器。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"控制器"}]},{"type":"text","text":"負責把內存上的指令、數據讀入寄存器,並根據指令的結果控制計算機"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"運算器"}]},{"type":"text","text":"負責運算從內存中讀入寄存器的數據"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"時鐘"}]},{"type":"text","text":" 負責發出 CPU 開始計時的時鐘信號"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"CPU 是一系列寄存器的集合體"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 CPU 的四個結構中,我們程序員只需要瞭解"},{"type":"codeinline","content":[{"type":"text","text":"寄存器"}]},{"type":"text","text":"就可以了,其餘三個不用過多關注,爲什麼這麼說?因爲程序是把寄存器作爲對象來描述的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不同類型的 CPU ,其內部寄存器的種類,數量以及寄存器存儲的數值範圍都是不同的。不過,根據功能的不同,可以將寄存器劃分爲下面這幾類"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 種類 | 功能 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ---------- | ------------------------------------------------------------ |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 累加寄存器 | 存儲運行的數據和運算後的數據。 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 標誌寄存器 | 用於反應處理器的狀態和運算結果的某些特徵以及控制指令的執行。 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 程序計數器 | 程序計數器是用於存放下一條指令所在單元的地址的地方。 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 基址寄存器 | 存儲數據內存的起始位置 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 變址寄存器 | 存儲基址寄存器的相對地址 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 通用寄存器 | 存儲任意數據 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 指令寄存器 | 儲存正在被運行的指令,CPU內部使用,程序員無法對該寄存器進行讀寫 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 棧寄存器 | 存儲棧區域的起始位置 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其中"},{"type":"text","marks":[{"type":"strong"}],"text":"程序計數器、累加寄存器、標誌寄存器、指令寄存器和棧寄存器"},{"type":"text","text":"都只有一個,其他寄存器一般有多個。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/13/132dd55a499a5953b378476861dc41de.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面就對各個寄存器進行說明"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"程序計數器"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"程序計數器(Program Counter)"}]},{"type":"text","text":"是用來存儲下一條指令所在單元的地址。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"程序執行時,PC的初值爲程序第一條指令的地址,在順序執行程序時,"},{"type":"codeinline","content":[{"type":"text","text":"控制器"}]},{"type":"text","text":"首先按程序計數器所指出的指令地址從內存中取出一條指令,然後分析和執行該指令,同時將PC的值加1指向下一條要執行的指令。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們還是以一個事例爲準來詳細的看一下程序計數器的執行過程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/29/295f4b15d62106c139ff24c9c62825bd.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這是一段進行相加的操作,程序啓動,在經過編譯解析後會由操作系統把硬盤中的程序複製到內存中,示例中的程序是將 123 和 456 執行相加操作,並將結果輸出到顯示器上。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"地址 "},{"type":"codeinline","content":[{"type":"text","text":"0100"}]},{"type":"text","text":" 是程序運行的起始位置。Windows 等操作系統把程序從硬盤複製到內存後,會將程序計數器作爲設定爲起始位置 0100,然後執行程序,每執行一條指令後,程序計數器的數值會增加1(或者直接指向下一條指令的地址),然後,CPU 就會根據程序計數器的數值,從內存中讀取命令並執行,也就是說,"},{"type":"text","marks":[{"type":"strong"}],"text":"程序計數器控制着程序的流程"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"條件分支和循環機制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"高級語言中的條件控制流程主要分爲三種:"},{"type":"codeinline","content":[{"type":"text","text":"順序執行、條件分支、循環判斷"}]},{"type":"text","text":"三種,順序執行是按照地址的內容順序的執行指令。條件分支是根據條件執行任意地址的指令。循環是重複執行同一地址的指令。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"順序執行的情況比較簡單,每執行一條指令程序計數器的值就是 + 1。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"條件和循環分支會使程序計數器的值指向任意的地址,這樣一來,程序便可以返回到上一個地址來重複執行同一個指令,或者跳轉到任意指令。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面以條件分支爲例來說明程序的執行過程(循環也很相似)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/91/9107f031b339c74e2b278bc9ff2adeca.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"程序的開始過程和順序流程是一樣的,CPU 從0100處開始執行命令,在0100和0101都是順序執行,PC 的值順序+1,執行到0102地址的指令時,判斷0106寄存器的數值大於0,跳轉(jump)到0104地址的指令,將數值輸出到顯示器中,然後結束程序,0103 的指令被跳過了,這就和我們程序中的 "},{"type":"codeinline","content":[{"type":"text","text":"if()"}]},{"type":"text","text":" 判斷是一樣的,在不滿足條件的情況下,指令會直接跳過。所以 PC 的執行過程也就沒有直接+1,而是下一條指令的地址。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"標誌寄存器"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"條件和循環分支會使用到 "},{"type":"codeinline","content":[{"type":"text","text":"jump(跳轉指令)"}]},{"type":"text","text":",會根據當前的指令來判斷是否跳轉,上面我們提到了"},{"type":"codeinline","content":[{"type":"text","text":"標誌寄存器"}]},{"type":"text","text":",無論當前累加寄存器的運算結果是正數、負數還是零,標誌寄存器都會將其保存"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CPU 在進行運算時,標誌寄存器的數值會根據當前運算的結果自動設定,運算結果的正、負和零三種狀態由標誌寄存器的三個位表示。標誌寄存器的第一個字節位、第二個字節位、第三個字節位各自的結果都爲1時,分別代表着正數、零和負數。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d5/d5692aa8cfdc82865b37bfdce50550b5.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CPU 的執行機制比較有意思,假設累加寄存器中存儲的 XXX 和通用寄存器中存儲的 YYY 做比較,執行比較的背後,CPU 的運算機制就會做減法運算。而無論減法運算的結果是正數、零還是負數,都會保存到標誌寄存器中。結果爲正表示 XXX 比 YYY 大,結果爲零表示 XXX 和 YYY 相等,結果爲負表示 XXX 比 YYY 小。程序比較的指令,實際上是在 CPU 內部做"},{"type":"codeinline","content":[{"type":"text","text":"減法"}]},{"type":"text","text":"運算。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"函數調用機制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來,我們繼續介紹函數調用機制,哪怕是高級語言編寫的程序,函數調用處理也是通過把程序計數器的值設定成函數的存儲地址來實現的。函數執行跳轉指令後,必須進行返回處理,單純的指令跳轉沒有意義,下面是一個實現函數跳轉的例子"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e5/e56a888a08540e2a88b123ed835633bc.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"圖中將變量 a 和 b 分別賦值爲 123 和 456 ,調用 MyFun(a,b) 方法,進行指令跳轉。圖中的地址是將 C 語言編譯成機器語言後運行時的地址,由於1行 C 程序在編譯後通常會變爲多行機器語言,所以圖中的地址是分散的。在執行完 MyFun(a,b)指令後,程序會返回到 MyFun(a,b) 的下一條指令,CPU 繼續執行下面的指令。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"函數的調用和返回很重要的兩個指令是 "},{"type":"codeinline","content":[{"type":"text","text":"call"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"return"}]},{"type":"text","text":" 指令,再將函數的入口地址設定到程序計數器之前,call 指令會把調用函數後要執行的指令地址存儲在名爲棧的主存內。函數處理完畢後,再通過函數的出口來執行 return 指令。return 指令的功能是把保存在棧中的地址設定到程序計數器。MyFun 函數在被調用之前,0154 地址保存在棧中,MyFun 函數處理完成後,會把 0154 的地址保存在程序計數器中。這個調用過程如下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ce/ceb4c1733013f5c6d715ad41d7ce6938.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在一些高級語言的條件或者循環語句中,函數調用的處理會轉換成 call 指令,函數結束後的處理則會轉換成 return 指令。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"通過地址和索引實現數組"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來我們看一下基址寄存器和變址寄存器,通過這兩個寄存器,我們可以對主存上的特定區域進行劃分,來實現類似數組的操作,首先,我們用十六進制數將計算機內存上的 00000000 - FFFFFFFF 的地址劃分出來。那麼,凡是該範圍的內存地址,只要有一個 32 位的寄存器,便可查看全部地址。但如果想要想數組那樣分割特定的內存區域以達到連續查看的目的的話,使用兩個寄存器會更加方便。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如,我們用兩個寄存器(基址寄存器和變址寄存器)來表示內存的值"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5a/5afdb79c1ae1570a9e8c7f25323e7e15.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這種表示方式很類似數組的構造,"},{"type":"codeinline","content":[{"type":"text","text":"數組"}]},{"type":"text","text":"是指同樣長度的數據在內存中進行連續排列的數據構造。用數組名錶示數組全部的值,通過索引來區分數組的各個數據元素,例如: a[0] - a[4],"},{"type":"codeinline","content":[{"type":"text","text":"[]"}]},{"type":"text","text":"內的 0 - 4 就是數組的下標。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"CPU 指令執行過程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"幾乎所有的馮·諾伊曼型計算機的CPU,其工作都可以分爲5個階段:"},{"type":"text","marks":[{"type":"strong"}],"text":"取指令、指令譯碼、執行指令、訪存取數、結果寫回"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"取指令"}]},{"type":"text","text":"階段是將內存中的指令讀取到 CPU 中寄存器的過程,程序寄存器用於存儲下一條指令所在的地址"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"指令譯碼"}]},{"type":"text","text":"階段,在取指令完成後,立馬進入指令譯碼階段,在指令譯碼階段,指令譯碼器按照預定的指令格式,對取回的指令進行拆分和解釋,識別區分出不同的指令類別以及各種獲取操作數的方法。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"執行指令"}]},{"type":"text","text":"階段,譯碼完成後,就需要執行這一條指令了,此階段的任務是完成指令所規定的各種操作,具體實現指令的功能。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"訪問取數"}]},{"type":"text","text":"階段,根據指令的需要,有可能需要從內存中提取數據,此階段的任務是:根據指令地址碼,得到操作數在主存中的地址,並從主存中讀取該操作數用於運算。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"結果寫回"}]},{"type":"text","text":"階段,作爲最後一個階段,結果寫回(Write Back,WB)階段把執行指令階段的運行結果數據“寫回”到某種存儲形式:結果數據經常被寫到CPU的內部寄存器中,以便被後續的指令快速地存取;"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"內存"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CPU 和 內存就像是一堆不可分割的戀人一樣,是無法拆散的一對兒,沒有內存,CPU 無法執行程序指令,那麼計算機也就失去了意義;只有內存,無法執行指令,那麼計算機照樣無法運行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼什麼是內存呢?內存和 CPU 如何進行交互?下面就來介紹一下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"什麼是內存"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內存(Memory)是計算機中最重要的部件之一,它是程序與CPU進行溝通的橋樑。計算機中所有程序的運行都是在內存中進行的,因此內存對計算機的影響非常大,內存又被稱爲"},{"type":"codeinline","content":[{"type":"text","text":"主存"}]},{"type":"text","text":",其作用是存放 CPU 中的運算數據,以及與硬盤等外部存儲設備交換的數據。只要計算機在運行中,CPU 就會把需要運算的數據調到主存中進行運算,當運算完成後CPU再將結果傳送出來,主存的運行也決定了計算機的穩定運行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"內存的物理結構"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內存的內部是由各種 IC 電路組成的,它的種類很龐大,但是其主要分爲三種存儲器"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隨機存儲器(RAM): 內存中最重要的一種,表示既可以從中讀取數據,也可以寫入數據。當機器關閉時,內存中的信息會 "},{"type":"codeinline","content":[{"type":"text","text":"丟失"}]},{"type":"text","text":"。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"只讀存儲器(ROM):ROM 一般只能用於數據的讀取,不能寫入數據,但是當機器停電時,這些數據不會丟失。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"高速緩存(Cache):Cache 也是我們經常見到的,它分爲一級緩存(L1 Cache)、二級緩存(L2 Cache)、三級緩存(L3 Cache)這些數據,它位於內存和 CPU 之間,是一個讀寫速度比內存"},{"type":"codeinline","content":[{"type":"text","text":"更快"}]},{"type":"text","text":"的存儲器。當 CPU 向內存寫入數據時,這些數據也會被寫入高速緩存中。當 CPU 需要讀取數據時,會直接從高速緩存中直接讀取,當然,如需要的數據在Cache中沒有,CPU會再去讀取內存中的數據。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內存 IC 是一個完整的結構,它內部也有電源、地址信號、數據信號、控制信號和用於尋址的 IC 引腳來進行數據的讀寫。下面是一個虛擬的 IC 引腳示意圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7a/7aa326fd95943543016550f5850933ba.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"圖中 VCC 和 GND 表示電源,A0 - A9 是地址信號的引腳,D0 - D7 表示的是控制信號、RD 和 WR 都是好控制信號,我用不同的顏色進行了區分,將電源連接到 VCC 和 GND 後,就可以對其他引腳傳遞 0 和 1 的信號,大多數情況下,"},{"type":"text","marks":[{"type":"strong"}],"text":"+5V 表示1,0V 表示 0"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們都知道內存是用來存儲數據,那麼這個內存 IC 中能存儲多少數據呢?D0 - D7 表示的是數據信號,也就是說,一次可以輸入輸出 8 bit = 1 byte 的數據。A0 - A9 是地址信號共十個,表示可以指定 00000 00000 - 11111 11111 共 2 的 10次方 = "},{"type":"codeinline","content":[{"type":"text","text":"1024個地址"}]},{"type":"text","text":"。每個地址都會存放 1 byte 的數據,因此我們可以得出內存 IC 的容量就是 1 KB。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"內存的讀寫過程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"讓我們把關注點放在內存 IC 對數據的讀寫過程上來吧!我們來看一個對內存IC 進行數據寫入和讀取的模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b4/b48ffa5edac8afb98844b1d8ea85b554.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來詳細描述一下這個過程,假設我們要向內存 IC 中寫入 1byte 的數據的話,它的過程是這樣的:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先給 VCC 接通 +5V 的電源,給 GND 接通 0V 的電源,使用 "},{"type":"codeinline","content":[{"type":"text","text":"A0 - A9"}]},{"type":"text","text":" 來指定數據的存儲場所,然後再把數據的值輸入給 "},{"type":"codeinline","content":[{"type":"text","text":"D0 - D7"}]},{"type":"text","text":" 的數據信號,並把 "},{"type":"codeinline","content":[{"type":"text","text":"WR(write)"}]},{"type":"text","text":"的值置爲 1,執行完這些操作後,即可以向內存 IC 寫入數據"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"讀出數據時,只需要通過 A0 - A9 的地址信號指定數據的存儲場所,然後再將 RD 的值置爲 1 即可。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"圖中的 RD 和 WR 又被稱爲控制信號。其中當WR 和 RD 都爲 0 時,無法進行寫入和讀取操作。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"內存的現實模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了便於記憶,我們把內存模型映射成爲我們現實世界的模型,在現實世界中,內存的模型很想我們生活的樓房。在這個樓房中,1層可以存儲一個字節的數據,樓層號就是"},{"type":"codeinline","content":[{"type":"text","text":"地址"}]},{"type":"text","text":",下面是內存和樓層整合的模型圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/01/01f34b6cd85b8b736563d6302fca821f.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們知道,程序中的數據不僅只有數值,還有"},{"type":"codeinline","content":[{"type":"text","text":"數據類型"}]},{"type":"text","text":"的概念,從內存上來看,就是佔用內存大小(佔用樓層數)的意思。即使物理上強制以 1 個字節爲單位來逐一讀寫數據的內存,在程序中,通過指定其數據類型,也能實現以特定字節數爲單位來進行讀寫。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"二進制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們都知道,計算機的底層都是使用二進制數據進行數據流傳輸的,那麼爲什麼會使用二進制表示計算機呢?或者說,什麼是二進制數呢?在拓展一步,如何使用二進制進行加減乘除?下面就來看一下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"什麼是二進制數"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼什麼是二進制數呢?爲了說明這個問題,我們先把 "},{"type":"codeinline","content":[{"type":"text","text":"00100111"}]},{"type":"text","text":" 這個數轉換爲十進制數看一下,二進制數轉換爲十進制數,直接將各位置上的值 * 位權即可,那麼我們將上面的數值進行轉換"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b9/b93f70c72a7b67b88be18ff22c7f102f.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"也就是說,二進制數代表的 "},{"type":"codeinline","content":[{"type":"text","text":"00100111"}]},{"type":"text","text":" 轉換成十進制就是 39,這個 39 並不是 3 和 9 兩個數字連着寫,而是 3 "},{"type":"text","marks":[{"type":"italic"}],"text":" 10 + 9 "},{"type":"text","text":" 1,這裏面的 "},{"type":"codeinline","content":[{"type":"text","text":"10 , 1"}]},{"type":"text","text":" 就是位權,以此類推,上述例子中的位權從高位到低位依次就是 "},{"type":"codeinline","content":[{"type":"text","text":"7 6 5 4 3 2 1 0 "}]},{"type":"text","text":"。這個位權也叫做次冪,那麼最高位就是2的7次冪,2的6次冪 等等。二進制數的運算每次都會以2爲底,這個2 指得就是基數,那麼十進制數的基數也就是 10 。在任何情況下位權的值都是 "},{"type":"text","marks":[{"type":"strong"}],"text":"數的位數 - 1"},{"type":"text","text":",那麼第一位的位權就是 1 - 1 = 0, 第二位的位權就睡 2 - 1 = 1,以此類推。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼我們所說的二進制數其實就是 用0和1兩個數字來表示的數,它的基數爲2,它的數值就是每個數的位數 "},{"type":"text","marks":[{"type":"italic"}],"text":" 位權再求和得到的結果,我們一般來說數值指的就是十進制數,那麼它的數值就是 3 "},{"type":"text","text":" 10 + 9 * 1 = 39。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"移位運算和乘除的關係"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在瞭解過二進制之後,下面我們來看一下二進制的運算,和十進制數一樣,加減乘除也適用於二進制數,只要注意逢 2 進位即可。二進制數的運算,也是計算機程序所特有的運算,因此瞭解二進制的運算是必須要掌握的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先我們來介紹"},{"type":"codeinline","content":[{"type":"text","text":"移位"}]},{"type":"text","text":" 運算,移位運算是指將二進制的數值的各個位置上的元素坐左移和右移操作,見下圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0a/0a608c69d8aae84eeb793dff8a60bf96.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"補數"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"剛纔我們沒有介紹右移的情況,是因爲右移之後空出來的高位數值,有 0 和 1 兩種形式。要想區分什麼時候補0什麼時候補1,首先就需要掌握二進制數表示"},{"type":"codeinline","content":[{"type":"text","text":"負數"}]},{"type":"text","text":"的方法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"二進制數中表示負數值時,一般會把最高位作爲符號來使用,因此我們把這個最高位當作符號位。"},{"type":"text","text":" 符號位是 0 時表示"},{"type":"codeinline","content":[{"type":"text","text":"正數"}]},{"type":"text","text":",是 1 時表示 "},{"type":"codeinline","content":[{"type":"text","text":"負數"}]},{"type":"text","text":"。那麼 -1 用二進制數該如何表示呢?可能很多人會這麼認爲: 因爲 1 的二進制數是 "},{"type":"codeinline","content":[{"type":"text","text":"0000 0001"}]},{"type":"text","text":",最高位是符號位,所以正確的表示 -1 應該是 "},{"type":"codeinline","content":[{"type":"text","text":"1000 0001"}]},{"type":"text","text":",但是這個答案真的對嗎?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"計算機世界中是沒有減法的,計算機在做減法的時候其實就是在做加法,也就是用加法來實現的減法運算。比如 100 - 50 ,其實計算機來看的時候應該是 100 + (-50),爲此,在表示負數的時候就要用到"},{"type":"codeinline","content":[{"type":"text","text":"二進制補數"}]},{"type":"text","text":",補數就是用正數來表示的負數。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了獲得"},{"type":"codeinline","content":[{"type":"text","text":"補數"}]},{"type":"text","text":",我們需要將二進制的各數位的數值全部取反,然後再將結果 + 1 即可,先記住這個結論,下面我們來演示一下。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c2/c2984741dd8a753aa2039b61c9dfc532.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"具體來說,就是需要先獲取某個數值的二進制數,然後對二進制數的每一位做取反操作(0 ---> 1 , 1 ---> 0),最後再對取反後的數 +1 ,這樣就完成了補數的獲取。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"補數的獲取,雖然直觀上不易理解,但是邏輯上卻非常嚴謹,比如我們來看一下 1 - 1 的這個過程,我們先用上面的這個 "},{"type":"codeinline","content":[{"type":"text","text":"1000 0001"}]},{"type":"text","text":"(它是1的補數,不知道的請看上文,正確性先不管,只是用來做一下計算)來表示一下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/de/de6cdafb42395f241b48838d68f89fe5.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"奇怪,1 - 1 會變成 130 ,而不是0,所以可以得出結論 "},{"type":"codeinline","content":[{"type":"text","text":"1000 0001"}]},{"type":"text","text":" 表示 -1 是完全錯誤的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼正確的該如何表示呢?其實我們上面已經給出結果了,那就是 "},{"type":"codeinline","content":[{"type":"text","text":"1111 1111"}]},{"type":"text","text":",來論證一下它的正確性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/38/388046a99f68fe205637873cfb752d55.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們可以看到 1 - 1 其實實際上就是 1 + (-1),對 -1 進行上面的取反 + 1 後變爲 "},{"type":"codeinline","content":[{"type":"text","text":"1111 1111"}]},{"type":"text","text":", 然後與 1 進行加法運算,得到的結果是九位的 "},{"type":"codeinline","content":[{"type":"text","text":"1 0000 0000"}]},{"type":"text","text":",結果發生了"},{"type":"codeinline","content":[{"type":"text","text":"溢出"}]},{"type":"text","text":",計算機會直接忽略掉溢出位,也就是直接拋掉 最高位 1 ,變爲 "},{"type":"codeinline","content":[{"type":"text","text":"0000 0000"}]},{"type":"text","text":"。也就是 0,結果正確,所以 "},{"type":"codeinline","content":[{"type":"text","text":"1111 1111"}]},{"type":"text","text":" 表示的就是 -1 。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"所以負數的二進制表示就是先求其補數,補數的求解過程就是對原始數值的二進制數各位取反,然後將結果 + 1"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"算數右移和邏輯右移的區別"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在瞭解完補數後,我們重新考慮一下右移這個議題,右移在移位後空出來的最高位有兩種情況 "},{"type":"codeinline","content":[{"type":"text","text":"0 和 1"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"將二進制數作爲帶符號的數值進行右移運算時,移位後需要在最高位填充移位前符號位的值( 0 或 1)。這就被稱爲"},{"type":"codeinline","content":[{"type":"text","text":"算數右移"}]},{"type":"text","text":"。如果數值使用補數表示的負數值,那麼右移後在空出來的最高位補 1,就可以正確的表示 "},{"type":"codeinline","content":[{"type":"text","text":"1/2,1/4,1/8"}]},{"type":"text","text":"等的數值運算。如果是正數,那麼直接在空出來的位置補 0 即可。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面來看一個右移的例子。將 -4 右移兩位,來各自看一下移位示意圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/24/24d77e21c41d41d54fe0f12f65e439b9.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上圖所示,在邏輯右移的情況下, -4 右移兩位會變成 "},{"type":"codeinline","content":[{"type":"text","text":"63"}]},{"type":"text","text":", 顯然不是它的 1/4,所以不能使用邏輯右移,那麼算數右移的情況下,右移兩位會變爲 "},{"type":"codeinline","content":[{"type":"text","text":"-1"}]},{"type":"text","text":",顯然是它的 1/4,故而採用算數右移。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼我們可以得出來一個結論:"},{"type":"text","marks":[{"type":"strong"}],"text":"左移時,無論是圖形還是數值,移位後,只需要將低位補 0 即可;右移時,需要根據情況判斷是邏輯右移還是算數右移。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面介紹一下符號擴展:"},{"type":"text","marks":[{"type":"strong"}],"text":"將數據進行符號擴展是爲了產生一個位數加倍、但數值大小不變的結果,以滿足有些指令對操作數位數的要求,例如倍長於除數的被除數,再如將數據位數加長以減少計算過程中的誤差。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以8位二進制爲例,符號擴展就是指在保持值不變的前提下將其轉換成爲16位和32位的二進制數。將"},{"type":"codeinline","content":[{"type":"text","text":"0111 1111"}]},{"type":"text","text":"這個正的 8位二進制數轉換成爲 16位二進制數時,很容易就能夠得出"},{"type":"codeinline","content":[{"type":"text","text":"0000 0000 0111 1111"}]},{"type":"text","text":"這個正確的結果,但是像 "},{"type":"codeinline","content":[{"type":"text","text":"1111 1111"}]},{"type":"text","text":"這樣的補數來表示的數值,該如何處理?直接將其表示成爲"},{"type":"codeinline","content":[{"type":"text","text":"1111 1111 1111 1111"}]},{"type":"text","text":"就可以了。也就是說,不管正數還是補數表示的負數,只需要將 0 和 1 填充高位即可。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"內存和磁盤的關係"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們大家知道,計算機的五大基礎部件是 "},{"type":"codeinline","content":[{"type":"text","text":"存儲器"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"控制器"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"運算器"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"輸入和輸出設備"}]},{"type":"text","text":",其中從存儲功能的角度來看,可以把存儲器分爲"},{"type":"codeinline","content":[{"type":"text","text":"內存"}]},{"type":"text","text":"和 "},{"type":"codeinline","content":[{"type":"text","text":"磁盤"}]},{"type":"text","text":",我們上面介紹過內存,下面就來介紹一下磁盤以及磁盤和內存的關係"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"程序不讀入內存就無法運行"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"計算機最主要的存儲部件是內存和磁盤。"},{"type":"text","marks":[{"type":"strong"}],"text":"磁盤中存儲的程序必須加載到內存中才能運行"},{"type":"text","text":",在磁盤中保存的程序是無法直接運行的,這是因爲負責解析和運行程序內容的 CPU 是需要通過程序計數器來指定內存地址從而讀出程序指令的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2a/2a5ea9400b7a64954ef734ca7c374041.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"磁盤構造"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"磁盤緩存"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們上面提到,磁盤往往和內存是互利共生的關係,相互協作,彼此持有良好的合作關係。每次內存都需要從磁盤中讀取數據,必然會讀到相同的內容,所以一定會有一個角色負責存儲我們經常需要讀到的內容。 我們大家做軟件的時候經常會用到"},{"type":"codeinline","content":[{"type":"text","text":"緩存技術"}]},{"type":"text","text":",那麼硬件層面也不例外,磁盤也有緩存,磁盤的緩存叫做"},{"type":"codeinline","content":[{"type":"text","text":"磁盤緩存"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"磁盤緩存指的是把從磁盤中讀出的數據存儲到內存的方式,這樣一來,當接下來需要讀取相同的內容時,就不會再通過實際的磁盤,而是通過磁盤緩存來讀取。某一種技術或者框架的出現勢必要解決某種問題的,那麼磁盤緩存就大大"},{"type":"text","marks":[{"type":"strong"}],"text":"改善了磁盤訪問的速度"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/58/5844434a9ccf86ab51d1fda29070e45d.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"虛擬內存"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"虛擬內存"}]},{"type":"text","text":"是內存和磁盤交互的第二個媒介。虛擬內存是指把磁盤的一部分作爲"},{"type":"codeinline","content":[{"type":"text","text":"假想內存"}]},{"type":"text","text":"來使用。這與磁盤緩存是假想的磁盤(實際上是內存)相對,虛擬內存是假想的內存(實際上是磁盤)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"虛擬內存是計算機系統內存管理的一種技術。它使得應用程序認爲它擁有"},{"type":"codeinline","content":[{"type":"text","text":"連續可用"}]},{"type":"text","text":"的內存(一個完整的地址空間),但是實際上,它通常被分割成多個物理碎片,還有部分存儲在外部磁盤管理器上,必要時進行數據交換。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過藉助虛擬內存,在內存不足時仍然可以運行程序。例如,在只剩 5MB 內存空間的情況下仍然可以運行 10MB 的程序。由於 CPU 只能執行加載到內存中的程序,因此,虛擬內存的空間就需要和內存中的空間進行"},{"type":"codeinline","content":[{"type":"text","text":"置換(swap)"}]},{"type":"text","text":",然後運行程序。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"虛擬內存與內存的交換方式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"虛擬內存的方法有"},{"type":"codeinline","content":[{"type":"text","text":"分頁式"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"分段式"}]},{"type":"text","text":" 兩種。Windows 採用的是分頁式。該方式是指在不考慮程序構造的情況下,把運行的程序按照一定大小的頁進行分割,並以"},{"type":"codeinline","content":[{"type":"text","text":"頁"}]},{"type":"text","text":"爲單位進行置換。在分頁式中,我們把磁盤的內容讀到內存中稱爲 "},{"type":"codeinline","content":[{"type":"text","text":"Page In"}]},{"type":"text","text":",把內存的內容寫入磁盤稱爲 "},{"type":"codeinline","content":[{"type":"text","text":"Page Out"}]},{"type":"text","text":"。Windows 計算機的頁大小爲 4KB ,也就是說,需要把應用程序按照 4KB 的頁來進行切分,以頁(page)爲單位放到磁盤中,然後進行置換。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5e/5e7b02eb3a45f9e47d8b84b8da2ccba6.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了實現內存功能,Windows 在磁盤上提供了虛擬內存使用的文件(page file,頁文件)。該文件由 Windows 生成和管理,文件的大小和虛擬內存大小相同,通常大小是內存的 1 - 2 倍。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"磁盤的物理結構"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之前我們介紹了CPU、內存的物理結構,現在我們來介紹一下磁盤的物理結構。"},{"type":"text","marks":[{"type":"strong"}],"text":"磁盤的物理結構指的是磁盤存儲數據的形式"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"磁盤是通過其物理表面劃分成多個空間來使用的。劃分的方式有兩種:"},{"type":"codeinline","content":[{"type":"text","text":"可變長方式"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"扇區方式"}]},{"type":"text","text":"。前者是將物理結構劃分成長度可變的空間,後者是將磁盤結構劃分爲固定長度的空間。一般 Windows 所使用的硬盤和軟盤都是使用扇區這種方式。扇區中,把磁盤表面分成若干個同心圓的空間就是 "},{"type":"codeinline","content":[{"type":"text","text":"磁道"}]},{"type":"text","text":",把磁道按照固定大小的存儲空間劃分而成的就是 "},{"type":"codeinline","content":[{"type":"text","text":"扇區"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/88/881ba0bcb9836531db3ee3530dc5cbd0.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"扇區"}]},{"type":"text","text":"是對磁盤進行物理讀寫的最小單位。Windows 中使用的磁盤,一般是一個扇區 512 個字節。不過,Windows 在邏輯方面對磁盤進行讀寫的單位是扇區整數倍簇。根據磁盤容量不同功能,1簇可以是 512 字節(1 簇 = 1扇區)、1KB(1簇 = 2扇區)、2KB、4KB、8KB、16KB、32KB( 1 簇 = 64 扇區)。簇和扇區的大小是相等的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"壓縮算法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們想必都有過"},{"type":"codeinline","content":[{"type":"text","text":"壓縮"}]},{"type":"text","text":"和 "},{"type":"codeinline","content":[{"type":"text","text":"解壓縮"}]},{"type":"text","text":"文件的經歷,當文件太大時,我們會使用文件壓縮來降低文件的佔用空間。比如微信上傳文件的限制是100 MB,我這裏有個文件夾無法上傳,但是我解壓完成後的文件一定會小於 100 MB,那麼我的文件就可以上傳了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此外,我們把相機拍完的照片保存到計算機上的時候,也會使用壓縮算法進行文件壓縮,文件壓縮的格式一般是"},{"type":"codeinline","content":[{"type":"text","text":"JPEG"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼什麼是壓縮算法呢?壓縮算法又是怎麼定義的呢?在認識算法之前我們需要先了解一下文件是如何存儲的"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"文件存儲"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文件是將數據存儲在磁盤等存儲媒介的一種形式。程序文件中最基本的存儲數據單位是"},{"type":"codeinline","content":[{"type":"text","text":"字節"}]},{"type":"text","text":"。文件的大小不管是 xxxKB、xxxMB等來表示,就是因爲文件是以字節 "},{"type":"codeinline","content":[{"type":"text","text":"B = Byte"}]},{"type":"text","text":" 爲單位來存儲的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文件就是字節數據的集合。用 1 字節(8 位)表示的字節數據有 256 種,用二進制表示的話就是 0000 0000 - 1111 1111 。如果文件中存儲的數據是文字,那麼該文件就是文本文件。如果是圖形,那麼該文件就是圖像文件。在任何情況下,文件中的字節數都是"},{"type":"codeinline","content":[{"type":"text","text":"連續存儲"}]},{"type":"text","text":"的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/81/814ac25f5163a8b158c5bd4e9fbbb9c9.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"壓縮算法的定義"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面介紹了文件的集合體其實就是一堆字節數據的集合,那麼我們就可以來給壓縮算法下一個定義。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"壓縮算法(compaction algorithm)"}]},{"type":"text","text":"指的就是數據壓縮的算法,主要包括壓縮和還原(解壓縮)的兩個步驟。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實就是在不改變原有文件屬性的前提下,降低文件字節空間和佔用空間的一種算法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根據壓縮算法的定義,我們可將其分成不同的類型:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"有損和無損"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"無損壓縮:能夠"},{"type":"codeinline","content":[{"type":"text","text":"無失真地"}]},{"type":"text","text":"從壓縮後的數據重構,準確地還原原始數據。可用於對數據的準確性要求嚴格的場合,如可執行文件和普通文件的壓縮、磁盤的壓縮,也可用於多媒體數據的壓縮。該方法的壓縮比較小。如差分編碼、RLE、Huffman編碼、LZW編碼、算術編碼。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有損壓縮:有失真,"},{"type":"codeinline","content":[{"type":"text","text":"不能完全準確地"}]},{"type":"text","text":"恢復原始數據,重構的數據只是原始數據的一個近似。可用於對數據的準確性要求不高的場合,如多媒體數據的壓縮。該方法的壓縮比較大。例如預測編碼、音感編碼、分形壓縮、小波壓縮、JPEG/MPEG。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"對稱性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果編解碼算法的複雜性和所需時間差不多,則爲對稱的編碼方法,多數壓縮算法都是對稱的。但也有不對稱的,一般是編碼難而解碼容易,如 Huffman 編碼和分形編碼。但用於密碼學的編碼方法則相反,是編碼容易,而解碼則非常難。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"幀間與幀內"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在視頻編碼中會同時用到幀內與幀間的編碼方法,幀內編碼是指在一幀圖像內獨立完成的編碼方法,同靜態圖像的編碼,如 JPEG;而幀間編碼則需要參照前後幀才能進行編解碼,並在編碼過程中考慮對幀之間的時間冗餘的壓縮,如 MPEG。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"實時性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在有些多媒體的應用場合,需要實時處理或傳輸數據(如現場的數字錄音和錄影、播放MP3/RM/VCD/DVD、視頻/音頻點播、網絡現場直播、可視電話、視頻會議),編解碼一般要求延時 ≤50 ms。這就需要簡單/快速/高效的算法和高速/複雜的CPU/DSP芯片。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"分級處理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有些壓縮算法可以同時處理不同分辨率、不同傳輸速率、不同質量水平的多媒體數據,如JPEG2000、MPEG-2/4。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這些概念有些抽象,主要是爲了讓大家瞭解一下壓縮算法的分類,下面我們就對具體的幾種常用的壓縮算法來分析一下它的特點和優劣"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"幾種常用壓縮算法的理解"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"RLE 算法的機制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來就讓我們正式看一下文件的壓縮機制。首先讓我們來嘗試對 "},{"type":"codeinline","content":[{"type":"text","text":"AAAAAABBCDDEEEEEF"}]},{"type":"text","text":" 這 17 個半角字符的文件(文本文件)進行壓縮。雖然這些文字沒有什麼實際意義,但是很適合用來描述 "},{"type":"codeinline","content":[{"type":"text","text":"RLE"}]},{"type":"text","text":" 的壓縮機制。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於半角字符(其實就是英文字符)是作爲 1 個字節保存在文件中的,所以上述的文件的大小就是 17 字節。如圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1f/1f752496118aba72d36c83702649ebc3.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼,如何才能壓縮該文件呢?大家不妨也考慮一下,只要是能夠使文件小於 17 字節,我們可以使用任何壓縮算法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最顯而易見的一種壓縮方式我覺得你已經想到了,就是把相同的字符"},{"type":"codeinline","content":[{"type":"text","text":"去重化"}]},{"type":"text","text":",也就是 "},{"type":"codeinline","content":[{"type":"text","text":"字符 * 重複次數"}]},{"type":"text","text":" 的方式進行壓縮。所以上面文件壓縮後就會變成下面這樣"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/98/987605bd5db88e6b4fd4f112671f3fa0.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從圖中我們可以看出,"},{"type":"text","marks":[{"type":"strong"}],"text":"AAAAAABBCDDEEEEEF"},{"type":"text","text":" 的17個字符成功被壓縮成了 "},{"type":"text","marks":[{"type":"strong"}],"text":"A6B2C1D2E5F1"},{"type":"text","text":" 的12個字符,也就是 12 / 17 = 70%,壓縮比爲 70%,壓縮成功了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"像這樣,把文件內容用 "},{"type":"codeinline","content":[{"type":"text","text":"數據 * 重複次數"}]},{"type":"text","text":" 的形式來表示的壓縮方法成爲 "},{"type":"codeinline","content":[{"type":"text","text":"RLE(Run Length Encoding, 行程長度編碼)"}]},{"type":"text","text":" 算法。RLE 算法是一種很好的壓縮方法,經常用於壓縮傳真的圖像等。因爲圖像文件的本質也是字節數據的集合體,所以可以用 RLE 算法進行壓縮"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"哈夫曼算法和莫爾斯編碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面我們來介紹另外一種壓縮算法,即哈夫曼算法。在瞭解哈夫曼算法之前,你必須捨棄"},{"type":"codeinline","content":[{"type":"text","text":"半角英文數字的1個字符是1個字節(8位)的數據"}]},{"type":"text","text":"。下面我們就來認識一下哈夫曼算法的基本思想。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文本文件是由不同類型的字符組合而成的,而且不同字符出現的次數也是不一樣的。例如,在某個文本文件中,A 出現了 100次左右,Q僅僅用到了 3 次,類似這樣的情況很常見。哈夫曼算法的關鍵就在於 "},{"type":"text","marks":[{"type":"strong"}],"text":"多次出現的數據用小於 8 位的字節數表示,不常用的數據則可以使用超過 8 位的字節數表示"},{"type":"text","text":"。A 和 Q 都用 8 位來表示時,原文件的大小就是 100次 "},{"type":"text","marks":[{"type":"italic"}],"text":" 8 位 + 3次 "},{"type":"text","text":" 8 位 = 824位,假設 A 用 2 位,Q 用 10 位來表示就是 2 "},{"type":"text","marks":[{"type":"italic"}],"text":" 100 + 3 "},{"type":"text","text":" 10 = 230 位。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">不過要注意一點,最終磁盤的存儲都是以8位爲一個字節來保存文件的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"哈夫曼算法比較複雜,在深入瞭解之前我們先吃點"},{"type":"codeinline","content":[{"type":"text","text":"甜品"}]},{"type":"text","text":",瞭解一下 "},{"type":"codeinline","content":[{"type":"text","text":"莫爾斯編碼"}]},{"type":"text","text":",你一定看過美劇或者戰爭片的電影,在戰爭中的通信經常採用莫爾斯編碼來傳遞信息,例如下面"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e2/e2c5a8ea40716393851bc9c35a76f7c4.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來我們來講解一下莫爾斯編碼,下面是莫爾斯編碼的"},{"type":"codeinline","content":[{"type":"text","text":"示例"}]},{"type":"text","text":",大家把 1 看作是短點(嘀),把 11 看作是長點(嗒)即可。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/af/afdefcec68a08ab0c8bc2f3fae4bc8a8.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"莫爾斯編碼一般把文本中出現最高頻率的字符用"},{"type":"codeinline","content":[{"type":"text","text":"短編碼"}]},{"type":"text","text":" 來表示。如表所示,假如表示短點的位是 1,表示長點的位是 11 的話,那麼 E(嘀)這一數據的字符就可以用 1 來表示,C(滴答滴答)就可以用 9 位的 "},{"type":"codeinline","content":[{"type":"text","text":"110101101"}]},{"type":"text","text":"來表示。在實際的莫爾斯編碼中,如果短點的長度是 1 ,長點的長度就是 3,短點和長點的間隔就是1。這裏的長度指的就是聲音的長度。比如我們想用上面的 AAAAAABBCDDEEEEEF 例子來用莫爾斯編碼重寫,在莫爾斯曼編碼中,各個字符之間需要加入表示時間間隔的符號。這裏我們用 00 加以區分。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以,AAAAAABBCDDEEEEEF 這個文本就變爲了 A "},{"type":"text","marks":[{"type":"italic"}],"text":" 6 次 + B "},{"type":"text","text":" 2次 + C "},{"type":"text","marks":[{"type":"italic"}],"text":" 1次 + D "},{"type":"text","text":" 2次 + E "},{"type":"text","marks":[{"type":"italic"}],"text":" 5次 + F "},{"type":"text","text":" 1次 + 字符間隔 "},{"type":"text","marks":[{"type":"italic"}],"text":" 16 = 4 位 "},{"type":"text","text":" 6次 + 8 位 "},{"type":"text","marks":[{"type":"italic"}],"text":" 2次 + 9 位 "},{"type":"text","text":" 1 次 + 6位 "},{"type":"text","marks":[{"type":"italic"}],"text":" 2次 + 1位 "},{"type":"text","text":" 5次 + 8 位 "},{"type":"text","marks":[{"type":"italic"}],"text":" 1次 + 2位 "},{"type":"text","text":" 16次 = 106位 = 14字節。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"所以使用莫爾斯電碼的壓縮比爲 14 / 17 = 82%"},{"type":"text","text":"。效率並不太突出。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"用二叉樹實現哈夫曼算法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"剛纔已經提到,莫爾斯編碼是根據日常文本中各字符的出現頻率來決定表示各字符的編碼數據長度的。不過,在該編碼體系中,對 AAAAAABBCDDEEEEEF 這種文本來說並不是效率最高的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面我們來看一下哈夫曼算法。哈夫曼算法是指,爲各壓縮對象文件分別構造最佳的編碼體系,並以該編碼體系爲基礎來進行壓縮。因此,用什麼樣的編碼(哈夫曼編碼)對數據進行分割,就要由各個文件而定。用哈夫曼算法壓縮過的文件中,存儲着哈夫曼編碼信息和壓縮過的數據。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/bd/bdb93d6dd4b23a9213f33ffb156c9118.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來,我們在對 AAAAAABBCDDEEEEEF 中的 A - F 這些字符,按照"},{"type":"codeinline","content":[{"type":"text","text":"出現頻率高的字符用盡量少的位數編碼來表示"}]},{"type":"text","text":"這一原則進行整理。按照出現頻率從高到低的順序整理後,結果如下,同時也列出了編碼方案。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 字符 | 出現頻率 | 編碼(方案) | 位數 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| :--: | :------: | :----------: | :--: |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| A | 6 | 0 | 1 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| E | 5 | 1 | 1 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| B | 2 | 10 | 2 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| D | 2 | 11 | 2 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| C | 1 | 100 | 3 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| F | 1 | 101 | 3 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上表的編碼方案中,隨着出現頻率的降低,字符編碼信息的數據位數也在逐漸增加,從最開始的 1位、2位依次增加到3位。不過這個編碼體系是存在問題的,你不知道100這個3位的編碼,它的意思是用 1、0、0這三個編碼來表示 E、A、A 呢?還是用10、0來表示 B、A 呢?還是用100來表示 C 呢。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而在哈夫曼算法中,通過藉助哈夫曼樹的構造編碼體系,即使在不使用字符區分符號的情況下,也可以構建能夠明確進行區分的編碼體系。不過哈夫曼樹的算法要比較複雜,下面是一個哈夫曼樹的構造過程。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1e/1e70451d3d83f6e5f67d40af5ececbec.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自然界樹的從根開始生葉的,而哈夫曼樹則是葉生枝"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"哈夫曼樹能夠提升壓縮比率"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用哈夫曼樹之後,出現頻率越高的數據所佔用的位數越少,這也是哈夫曼樹的核心思想。通過上圖的步驟二可以看出,枝條連接數據時,我們是從出現頻率較低的數據開始的。這就意味着出現頻率低的數據到達根部的枝條也越多。而枝條越多則意味着編碼的位數隨之增加。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來我們來看一下哈夫曼樹的壓縮比率,用上圖得到的數據表示 AAAAAABBCDDEEEEEF 爲 000000000000 100100 110 101101 0101010101 111,40位 = 5 字節。壓縮前的數據是 17 字節,壓縮後的數據竟然達到了驚人的5 字節,也就是壓縮比率 = 5 / 17 = 29% 如此高的壓縮率,簡直是太驚豔了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大家可以參考一下,無論哪種類型的數據,都可以用哈夫曼樹作爲壓縮算法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 文件類型 | 壓縮前 | 壓縮後 | 壓縮比率 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| :------: | :-------: | :------: | :------: |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 文本文件 | 14862字節 | 4119字節 | 28% |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 圖像文件 | 96062字節 | 9456字節 | 10% |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| EXE文件 | 24576字節 | 4652字節 | 19% |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"可逆壓縮和非可逆壓縮"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最後,我們來看一下圖像文件的數據形式。圖像文件的使用目的通常是把圖像數據輸出到顯示器、打印機等設備上。常用的圖像格式有 : "},{"type":"codeinline","content":[{"type":"text","text":"BMP"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"JPEG"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"TIFF"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"GIF"}]},{"type":"text","text":" 格式等。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"BMP : 是使用 Windows 自帶的畫筆來做成的一種圖像形式"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"JPEG:是數碼相機等常用的一種圖像數據形式"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TIFF: 是一種通過在文件中包含\"標籤\"就能夠快速顯示出數據性質的圖像形式"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GIF: 是由美國開發的一種數據形式,要求色數不超過 256個"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"圖像文件可以使用前面介紹的 RLE 算法和哈夫曼算法,因爲圖像文件在多數情況下並不要求數據需要還原到和壓縮之前一摸一樣的狀態,允許丟失一部分數據。我們把能還原到壓縮前狀態的壓縮稱爲 "},{"type":"codeinline","content":[{"type":"text","text":"可逆壓縮"}]},{"type":"text","text":",無法還原到壓縮前狀態的壓縮稱爲"},{"type":"codeinline","content":[{"type":"text","text":"非可逆壓縮"}]},{"type":"text","text":" 。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/57/579c69c11c1da790a516f784ad575c15.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般來說,JPEG格式的文件是非可逆壓縮,因此還原後有部分圖像信息比較模糊。GIF 是可逆壓縮"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"操作系統"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"操作系統環境"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"程序中包含着"},{"type":"codeinline","content":[{"type":"text","text":"運行環境"}]},{"type":"text","text":"這一內容,可以說 "},{"type":"text","marks":[{"type":"strong"}],"text":"運行環境 = 操作系統 + 硬件"},{"type":"text","text":" ,操作系統又可以被稱爲軟件,它是由一系列的指令組成的。我們不介紹操作系統,我們主要來介紹一下硬件的識別。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們肯定都玩兒過遊戲,你玩兒遊戲前需要幹什麼?是不是需要先看一下自己的筆記本或者電腦是不是能肝的起遊戲?下面是一個遊戲的配置(懷念一下 wow)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d3/d3e75c18a041dffc937ed43bc81ed254.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"圖中的主要配置如下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"操作系統版本:說的就是應用程序運行在何種系統環境,現在市面上主要有三種操作系統環境,Windows 、Linux 和 Unix ,一般我們玩兒的大型遊戲幾乎都是在 Windows 上運行,可以說 Windows 是遊戲的天堂。Windows 操作系統也會有區分,分爲32位操作系統和64位操作系統,互不兼容。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"處理器:處理器指的就是 CPU,你的電腦的計算能力,通俗來講就是每秒鐘能處理的指令數,如果你的電腦覺得卡帶不起來的話,很可能就是 CPU 的計算能力不足導致的。想要加深理解,請閱讀博主的另一篇文章:"},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s?_biz=MzU2NDg0OTgyMA==&mid=2247484585&idx=1&sn=0d6c3ccf8cc5bec2fea80eb437213801&chksm=fc45f95acb32704c7dcc952a803e88e8a9b0e67c86d8c27abf4e6c776e48b1fcd770dd2dcc8d&token=653889808&lang=zhCN#rd","title":""},"content":[{"type":"text","text":"程序員需要了解的硬核知識之CPU"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"顯卡:顯卡承擔圖形的輸出任務,因此又被稱爲圖形處理器(Graphic Processing Unit,GPU),顯卡也非常重要,比如我之前玩兒的"},{"type":"codeinline","content":[{"type":"text","text":"劍靈"}]},{"type":"text","text":"開五檔(其實就是圖像變得更清晰)會卡,其實就是顯卡顯示不出來的原因。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內存:內存即主存,就是你的應用程序在運行時能夠動態分析指令的這部分存儲空間,它的大小也能決定你電腦的運行速度,想要加深理解,請閱讀博主的另一篇文章 "},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s?_biz=MzU2NDg0OTgyMA==&mid=2247484617&idx=1&sn=497af1daab7bba6b44ca291194c56621&chksm=fc45f93acb32702c5bc8575f7830f2b585952e31e62af3d73867c42360a2f538ae779452eb52&token=653889808&lang=zhCN#rd","title":""},"content":[{"type":"text","text":"程序員需要了解的硬核知識之內存"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"存儲空間:存儲空間指的就是應用程序安裝所佔用的磁盤空間,由圖中可知,此遊戲的最低存儲空間必須要大於 5GB,其實我們都會遺留很大一部分用來安裝遊戲。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從程序的運行環境這一角度來考量的話,CPU 的種類是特別重要的參數,爲了使程序能夠正常運行,必須滿足 CPU 所需的最低配置。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CPU 只能解釋其自身固有的語言。不同的 CPU 能解釋的機器語言的種類也是不同的。機器語言的程序稱爲 "},{"type":"codeinline","content":[{"type":"text","text":"本地代碼(native code)"}]},{"type":"text","text":",程序員用 C 等高級語言編寫的程序,僅僅是文本文件。"},{"type":"codeinline","content":[{"type":"text","text":"文本文件(排除文字編碼的問題)"}]},{"type":"text","text":"在任何環境下都能顯示和編輯。我們稱之爲"},{"type":"codeinline","content":[{"type":"text","text":"源代碼"}]},{"type":"text","text":"。通過對源代碼進行編譯,就可以得到"},{"type":"codeinline","content":[{"type":"text","text":"本地代碼"}]},{"type":"text","text":"。下圖反映了這個過程。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"uploading-image-703074.png"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Windows 操作系統克服了CPU以外的硬件差異"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"計算機的硬件並不僅僅是由 CPU 組成的,還包括用於存儲程序指令的數據和內存,以及通過 I/O 連接的鍵盤、顯示器、硬盤、打印機等外圍設備。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 WIndows 軟件中,鍵盤輸入、顯示器輸出等並不是直接向硬件發送指令。而是通過向 Windows 發送指令實現的。因此,程序員就不用注意內存和 I/O 地址的不同構成了。Windows 操作的是硬件而不是軟件,軟件通過操作 Windows 系統可以達到控制硬件的目的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d9/d9aa353fd69c34b9770dd904fdd661c3.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"不同操作系統的 API 差異性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來我們看一下操作系統的種類。同樣機型的計算機,可安裝的操作系統類型也會有多種選擇。例如:AT 兼容機除了可以安裝 Windows 之外,還可以採用 Unix 系列的 Linux 以及 FreeBSD (也是一種Unix操作系統)等多個操作系統。當然,應用軟件則必須根據不同的操作系統類型來專門開發。"},{"type":"text","marks":[{"type":"strong"}],"text":"CPU 的類型不同,所對應機器的語言也不同"},{"type":"text","text":",同樣的道理,"},{"type":"text","marks":[{"type":"strong"}],"text":"操作系統的類型不同,應用程序向操作系統傳遞指令的途徑也不同"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"應用程序向系統傳遞指令的途徑稱爲 "},{"type":"codeinline","content":[{"type":"text","text":"API(Application Programming Interface)"}]},{"type":"text","text":"。Windows 以及 Linux 操作系統的 API,提供了任何應用程序都可以利用的函數組合。因爲不同操作系統的 API 是有差異的。所以,如何要將同樣的應用程序移植到另外的操作系統,就必須要覆蓋應用所用到的 API 部分。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"鍵盤輸入、鼠標輸入、顯示器輸出、文件輸入和輸出等同外圍設備進行交互的功能,都是通過 API 提供的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"這也就是爲什麼 Windows 應用程序不能直接移植到 Linux 操作系統上的原因,API 差異太大了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在同類型的操作系統下,不論硬件如何,API 幾乎相同。但是,由於不同種類 CPU 的機器語言不同,因此本地代碼也不盡相同。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"操作系統功能的歷史"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"操作系統"}]},{"type":"text","text":"其實也是一種軟件,任何新事物的出現肯定都有它的歷史背景,那麼操作系統也不是憑空出現的,肯定有它的歷史背景。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在計算機尚不存在操作系統的年代,完全沒有任何程序,人們通過各種"},{"type":"codeinline","content":[{"type":"text","text":"按鈕"}]},{"type":"text","text":"來控制計算機,這一過程非常麻煩。於是,有人開發出了僅具有加載和運行功能的"},{"type":"codeinline","content":[{"type":"text","text":"監控程序"}]},{"type":"text","text":",這就是操作系統的原型。通過事先啓動監控程序,程序員可以根據需要將各種程序加載到內存中運行。雖然仍舊比較麻煩,但比起在沒有任何程序的狀態下進行開發,工作量得到了很大的緩解。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0b/0b3266ded5fe53053aa444f940a86a67.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隨着時代的發展,人們在利用監控程序編寫程序的過程中發現很多程序都有公共的部分。例如,通過鍵盤進行文字輸入,顯示器進行數據展示等,如果每編寫一個新的應用程序都需要相同的處理的話,那真是太浪費時間了。因此,基本的輸入輸出部分的程序就被追加到了監控程序中。初期的操作系統就是這樣誕生了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/55/556c0fefd95cfab37b83975704825bd4.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"類似的想法可以共用,人們又發現有更多的應用程序可以追加到監控程序中,比如"},{"type":"codeinline","content":[{"type":"text","text":"硬件控制程序"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":"編程語言處理器(彙編、編譯、解析)"}]},{"type":"text","text":"以及各種應用程序等,結果就形成了和現在差異不大的操作系統,也就是說,其實操作系統是多個程序的集合體。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/51/51ebef1b3ffba3518998f866a4b356c9.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Windows 操作系統的特徵"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Windows 操作系統是世界上用戶數量最龐大的羣體,作爲 Windows 操作系統的"},{"type":"codeinline","content":[{"type":"text","text":"資深"}]},{"type":"text","text":"用戶,你都知道 Windows 操作系統有哪些特徵嗎?下面列舉了一些 Windows 操作系統的特性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Windows 操作系統有兩個版本:32位和64位"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過 "},{"type":"codeinline","content":[{"type":"text","text":"API"}]},{"type":"text","text":" 函數集成來提供系統調用"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"提供了採用圖形用戶界面的用戶界面"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過 "},{"type":"codeinline","content":[{"type":"text","text":"WYSIWYG"}]},{"type":"text","text":" 實現打印輸出,WYSIWYG 其實就是 What You See Is What You Get ,值得是顯示器上顯示的圖形和文本都是可以原樣輸出到打印機打印的。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"提供多任務功能,即能夠同時開啓多個任務"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"提供網絡功能和數據庫功能"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過即插即用實現設備驅動的自設定"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這些是對程序員來講比較有意義的一些特徵,下面針對這些特徵來進行分別的介紹"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"32位操作系統"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏表示的32位操作系統表示的是"},{"type":"text","marks":[{"type":"strong"}],"text":"處理效率最高的數據大小"},{"type":"text","text":"。Windows 處理數據的基本單位是 32 位。這與最一開始在 "},{"type":"codeinline","content":[{"type":"text","text":"MS-DOS"}]},{"type":"text","text":" 等16位操作系統不同,因爲在16位操作系統中處理32位數據需要兩次,而32位操作系統只需要一次就能夠處理32位的數據,所以一般在 windows 上的應用,它們的最高能夠處理的數據都是 32 位的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如,用 C 語言來處理整數數據時,有8位的 "},{"type":"codeinline","content":[{"type":"text","text":"char"}]},{"type":"text","text":" 類型,16位的"},{"type":"codeinline","content":[{"type":"text","text":"short"}]},{"type":"text","text":"類型,以及32位的"},{"type":"codeinline","content":[{"type":"text","text":"long"}]},{"type":"text","text":"類型三個選項,使用位數較大的 long 類型進行處理的話,增加的只是內存以及磁盤的開銷,對性能影響不大。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在市面上大部分都是64位操作系統了,64位操作系統也是如此。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"通過 API 函數集來提供系統調用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Windows 是通過名爲 "},{"type":"codeinline","content":[{"type":"text","text":"API"}]},{"type":"text","text":" 的函數集來提供系統調用的。API是聯繫應用程序和操作系統之間的接口,全稱叫做 "},{"type":"codeinline","content":[{"type":"text","text":"Application Programming Interface"}]},{"type":"text","text":",應用程序接口。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當前主流的32位版 Windows API 也稱爲 "},{"type":"codeinline","content":[{"type":"text","text":"Win32 API"}]},{"type":"text","text":",之所以這樣命名,是需要和不同的操作系統進行區分,比如最一開始的 16 位版的 "},{"type":"codeinline","content":[{"type":"text","text":"Win16 API"}]},{"type":"text","text":",和後來流行的 "},{"type":"codeinline","content":[{"type":"text","text":"Win64 API"}]},{"type":"text","text":" 。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"API 通過多個 DLL 文件來提供,各個 API 的實體都是用 C 語言編寫的函數。所以,在 C 語言環境下,使用 API 更加容易,比如 API 所用到的 "},{"type":"codeinline","content":[{"type":"text","text":"MessageBox()"}]},{"type":"text","text":" 函數,就被保存在了 Windows 提供的 user32.dll 這個 DLL 文件中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"提供採用了 GUI 的用戶界面"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"GUI(Graphical User Interface)"}]},{"type":"text","text":" 指得就是圖形用戶界面,通過點擊顯示器中的窗口以及圖標等可視化的用戶界面,舉個例子:Linux 操作系統就有兩個版本,一種是簡潔版,直接通過命令行控制硬件,還有一種是可視化版,通過光標點擊圖形界面來控制硬件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"通過 WYSIWYG 實現打印輸出"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"WYSIWYG 指的是顯示器上輸出的內容可以直接通過打印機打印輸出。在 Windows 中,顯示器和打印機被認作同等的圖形輸出設備處理的,該功能也爲 WYSIWYG 提供了條件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"藉助 WYSIWYG 功能,程序員可以輕鬆不少。最初,爲了是現在顯示器中顯示和在打印機中打印,就必須分別編寫各自的程序,而在 Windows 中,可以藉助 WYSIWYG 基本上在一個程序中就可以做到顯示和打印這兩個功能了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"提供多任務功能"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"多任務指的就是同時能夠運行多個應用程序的功能,Windows 是通過"},{"type":"codeinline","content":[{"type":"text","text":"時鐘分割"}]},{"type":"text","text":"技術來實現多任務功能的。時鐘分割指的是短時間間隔內,多個程序切換運行的方式。在用戶看來,就好像是多個程序在同時運行,其底層是 "},{"type":"codeinline","content":[{"type":"text","text":"CPU 時間切片"}]},{"type":"text","text":",這也是多線程多任務的核心。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/21/2134590adbdb03d018b31fd06da102a5.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"提供網絡功能和數據庫功能"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Windows 中,網絡功能是作爲標準功能提供的。數據庫(數據庫服務器)功能有時也會在後面追加。網絡功能和數據庫功能雖然並不是操作系統不可或缺的,但因爲它們和操作系統很接近,所以被統稱爲"},{"type":"codeinline","content":[{"type":"text","text":"中間件"}]},{"type":"text","text":"而不是應用。意思是處於操作系統和應用的中間層,操作系統和中間件組合在一起,稱爲"},{"type":"codeinline","content":[{"type":"text","text":"系統軟件"}]},{"type":"text","text":"。應用不僅可以利用操作系統,也可以利用中間件的功能。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/28/28549dc6418b47f435251452d622d990.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相對於操作系統一旦安裝就不能輕易更換,中間件可以根據需要進行更換,不過,對於大部分應用來說,更換中間件的話,會造成應用也隨之更換,從這個角度來說,更å換中間件也不是那麼容易。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"通過即插即用實現設備驅動的自動設定"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"即插即用(Plug-and-Play)"}]},{"type":"text","text":"指的是新的設備連接(plug) 後就可以直接使用的機制,新設備連接計算機後,計算機就會自動安裝和設定用來控制該設備的"},{"type":"codeinline","content":[{"type":"text","text":"驅動程序"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"設備驅動是操作系統的一部分,提供了同硬件進行基本的輸入輸出的功能。鍵盤、鼠標、顯示器、磁盤裝置等,這些計算機中必備的硬件的設備驅動,一般都是隨操作系統一起安裝的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有時 DLL 文件也會同設備驅動文件一起安裝。這些 DLL 文件中存儲着用來利用該新追加的硬件API,通過 API ,可以製作出運行該硬件的心應用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"彙編語言和本地代碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們在之前的文章中探討過,計算機 CPU 只能運行本地代碼(機器語言)程序,用 C 語言等高級語言編寫的代碼,需要經過編譯器編譯後,轉換爲本地代碼才能夠被 CPU 解釋執行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是本地代碼的可讀性非常差,所以需要使用一種能夠直接讀懂的語言來替換本地代碼,那就是在各本地代碼中,附帶上表示其功能的英文縮寫,比如在加法運算的本地代碼加上"},{"type":"codeinline","content":[{"type":"text","text":"add(addition)"}]},{"type":"text","text":" 的縮寫、在比較運算符的本地代碼中加上"},{"type":"codeinline","content":[{"type":"text","text":"cmp(compare)"}]},{"type":"text","text":"的縮寫等,這些通過縮寫來表示具體本地代碼指令的標誌稱爲 "},{"type":"codeinline","content":[{"type":"text","text":"助記符"}]},{"type":"text","text":",使用助記符的語言稱爲"},{"type":"codeinline","content":[{"type":"text","text":"彙編語言"}]},{"type":"text","text":"。這樣,通過閱讀彙編語言,也能夠了解本地代碼的含義了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不過,即使是使用彙編語言編寫的源代碼,最終也必須要轉換爲本地代碼才能夠運行,負責做這項工作的程序稱爲"},{"type":"codeinline","content":[{"type":"text","text":"編譯器"}]},{"type":"text","text":",轉換的這個過程稱爲"},{"type":"codeinline","content":[{"type":"text","text":"彙編"}]},{"type":"text","text":"。在將源代碼轉換爲本地代碼這個功能方面,彙編器和編譯器是同樣的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用彙編語言編寫的源代碼和本地代碼是一一對應的。因而,本地代碼也可以反過來轉換成彙編語言編寫的代碼。把本地代碼轉換爲彙編代碼的這一過程稱爲"},{"type":"codeinline","content":[{"type":"text","text":"反彙編"}]},{"type":"text","text":",執行反彙編的程序稱爲"},{"type":"codeinline","content":[{"type":"text","text":"反彙編程序"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/df/df8f30148f5016d880bbbeec43b22389.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"哪怕是 C 語言編寫的源代碼,編譯後也會轉換成特定 CPU 用的本地代碼。而將其反彙編的話,就可以得到彙編語言的源代碼,並對其內容進行調查。不過,"},{"type":"text","marks":[{"type":"strong"}],"text":"本地代碼變成 C 語言源代碼的反編譯,要比本地代碼轉換成彙編代碼的反彙編要困難"},{"type":"text","text":",這是因爲,C 語言代碼和本地代碼不是一一對應的關係。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"通過編譯器輸出彙編語言的源代碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們上面提到本地代碼可以經過反彙編轉換成爲彙編代碼,但是隻有這一種轉換方式嗎?顯然不是,C 語言編寫的源代碼也能夠通過編譯器編譯稱爲彙編代碼,下面就來嘗試一下。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先需要先做一些準備,需要先下載 "},{"type":"codeinline","content":[{"type":"text","text":"Borland C++ 5.5"}]},{"type":"text","text":" 編譯器,爲了方便,我這邊直接下載好了讀者直接從我的百度網盤提取即可 (鏈接:https://pan.baidu.com/s/19LqVICpn5GcV88thD2AnlA 密碼:hz1u)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下載完畢,需要進行配置,下面是配置說明 (https://wenku.baidu.com/view/22e2f418650e52ea551898ad.html),教程很完整跟着配置就可以,下面開始我們的編譯過程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先用 Windows 記事本等文本編輯器編寫如下代碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"c"},"content":[{"type":"text","text":"// 返回兩個參數值之和的函數\nint AddNum(int a,int b){\n return a + b;\n}\n\n// 調用 AddNum 函數的函數\nvoid MyFunc(){\n int c;\n c = AddNum(123,456);\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"編寫完成後將其文件名保存爲 Sample4.c ,C 語言源文件的擴展名,通常用"},{"type":"codeinline","content":[{"type":"text","text":".c"}]},{"type":"text","text":" 來表示,上面程序是提供兩個輸入參數並返回它們之和。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Windows 操作系統下打開 "},{"type":"codeinline","content":[{"type":"text","text":"命令提示符"}]},{"type":"text","text":",切換到保存 Sample4.c 的文件夾下,然後在命令提示符中輸入"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"c"},"content":[{"type":"text","text":"bcc32 -c -S Sample4.c"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"bcc32 是啓動 Borland C++ 的命令,"},{"type":"codeinline","content":[{"type":"text","text":"-c"}]},{"type":"text","text":" 的選項是指僅進行編譯而不進行鏈接,"},{"type":"codeinline","content":[{"type":"text","text":"-S"}]},{"type":"text","text":" 選項被用來指定生成彙編語言的源代碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作爲編譯的結果,當前目錄下會生成一個名爲"},{"type":"codeinline","content":[{"type":"text","text":"Sample4.asm"}]},{"type":"text","text":" 的彙編語言源代碼。彙編語言源文件的擴展名,通常用"},{"type":"codeinline","content":[{"type":"text","text":".asm"}]},{"type":"text","text":" 來表示,下面就讓我們用編輯器打開看一下 Sample4.asm 中的內容"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"\t.386p\n\tifdef ??version\n\tif ??version GT 500H\n\t.mmx\n\tendif\n\tendif\n\tmodel flat\n\tifndef\t??version\n\t?debug\tmacro\n\tendm\n\tendif\n\t?debug\tS \"Sample4.c\"\n\t?debug\tT \"Sample4.c\"\n_TEXT\tsegment dword public use32 'CODE'\n_TEXT\tends\n_DATA\tsegment dword public use32 'DATA'\n_DATA\tends\n_BSS\tsegment dword public use32 'BSS'\n_BSS\tends\nDGROUP\tgroup\t_BSS,_DATA\n_TEXT\tsegment dword public use32 'CODE'\n_AddNum\tproc\tnear\n?live1@0:\n ;\t\n ;\tint AddNum(int a,int b){\n ;\t\n\tpush ebp\n\tmov ebp,esp\n ;\t\n ;\t\n ;\t return a + b;\n ;\t\n@1:\n\tmov eax,dword ptr [ebp+8]\n\tadd eax,dword ptr [ebp+12]\n ;\t\n ;\t}\n ;\t\n@3:\n@2:\n\tpop ebp\n\tret \n_AddNum\tendp\n_MyFunc\tproc\tnear\n?live1@48:\n ;\t\n ;\tvoid MyFunc(){\n ;\t\n\tpush ebp\n\tmov ebp,esp\n ;\t\n ;\t int c;\n ;\t c = AddNum(123,456);\n ;\t\n@4:\n\tpush 456\n\tpush 123\n\tcall _AddNum\n\tadd esp,8\n ;\t\n ;\t}\n ;\t\n@5:\n\tpop ebp\n\tret \n_MyFunc\tendp\n_TEXT\tends\n\tpublic\t_AddNum\n\tpublic\t_MyFunc\n\t?debug\tD \"Sample4.c\" 20343 45835\n\tend"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣,編譯器就成功的把 C 語言轉換成爲了彙編代碼了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"不會轉換成本地代碼的僞指令"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第一次看到彙編代碼的讀者可能感覺起來比較難,不過實際上其實比較簡單,而且可能比 C 語言還要簡單,爲了便於閱讀彙編代碼的源代碼,需要注意幾個要點"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"彙編語言的源代碼,是由轉換成本地代碼的指令(後面講述的操作碼)和針對彙編器的僞指令構成的。僞指令負責把程序的構造以及彙編的方法指示給彙編器(轉換程序)。不過僞指令是無法彙編轉換成爲本地代碼的。下面是上面程序截取的僞指令"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_TEXT\tsegment dword public use32 'CODE'\n_TEXT\tends\n_DATA\tsegment dword public use32 'DATA'\n_DATA\tends\n_BSS\tsegment dword public use32 'BSS'\n_BSS\tends\nDGROUP\tgroup\t_BSS,_DATA\n\n_AddNum\tproc\tnear\n_AddNum\tendp\n\n_MyFunc\tproc\tnear\n_MyFunc\tendp\n\n_TEXT\tends\n\tend"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由僞指令 "},{"type":"codeinline","content":[{"type":"text","text":"segment"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"ends"}]},{"type":"text","text":" 圍起來的部分,是給構成程序的命令和數據的集合體上加一個名字而得到的,稱爲"},{"type":"codeinline","content":[{"type":"text","text":"段定義"}]},{"type":"text","text":"。段定義的英文表達具有"},{"type":"codeinline","content":[{"type":"text","text":"區域"}]},{"type":"text","text":"的意思,在這個程序中,段定義指的是命令和數據等程序的集合體的意思,一個程序由多個段定義構成。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面代碼的開始位置,定義了3個名稱分別爲 "},{"type":"codeinline","content":[{"type":"text","text":"_TEXT、_DATA、_BSS"}]},{"type":"text","text":" 的段定義,"},{"type":"codeinline","content":[{"type":"text","text":"_TEXT"}]},{"type":"text","text":" 是指定的段定義,"},{"type":"codeinline","content":[{"type":"text","text":"_DATA"}]},{"type":"text","text":" 是被初始化(有初始值)的數據的段定義,"},{"type":"codeinline","content":[{"type":"text","text":"_BSS"}]},{"type":"text","text":" 是尚未初始化的數據的段定義。這種定義的名稱是由 Borland C++ 定義的,是由 Borland C++ 編譯器自動分配的,所以程序段定義的順序就成爲了 "},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"italic"}],"text":"TEXT、"},{"type":"text","text":"DATA、_BSS"}]},{"type":"text","text":" ,這樣也確保了內存的連續性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_TEXT\tsegment dword public use32 'CODE'\n_TEXT\tends\n_DATA\tsegment dword public use32 'DATA'\n_DATA\tends\n_BSS\tsegment dword public use32 'BSS'\n_BSS\tends"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">段定義( segment ) 是用來區分或者劃分範圍區域的意思。彙編語言的 segment 僞指令表示段定義的起始,ends 僞指令表示段定義的結束。段定義是一段連續的內存空間"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而"},{"type":"codeinline","content":[{"type":"text","text":"group"}]},{"type":"text","text":" 這個僞指令表示的是將 "},{"type":"codeinline","content":[{"type":"text","text":"_BSS和_DATA"}]},{"type":"text","text":" 這兩個段定義彙總名爲 DGROUP 的組"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"DGROUP\tgroup\t_BSS,_DATA"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"圍起 "},{"type":"codeinline","content":[{"type":"text","text":"_AddNum"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"_MyFun"}]},{"type":"text","text":" 的 "},{"type":"codeinline","content":[{"type":"text","text":"_TEXT"}]},{"type":"text","text":" segment 和 "},{"type":"codeinline","content":[{"type":"text","text":"_TEXT"}]},{"type":"text","text":" ends ,表示"},{"type":"codeinline","content":[{"type":"text","text":"_AddNum"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"_MyFun"}]},{"type":"text","text":" 是屬於 "},{"type":"codeinline","content":[{"type":"text","text":"_TEXT"}]},{"type":"text","text":" 這一段定義的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_TEXT\tsegment dword public use32 'CODE'\n_TEXT\tends"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,即使在源代碼中指令和數據是混雜編寫的,經過編譯和彙編後,也會轉換成爲規整的本地代碼。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"_AddNum proc "}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"_AddNum endp"}]},{"type":"text","text":" 圍起來的部分,以及"},{"type":"codeinline","content":[{"type":"text","text":"_MyFunc proc"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"_MyFunc endp"}]},{"type":"text","text":" 圍起來的部分,分別表示 AddNum 函數和 MyFunc 函數的範圍。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_AddNum\tproc\tnear\n_AddNum\tendp\n\n_MyFunc\tproc\tnear\n_MyFunc\tendp"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"編譯後在函數名前附帶上下劃線"},{"type":"codeinline","content":[{"type":"text","text":"_"}]},{"type":"text","text":" ,是 Borland C++ 的規定。在 C 語言中編寫的 AddNum 函數,在內部是以 _AddNum 這個名稱處理的。僞指令 proc 和 endp 圍起來的部分,表示的是 "},{"type":"codeinline","content":[{"type":"text","text":"過程(procedure)"}]},{"type":"text","text":" 的範圍。在彙編語言中,這種相當於 C 語言的函數的形式稱爲過程。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"末尾的 "},{"type":"codeinline","content":[{"type":"text","text":"end"}]},{"type":"text","text":" 僞指令,表示的是源代碼的結束。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"彙編語言的語法是 操作碼 + 操作數"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在彙編語言中,一行表示一對 CPU 的一個指令。彙編語言指令的語法結構是 "},{"type":"text","marks":[{"type":"strong"}],"text":"操作碼 + 操作數"},{"type":"text","text":",也存在只有操作碼沒有操作數的指令。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"操作碼錶示的是指令動作,操作數表示的是指令對象。操作碼和操作數一起使用就是一個英文指令。比如從英語語法來分析的話,操作碼是動詞,操作數是賓語。比如這個句子 "},{"type":"codeinline","content":[{"type":"text","text":"Give me money"}]},{"type":"text","text":"這個英文指令的話,Give 就是操作碼,me 和 money 就是操作數。彙編語言中存在多個操作數的情況,要用逗號把它們分割,就像是 Give me,money 這樣。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"能夠使用何種形式的操作碼,是由 CPU 的種類決定的,下面對操作碼的功能進行了整理。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/80/80b9a0b64bda4fc985fc5f6a2ab02f30.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本地代碼需要加載到內存後才能運行,內存中存儲着構成本地代碼的指令和數據。程序運行時,CPU會從內存中把數據和指令讀出來,然後放在 CPU 內部的寄存器中進行處理。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c0/c0f1049d8409487a277f6498054a2583.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果 CPU 和內存的關係你還不是很瞭解的話,請閱讀作者的另一篇文章 "},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s?_biz=MzU2NDg0OTgyMA==&mid=2247484585&idx=1&sn=0d6c3ccf8cc5bec2fea80eb437213801&chksm=fc45f95acb32704c7dcc952a803e88e8a9b0e67c86d8c27abf4e6c776e48b1fcd770dd2dcc8d&token=653889808&lang=zhCN#rd","title":""},"content":[{"type":"text","text":"程序員需要了解的硬核知識之CPU"}]},{"type":"text","text":" 詳細瞭解。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"寄存器是 CPU 中的存儲區域,寄存器除了具有臨時存儲和計算的功能之外,還具有運算功能,x86 系列的主要種類和角色如下圖所示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5d/5da8b8b03164b5341dde5a8ff30a784b.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"指令解析"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面就對 CPU 中的指令進行分析"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"最常用的 mov 指令"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"指令中最常使用的是對寄存器和內存進行數據存儲的 "},{"type":"codeinline","content":[{"type":"text","text":"mov"}]},{"type":"text","text":" 指令,mov 指令的兩個操作數,分別用來指定數據的存儲地和讀出源。操作數中可以指定寄存器、常數、標籤(附加在地址前),以及用方括號"},{"type":"codeinline","content":[{"type":"text","text":"([])"}]},{"type":"text","text":" 圍起來的這些內容。如果指定了沒有用"},{"type":"codeinline","content":[{"type":"text","text":"([])"}]},{"type":"text","text":" 方括號圍起來的內容,就表示對該值進行處理;如果指定了用方括號圍起來的內容,方括號的值則會被解釋爲內存地址,然後就會對該內存地址對應的值進行讀寫操作。讓我們對上面的代碼片段進行說明"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"\tmov ebp,esp\n\tmov eax,dword ptr [ebp+8]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"mov ebp,esp 中,esp 寄存器中的值被直接存儲在了 ebp 中,也就是說,如果 esp 寄存器的值是100的話那麼 ebp 寄存器的值也是 100。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而在 "},{"type":"codeinline","content":[{"type":"text","text":"mov eax,dword ptr [ebp+8]"}]},{"type":"text","text":" 這條指令中,ebp 寄存器的值 + 8 後會被解析稱爲內存地址。如果 ebp"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 寄存器的值是100的話,那麼 eax 寄存器的值就是 100 + 8 的地址的值。"},{"type":"codeinline","content":[{"type":"text","text":"dword ptr"}]},{"type":"text","text":" 也叫做 "},{"type":"codeinline","content":[{"type":"text","text":"double word pointer"}]},{"type":"text","text":" 簡單解釋一下就是從指定的內存地址中讀出4字節的數據"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"對棧進行 push 和 pop"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"程序運行時,會在內存上申請分配一個稱爲棧的數據空間。棧(stack)的特性是後入先出,數據在存儲時是從內存的下層(大的地址編號)逐漸往上層(小的地址編號)累積,讀出時則是按照從上往下進行讀取的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/aa/aacab0fb806a3bd4447beb1f128c2055.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"棧是存儲臨時數據的區域,它的特點是通過 push 指令和 pop 指令進行數據的存儲和讀出。向棧中存儲數據稱爲 "},{"type":"codeinline","content":[{"type":"text","text":"入棧"}]},{"type":"text","text":" ,從棧中讀出數據稱爲 "},{"type":"codeinline","content":[{"type":"text","text":"出棧"}]},{"type":"text","text":",32位 x86 系列的 CPU 中,進行1次 push 或者 pop,即可處理 32 位(4字節)的數據。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"函數的調用機制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面我們一起來分析一下函數的調用機制,我們以上面的 C 語言編寫的代碼爲例。首先,讓我們從"},{"type":"codeinline","content":[{"type":"text","text":"MyFunc"}]},{"type":"text","text":" 函數調用"},{"type":"codeinline","content":[{"type":"text","text":"AddNum"}]},{"type":"text","text":" 函數的彙編語言部分開始,來對函數的調用機制進行說明。棧在函數的調用中發揮了巨大的作用,下面是經過處理後的 MyFunc 函數的彙編處理內容"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_MyFunc \t proc \t near\n\tpush \t\t\tebp\t\t ; 將 ebp 寄存器的值存入棧中 (1) \n\tmov\t\t\t\tebp,esp ; 將 esp 寄存器的值存入 ebp 寄存器中\t (2)\n\tpush\t\t\t456\t\t\t; 將 456 入棧\t\t\t\t\t\t\t\t\t\t\t\t (3)\n\tpush \t\t\t123\t\t\t; 將 123 入棧\t\t\t\t\t\t\t\t\t\t\t\t (4)\n\tcall\t\t\t_AddNum ; 調用 AddNum 函數\t\t\t\t\t\t\t\t\t\t (5)\n\tadd\t\t\t\tesp,8\t\t; esp 寄存器的值 + 8\t\t\t\t\t\t\t\t\t\t(6)\n\tpop\t\t\t\tebp\t\t\t; 讀出棧中的數值存入 esp 寄存器中\t\t\t\t (7)\n\tret \t\t\t\t\t\t\t; 結束 MyFunc 函數,返回到調用源\t\t\t\t\t(8)\n_MyFunc \t\tendp"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代碼解釋中的(1)、(2)、(7)、(8)的處理適用於 C 語言中的所有函數,我們會在後面展示 "},{"type":"codeinline","content":[{"type":"text","text":"AddNum"}]},{"type":"text","text":" 函數處理內容時進行說明。這裏希望大家先關注(3) - (6) 這一部分,這對了解函數調用機制至關重要。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(3) 和 (4) 表示的是將傳遞給 AddNum 函數的參數通過 push 入棧。在 C 語言源代碼中,雖然記述爲函數 AddNum(123,456),但入棧時則會先按照 456,123 這樣的順序。也就是位於後面的數值先入棧。這是 C 語言的規定。(5) 表示的 call 指令,會把程序流程跳轉到 AddNum 函數指令的地址處。在彙編語言中,"},{"type":"codeinline","content":[{"type":"text","text":"函數名"}]},{"type":"text","text":"表示的就是函數所在的內存地址。AddNum 函數處理完畢後,程序流程必須要返回到編號(6) 這一行。call 指令運行後,call 指令的下一行(也就指的是 (6) 這一行)的內存地址(調用函數完畢後要返回的內存地址)會自動的 push 入棧。該值會在 AddNum 函數處理的最後通過 "},{"type":"codeinline","content":[{"type":"text","text":"ret"}]},{"type":"text","text":" 指令 pop 出棧,然後程序會返回到 (6) 這一行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(6) 部分會把棧中存儲的兩個參數 (456 和 123) 進行銷燬處理。雖然通過兩次的 pop 指令也可以實現,不過採用 esp 寄存器 + 8 的方式會更有效率(處理 1 次即可)。對棧進行數值的輸入和輸出時,數值的單位是4字節。因此,通過在負責棧地址管理的 esp 寄存器中加上4的2倍8,就可以達到和運行兩次 pop 命令同樣的效果。雖然內存中的數據實際上還殘留着,但只要把 esp 寄存器的值更新爲數據存儲地址前面的數據位置,該數據也就相當於銷燬了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我在編譯 "},{"type":"codeinline","content":[{"type":"text","text":"Sample4.c"}]},{"type":"text","text":" 文件時,出現了下圖的這條消息"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/88/88df26c417d0570d077f908017f2b12b.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"圖中的意思是指 c 的值在 MyFunc 定義了但是一直未被使用,這其實是一項編譯器優化的功能,由於存儲着 AddNum 函數返回值的變量 c 在後面沒有被用到,因此編譯器就認爲 "},{"type":"text","marks":[{"type":"strong"}],"text":"該變量沒有意義"},{"type":"text","text":",進而也就"},{"type":"text","marks":[{"type":"strong"}],"text":"沒有生成與之對應的彙編語言代碼"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下圖是調用 AddNum 這一函數前後棧內存的變化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0b/0b601746e9c4587403180b6c4f587154.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"函數的內部處理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面我們用匯編代碼分析了一下 Sample4.c 整個過程的代碼,現在我們着重分析一下 AddNum 函數的源代碼部分,分析一下參數的接收、返回值和返回等機制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_AddNum \t\tproc\t\tnear\n\tpush\t\t\tebp\t\t\t -----------(1)\n\tmov\t\t\t\tebp,esp -----------(2)\n\tmov\t\t\t\teax,dword ptr[ebp+8] -----------(3)\n\tadd\t\t\t\teax,dword ptr[ebp+12] -----------(4)\n\tpop\t\t\t\tebp\t\t\t\t\t\t\t\t\t\t -----------(5)\n\tret\t\t\t\t----------------------------------(6)\n_AddNum\t\t\tendp"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ebp 寄存器的值在(1)中入棧,在(5)中出棧,這主要是爲了把函數中用到的 ebp 寄存器的內容,恢復到函數調用前的狀態。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(2) 中把負責管理棧地址的 esp 寄存器的值賦值到了 ebp 寄存器中。這是因爲,在 mov 指令中方括號內的參數,是不允許指定 esp 寄存器的。因此,這裏就採用了不直接通過 esp,而是用 ebp 寄存器來讀寫棧內容的方法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(3) 使用[ebp + 8] 指定棧中存儲的第1個參數123,並將其讀出到 eax 寄存器中。像這樣,不使用 pop 指令,也可以參照棧的內容。而之所以從多個寄存器中選擇了 eax 寄存器,是因爲 eax 是負責運算的累加寄存器。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過(4) 的 add 指令,把當前 eax 寄存器的值同第2個參數相加後的結果存儲在 eax 寄存器中。[ebp + 12] 是用來指定第2個參數456的。在 C 語言中,函數的返回值必須通過 eax 寄存器返回,這也是規定。也就是 "},{"type":"text","marks":[{"type":"strong"}],"text":"函數的參數是通過棧來傳遞,返回值是通過寄存器返回的"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(6) 中 ret 指令運行後,函數返回目的地內存地址會"},{"type":"codeinline","content":[{"type":"text","text":"自動出棧"}]},{"type":"text","text":",據此,程序流程就會跳轉返回到"},{"type":"codeinline","content":[{"type":"text","text":"(6) (Call _AddNum)"}]},{"type":"text","text":" 的下一行。這時,AddNum 函數入口和出口處棧的狀態變化,就如下圖所示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2e/2e6e693b6dcfb9052abf097ee1842e12.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"全局變量和局部變量"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在熟悉了彙編語言後,接下來我們來了解一下全局變量和局部變量,在函數外部定義的變量稱爲"},{"type":"codeinline","content":[{"type":"text","text":"全局變量"}]},{"type":"text","text":",在函數內部定義的變量稱爲"},{"type":"codeinline","content":[{"type":"text","text":"局部變量"}]},{"type":"text","text":",全局變量可以在任意函數中使用,局部變量只能在函數定義局部變量的內部使用。下面,我們就通過彙編語言來看一下全局變量和局部變量的不同之處。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面定義的 C 語言代碼分別定義了局部變量和全局變量,並且給各變量進行了賦值,我們先看一下源代碼部分"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"c"},"content":[{"type":"text","text":"// 定義被初始化的全局變量\nint a1 = 1;\nint a2 = 2;\nint a3 = 3;\nint a4 = 4;\nint a5 = 5;\n\n// 定義沒有初始化的全局變量\nint b1,b2,b3,b4,b5;\n\n// 定義函數\nvoid MyFunc(){\n // 定義局部變量\n int c1,c2,c3,c4,c5,c6,c7,c8,c9,c10;\n \n // 給局部變量賦值\n c1 = 1;\n c2 = 2;\n c3 = 3;\n c4 = 4;\n c5 = 5;\n c6 = 6;\n c7 = 7;\n c8 = 8;\n c9 = 9;\n c10 = 10;\n \n // 把局部變量賦值給全局變量\n a1 = c1;\n a2 = c2;\n a3 = c3;\n a4 = c4;\n a5 = c5;\n b1 = c6;\n b2 = c7;\n b3 = c8;\n b4 = c9;\n b5 = c10;\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面的代碼挺暴力的,不過沒關係,能夠便於我們分析其彙編源碼就好,我們用 Borland C++ 編譯後的彙編代碼如下,編譯完成後的源碼比較長,這裏我們只拿出來一部分作爲分析使用(我們改變了一下段定義順序,刪除了部分註釋)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_DATA segment dword public use32 'DATA'\n align 4\n _a1 label dword\n \t\t\tdd 1\n align 4\n _a2 label dword\n \t\t\tdd 2\n align 4\n _a3 label dword\n \t\t\tdd 3\n align 4\n _a4 label dword\n \t\t\tdd 4\n align 4\n _a5 label dword\n \t\t\tdd 5\n_DATA ends\n\n_BSS segment dword public use32 'BSS'\n align 4\n _b1 label dword\n \t\t\tdb 4 dup(?)\n align 4\n _b2 label dword\n \t\t\tdb 4 dup(?)\n align 4\n _b3 label dword\n \t\t\tdb 4 dup(?)\n align 4\n _b4 label dword\n \t\t\tdb 4 dup(?)\n align 4\n _b5 label dword\n \t\t\tdb 4 dup(?)\n_BSS ends\n\n_TEXT segment dword public use32 'CODE'\n_MyFunc proc near\n\n push ebp\n mov ebp,esp\n add esp,-20\n push ebx\n push esi\n mov eax,1\n mov edx,2\n mov ecx,3\n mov ebx,4\n mov esi,5\n mov dword ptr [ebp-4],6\n mov dword ptr [ebp-8],7\n mov dword ptr [ebp-12],8\n mov dword ptr [ebp-16],9\n mov dword ptr [ebp-20],10\n mov dword ptr [_a1],eax\n mov dword ptr [_a2],edx\n mov dword ptr [_a3],ecx\n mov dword ptr [_a4],ebx\n mov dword ptr [_a5],esi\n mov eax,dword ptr [ebp-4]\n mov dword ptr [_b1],eax\n mov edx,dword ptr [ebp-8]\n mov dword ptr [_b2],edx\n mov ecx,dword ptr [ebp-12]\n mov dword ptr [_b3],ecx\n mov eax,dword ptr [ebp-16]\n mov dword ptr [_b4],eax\n mov edx,dword ptr [ebp-20]\n mov dword ptr [_b5],edx\n pop esi\n pop ebx\n mov esp,ebp\n pop ebp\n ret\n \n_MyFunc endp\n_TEXT \tends"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"編譯後的程序,會被歸類到名爲段定義的組。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"初始化的全局變量,會彙總到名爲 _DATA 的段定義中"},{"type":"text","text":" "}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_DATA segment dword public use32 'DATA'\n...\n_DATA ends"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"沒有初始化的全局變量,會彙總到名爲 _BSS 的段定義中"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_BSS segment dword public use32 'BSS'\n ...\n_BSS ends"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"被段定義 _TEXT 圍起來的彙編代碼則是 Borland C++ 的定義"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_TEXT segment dword public use32 'CODE'\n_MyFunc proc near\n...\n_MyFunc endp\n_TEXT \tends"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們在分析上面彙編代碼之前,先來認識一下更多的彙編指令,此表是對上面"},{"type":"text","marks":[{"type":"strong"}],"text":"部分操作碼及其功能"},{"type":"text","text":"的接續"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 操作碼 | 操作數 | 功能 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ------ | ------ | ---------------------------------------------- |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| add | A,B | 把A和B的值相加,並把結果賦值給A |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| call | A | 調用函數A |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| cmp | A,B | 對A和B進行比較,比較結果會自動存入標誌寄存器中 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| inc | A | 對A的值 + 1 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ige | 標籤名 | 和 cmp 命令組合使用。跳轉到標籤行 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| jl | 標籤名 | 和 cmp 命令組合使用。跳轉到標籤行 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| jle | 標籤名 | 和 cmp 命令組合使用。跳轉到標籤行 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| jmp | 標籤名 | 和 cmp 命令組合使用。跳轉到標籤行 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| mov | A,B | 把 B 的值賦給 A |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| pop | A | 從棧中讀取數值並存入A |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| push | A | 把A的值存入棧中 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ret | 無 | 將處理返回到調用源 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| xor | A,B | A和B的位進行亦或比較,並將結果存入A中 |"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們首先來看一下 "},{"type":"codeinline","content":[{"type":"text","text":"_DATA"}]},{"type":"text","text":" 段定義的內容。"},{"type":"codeinline","content":[{"type":"text","text":" _a1 label dword"}]},{"type":"text","text":" 定義了 "},{"type":"codeinline","content":[{"type":"text","text":"_a1"}]},{"type":"text","text":" 這個標籤。標籤表示的是相對於段定義起始位置的位置。由於"},{"type":"codeinline","content":[{"type":"text","text":"_a1"}]},{"type":"text","text":" 在 "},{"type":"codeinline","content":[{"type":"text","text":"_DATA 段"}]},{"type":"text","text":"定義的開頭位置,所以相對位置是0。 "},{"type":"codeinline","content":[{"type":"text","text":"_a1"}]},{"type":"text","text":" 就相當於是全局變量a1。編譯後的函數名和變量名前面會加一個"},{"type":"codeinline","content":[{"type":"text","text":"(_)"}]},{"type":"text","text":",這也是 Borland C++ 的規定。"},{"type":"codeinline","content":[{"type":"text","text":"dd 1"}]},{"type":"text","text":" 指的是,申請分配了4字節的內存空間,存儲着1這個初始值。 dd指的是 "},{"type":"codeinline","content":[{"type":"text","text":"define double word "}]},{"type":"text","text":"表示有兩個長度爲2的字節領域(word),也就是4字節的意思。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Borland C++ 中,由於"},{"type":"codeinline","content":[{"type":"text","text":"int"}]},{"type":"text","text":" 類型的長度是4字節,因此彙編器就把 int a1 = 1 變換成了 "},{"type":"codeinline","content":[{"type":"text","text":"_a1 label dword 和 dd 1"}]},{"type":"text","text":"。同樣,這裏也定義了相當於全局變量的 a2 - a5 的標籤 "},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"italic"}],"text":"a2 - "},{"type":"text","text":"a5"}]},{"type":"text","text":",它們各自的初始值 2 - 5 也被存儲在各自的4字節中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來,我們來說一說 "},{"type":"codeinline","content":[{"type":"text","text":"_BSS"}]},{"type":"text","text":" 段定義的內容。這裏定義了相當於全局變量 b1 - b5 的標籤 "},{"type":"codeinline","content":[{"type":"text","text":"_b1 - _b5"}]},{"type":"text","text":"。其中的"},{"type":"codeinline","content":[{"type":"text","text":"db 4dup(?)"}]},{"type":"text","text":" 表示的是申請分配了4字節的領域,但值尚未確定(這裏用 ? 來表示)的意思。"},{"type":"codeinline","content":[{"type":"text","text":"db(define byte)"}]},{"type":"text","text":" 表示有1個長度是1字節的內存空間。因而,db 4 dup(?) 的情況下,就是4字節的內存空間。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">注意:db 4 dup(?) 不要和 dd 4 混淆了,前者表示的是4個長度是1字節的內存空間。而 db 4 表示的則是雙字節( = 4 字節) 的內存空間中存儲的值是 4 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"臨時確保局部變量使用的內存空間"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們知道,局部變量是臨時保存在寄存器和棧中的。函數內部利用棧進行局部變量的存儲,函數調用完成後,局部變量值被銷燬,但是寄存器可能用於其他目的。所以,"},{"type":"text","marks":[{"type":"strong"}],"text":"局部變量只是函數在處理期間臨時存儲在寄存器和棧中的"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"回想一下上述代碼是不是定義了10個局部變量?這是爲了表示存儲局部變量的不僅僅是棧,還有寄存器。爲了確保 c1 - c10 所需的域,寄存器空閒的時候就會使用寄存器,寄存器空間不足的時候就會使用棧。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"讓我們繼續來分析上面代碼的內容。"},{"type":"codeinline","content":[{"type":"text","text":"_TEXT"}]},{"type":"text","text":"段定義表示的是 "},{"type":"codeinline","content":[{"type":"text","text":"MyFunc"}]},{"type":"text","text":" 函數的範圍。在 MyFunc 函數中定義的局部變量所需要的內存領域。會被儘可能的分配在寄存器中。大家可能認爲使用高性能的寄存器來替代普通的內存是一種資源浪費,但是編譯器不這麼認爲,只要寄存器有空間,編譯器就會使用它。由於寄存器的訪問速度遠高於內存,所以直接訪問寄存器能夠高效的處理。局部變量使用寄存器,是 Borland C++ 編譯器最優化的運行結果。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代碼清單中的如下內容表示的是向寄存器中分配局部變量的部分"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"mov eax,1\nmov edx,2\nmov ecx,3\nmov ebx,4\nmov esi,5"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"僅僅對局部變量進行定義是不夠的,只有在給局部變量賦值時,纔會被分配到寄存器的內存區域。上述代碼相當於就是給5個局部變量 c1 - c5 分別賦值爲 1 - 5。"},{"type":"text","marks":[{"type":"strong"}],"text":"eax、edx、ecx、ebx、esi"},{"type":"text","text":" 是 x86 系列32位 CPU 寄存器的名稱。至於使用哪個寄存器,是由"},{"type":"codeinline","content":[{"type":"text","text":"編譯器"}]},{"type":"text","text":"來決定的 。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"x86 系列 CPU 擁有的寄存器中,程序可以操作的是十幾,其中空閒的最多會有幾個。因而,局部變量超過寄存器數量的時候,可分配的寄存器就不夠用了,這種情況下,編譯器就會把棧派上用場,用來存儲剩餘的局部變量。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上述代碼這一部分,給局部變量c1 - c5 分配完寄存器後,可用的寄存器數量就不足了。於是,剩下的5個局部變量c6 - c10 就被分配給了棧的內存空間。如下面代碼所示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"mov dword ptr [ebp-4],6\nmov dword ptr [ebp-8],7\nmov dword ptr [ebp-12],8\nmov dword ptr [ebp-16],9\nmov dword ptr [ebp-20],10"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"函數入口 "},{"type":"codeinline","content":[{"type":"text","text":"add esp,-20"}]},{"type":"text","text":" 指的是,對棧數據存儲位置的 esp 寄存器(棧指針)的值做減20的處理。爲了確保內存變量 c6 - c10 在棧中,就需要保留5個 int 類型的局部變量(4字節 * 5 = 20 字節)所需的空間。"},{"type":"codeinline","content":[{"type":"text","text":" mov ebp,esp"}]},{"type":"text","text":"這行指令表示的意思是將 esp 寄存器的值賦值到 ebp 寄存器。之所以需要這麼處理,是爲了通過在函數出口處 "},{"type":"codeinline","content":[{"type":"text","text":"mov esp ebp"}]},{"type":"text","text":" 這一處理,把 esp 寄存器的值還原到原始狀態,從而對申請分配的棧空間進行釋放,這時棧中用到的局部變量就消失了。這也是棧的清理處理。在使用寄存器的情況下,局部變量則會在寄存器被用於其他用途時自動消失,如下圖所示。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f9/f99b4efdf35128db350159254e04fb01.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":" mov dword ptr [ebp-4],6\n mov dword ptr [ebp-8],7\n mov dword ptr [ebp-12],8\n mov dword ptr [ebp-16],9\n mov dword ptr [ebp-20],10"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這五行代碼是往棧空間代入數值的部分,由於在向棧申請內存空間前,藉助了 "},{"type":"codeinline","content":[{"type":"text","text":"mov ebp, esp"}]},{"type":"text","text":" 這個處理,esp 寄存器的值被保存到了 esp 寄存器中,因此,通過使用[ebp - 4]、[ebp - 8]、[ebp - 12]、[ebp - 16]、[ebp - 20] 這樣的形式,就可以申請分配20字節的棧內存空間切分成5個長度爲4字節的空間來使用。例如,"},{"type":"codeinline","content":[{"type":"text","text":" mov dword ptr [ebp-4],6"}]},{"type":"text","text":" 表示的就是,從申請分配的內存空間的下端(ebp寄存器指示的位置)開始向前4字節的地址([ebp - 4]) 中,存儲着6這一4字節數據。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f5/f50b7bbca71436c92fb3376e01ee6717.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"循環控制語句的處理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面說的都是順序流程,那麼現在就讓我們分析一下循環流程的處理,看一下 "},{"type":"codeinline","content":[{"type":"text","text":"for 循環"}]},{"type":"text","text":"以及 "},{"type":"codeinline","content":[{"type":"text","text":"if 條件分支"}]},{"type":"text","text":"等 c 語言程序的 "},{"type":"codeinline","content":[{"type":"text","text":"流程控制"}]},{"type":"text","text":"是如何實現的,我們還是以代碼以及編譯後的結果爲例,看一下程序控制流程的處理過程。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"c"},"content":[{"type":"text","text":"// 定義MySub 函數\nvoid MySub(){\n // 不做任何處理\n \n}\n\n// 定義MyFunc 函數\nvoid Myfunc(){\n int i;\n for(int i = 0;i < 10;i++){\n // 重複調用MySub十次\n MySub();\n }\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上述代碼將局部變量 i 作爲循環條件,循環調用十次"},{"type":"codeinline","content":[{"type":"text","text":"MySub"}]},{"type":"text","text":" 函數,下面是它主要的彙編代碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"\t\txor \t\tebx, ebx \t; 將寄存器清0\n@4 call\t\t_MySub\t\t; 調用MySub函數\n\t\tinc\t\t\tebx\t\t\t\t; ebx寄存器的值 + 1\n\t\tcmp\t\t\tebx,10\t\t;\t將ebx寄存器的值和10進行比較\n\t\tjl\t\t\tshort @4\t; 如果小於10就跳轉到 @4"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C 語言中的 for 語句是通過在括號中指定循環計數器的初始值(i = 0)、循環的繼續條件(i < 10)、循環計數器的更新(i++) 這三種形式來進行循環處理的。與此相對的彙編代碼就是通過"},{"type":"codeinline","content":[{"type":"text","text":"比較指令(cmp)"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"跳轉指令(jl)"}]},{"type":"text","text":"來實現的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面我們來對上述代碼進行說明"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"MyFunc"}]},{"type":"text","text":" 函數中用到的局部變量只有 i ,變量 i 申請分配了 ebx 寄存器的內存空間。for 語句括號中的 i = 0 被轉換爲 "},{"type":"codeinline","content":[{"type":"text","text":"xor ebx,ebx"}]},{"type":"text","text":" 這一處理,xor 指令會對左起第一個操作數和右起第二個操作數進行 XOR 運算,然後把結果存儲在第一個操作數中。由於這裏把第一個操作數和第二個操作數都指定爲了 ebx,因此就變成了對相同數值的 XOR 運算。也就是說不管當前寄存器的值是什麼,最終的結果都是0。類似的,我們使用 "},{"type":"codeinline","content":[{"type":"text","text":"mov ebx,0"}]},{"type":"text","text":" 也能得到相同的結果,但是 xor 指令的處理速度更快,而且編譯器也會啓動最優化功能。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">XOR 指的就是異或操作,它的運算規則是 "},{"type":"text","marks":[{"type":"strong"}],"text":"如果a、b兩個值不相同,則異或結果爲1。如果a、b兩個值相同,異或結果爲0"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">相同數值進行 XOR 運算,運算結果爲0。XOR 的運算規則是,值不同時結果爲1,值相同時結果爲0。例如 01010101 和 01010101 進行運算,就會分別對各個數字位進行 XOR 運算。因爲每個數字位都相同,所以運算結果爲0。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ebx 寄存器的值初始化後,會通過 call 指定調用 "},{"type":"text","marks":[{"type":"italic"}],"text":"MySub 函數,從 "},{"type":"text","text":"MySub 函數返回後,會執行"},{"type":"codeinline","content":[{"type":"text","text":"inc ebx"}]},{"type":"text","text":" 指令,對 ebx 的值進行 + 1 操作,這個操作就相當於 i++ 的意思,++ 表示的就是當前數值 + 1。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">這裏需要知道 i++ 和 ++i 的區別"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">i++ 是先賦值,複製完成後再對 i執行 + 1 操作"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">++i 是先進行 +1 操作,完成後再進行賦值"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"inc"}]},{"type":"text","text":" 下一行的 "},{"type":"codeinline","content":[{"type":"text","text":"cmp"}]},{"type":"text","text":" 是用來對第一個操作數和第二個操作數的數值進行比較的指令。 "},{"type":"codeinline","content":[{"type":"text","text":"cmp ebx,10"}]},{"type":"text","text":" 就相當於 C 語言中的 i < 10 這一處理,意思是把 ebx 寄存器的值與10進行比較。彙編語言中比較指令的結果,會存儲在 CPU 的標誌寄存器中。不過,標誌寄存器的值,程序是無法直接參考的。那如何判斷比較結果呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"彙編語言中有多個"},{"type":"codeinline","content":[{"type":"text","text":"跳轉指令"}]},{"type":"text","text":",這些跳轉指令會根據標誌寄存器的值來判斷是否進行跳轉操作,例如最後一行的 jl,它會根據 cmp ebx,10 指令所存儲在標誌寄存器中的值來判斷是否跳轉,"},{"type":"codeinline","content":[{"type":"text","text":"jl"}]},{"type":"text","text":" 這條指令表示的就是 "},{"type":"codeinline","content":[{"type":"text","text":"jump on less than(小於的話就跳轉)"}]},{"type":"text","text":"。發現如果 i 比 10 小,就會跳轉到 @4 所在的指令處繼續執行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼彙編代碼的意思也可以用 C 語言來改寫一下,加深理解"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"c"},"content":[{"type":"text","text":"\t\ti ^= i;\nL4: MySub();\n\t\ti++;\n\t\tif(i < 10) goto L4;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代碼第一行 i ^= i 指的就是 i 和 i 進行異或運算,也就是 XOR 運算,MySub() 函數用 L4 標籤來替代,然後進行 i 自增操作,如果i 的值小於 10 的話,就會一直循環 MySub() 函數。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"條件分支的處理方法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"條件分支的處理方式和循環的處理方式很相似,使用的也是 cmp 指令和跳轉指令。下面是用 C 語言編寫的條件分支的代碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"c"},"content":[{"type":"text","text":"// 定義MySub1 函數\nvoid MySub1(){\n\n // 不做任何處理\n}\n\n// 定義MySub2 函數\nvoid MySub2(){\n \n // 不做任何處理\n}\n\n// 定義MySub3 函數\nvoid MySub3(){\n\n // 不做任何處理\n}\n\n// 定義MyFunc 函數\nvoid MyFunc(){\n\n int a = 123;\n // 根據條件調用不同的函數\n if(a > 100){\n MySub1();\n }\n else if(a < 50){\n MySub2();\n }\n else\n {\n MySub3();\n }\n\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"很簡單的一個實現了條件判斷的 C 語言代碼,那麼我們把它用 Borland C++ 編譯之後的結果如下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"_MyFunc proc near\n push ebp \t\t\t\t\n mov ebp,esp\n mov eax,123\t\t\t; 把123存入 eax 寄存器中\n cmp eax,100\t\t\t; 把 eax 寄存器的值同100進行比較\n jle short @8\t\t\t; 比100小時,跳轉到@8標籤\n call _MySub1\t\t\t; 調用MySub1函數\n jmp \t\t\t short @11 \t\t; 跳轉到@11標籤\n@8:\n cmp eax,50\t\t\t\t; 把 eax 寄存器的值同50進行比較\n jge short @10\t\t; 比50大時,跳轉到@10標籤\n call _MySub2\t\t\t; 調用MySub2函數\n jmp\t\t\t short @11\t\t; 跳轉到@11標籤\n@10:\n call _MySub3\t\t\t; 調用MySub3函數\n@11:\n pop ebp\n ret \n_MyFunc endp"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面代碼用到了三種跳轉指令,分別是"},{"type":"codeinline","content":[{"type":"text","text":"jle(jump on less or equal)"}]},{"type":"text","text":" 比較結果小時跳轉,"},{"type":"codeinline","content":[{"type":"text","text":"jge(jump on greater or equal)"}]},{"type":"text","text":" 比較結果大時跳轉,還有不管結果怎樣都會進行跳轉的"},{"type":"codeinline","content":[{"type":"text","text":"jmp"}]},{"type":"text","text":",在這些跳轉指令之前還有用來比較的指令 "},{"type":"codeinline","content":[{"type":"text","text":"cmp"}]},{"type":"text","text":",構成了上述彙編代碼的主要邏輯形式。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"瞭解程序運行邏輯的必要性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過對上述彙編代碼和 C 語言源代碼進行比較,想必大家對程序的運行方式有了新的理解,而且,從彙編源代碼中獲取的知識,也有助於瞭解 Java 等高級語言的特性,比如 Java 中就有 native 關鍵字修飾的變量,那麼這個變量的底層就是使用 C 語言編寫的,還有一些 Java 中的語法糖只有通過彙編代碼才能知道其運行邏輯。在某些情況下,對於查找 bug 的原因也是有幫助的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面我們瞭解到的編程方式都是串行處理的,那麼串行處理有什麼特點呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fc/fce9059e59f6667dfd7f3c27b973dd89.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"串行處理最大的一個特點就是"},{"type":"codeinline","content":[{"type":"text","text":"專心只做一件事情"}]},{"type":"text","text":",一件事情做完之後纔會去做另外一件事情。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"計算機是支持多線程的,多線程的核心就是 CPU切換,如下圖所示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f2/f2ed184f87f0f7c142749ce82bfbd78e.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們還是舉個實際的例子,讓我們來看一段代碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"c"},"content":[{"type":"text","text":"// 定義全局變量\nint counter = 100;\n\n// 定義MyFunc1()\nvoid MyFunc(){\n counter *= 2;\n}\n\n// 定義MyFunc2()\nvoid MyFunc2(){\n counter *= 2;\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上述代碼是更新 counter 的值的 C 語言程序,MyFunc1() 和 MyFunc2() 的處理內容都是把 counter 的值擴大至原來的二倍,然後再把 counter 的值賦值給 counter 。這裏,我們假設使用"},{"type":"codeinline","content":[{"type":"text","text":"多線程處理"}]},{"type":"text","text":",同時調用了一次MyFunc1 和 MyFunc2 函數,這時,全局變量 counter 的值,理應編程 100 "},{"type":"text","marks":[{"type":"italic"}],"text":" 2 "},{"type":"text","text":" 2 = 400。如果你開啓了多個線程的話,你會發現 counter 的數值有時也是 200,對於爲什麼出現這種情況,如果你不瞭解程序的運行方式,是很難找到原因的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們將上面的代碼轉換成彙編語言的代碼如下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"asm"},"content":[{"type":"text","text":"mov eax,dword ptr[_counter] \t; 將 counter 的值讀入 eax 寄存器\nadd eax,eax\t\t\t\t\t\t\t\t\t\t; 將 eax 寄存器的值擴大2倍。\nmov dword ptr[_counter],eax\t\t; 將 eax 寄存器的值存入 counter 中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在多線程程序中,用彙編語言表示的代碼每運行一行,處理都有可能切換到其他線程中。因而,假設 MyFun1 函數在讀出 counter 數值100後,還未來得及將它的二倍值200寫入 counter 時,正巧 MyFun2 函數讀出了 counter 的值100,那麼結果就將變爲 200 。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c0/c0d6ed92b6ee78c3a0b4012f792526f2.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了避免該bug,我們可以採用以函數或 C 語言代碼的行爲單位來禁止線程切換的"},{"type":"codeinline","content":[{"type":"text","text":"鎖定"}]},{"type":"text","text":"方法,或者使用某種線程安全的方式來避免該問題的出現。\t"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在基本上沒有人用彙編語言來編寫程序了,因爲 C、Java等高級語言的效率要比彙編語言快很多。不過,彙編語言的經驗還是很重要的,通過藉助彙編語言,我們可以更好的瞭解計算機運行機制。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3b/3b132b9df8ffc8d72ed0fc04a1704dfa.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章