# X64 - Computer Registers [TOC] ## References ### [Arch100X 02 MemoryHierarchy](https://youtu.be/Zph6GkevMzo) {%youtube Zph6GkevMzo %} ### [Arch1001 01 Regs1 GP Regs](https://youtu.be/T9FUYHrnLUM) {%youtube T9FUYHrnLUM %} ### [Arch1001 01 Regs2 RegConventions](https://youtu.be/_HOfsOFhFxw) {%youtube _HOfsOFhFxw %} ### [x86 Assembly | CSAW CTF "A Tour of x86"](https://youtu.be/xnGNStPg4GI) {%youtube xnGNStPg4GI %} ## x86-64 general purpose registers ![](https://i.imgur.com/NBlLvY2.png) (*Intel, 3-6 Vol. 1*) 這裡的內容主要在 Intel 手冊的 *Vol. 1* 的 *CHAPTER 3 BASIC EXECUTION ENVIRONMENT* 中。首先,影片中提到裡面有 16 個 *GPR*,這個紀錄在 *Vol. 1 3-5* 中: > ***Basic program execution registers** — The number of general-purpose registers (GPRs) available is 16. GPRs are 64-bits wide and they support operations on byte, word, doubleword and quadword integers...* 而剩下的 *GPR* 相關敘述主要是在 *3.4.1 General-Purpose Registers* 中。 ## 暫存器命名: ### 8008 - 8 位元 `A` 暫存器 在 [8008](https://en.wikipedia.org/wiki/Intel_8008) 處理器中有 `A` ~ `E` 等 8 位元的暫存器。 ![](https://i.imgur.com/9LfjJ3E.png) ### 8086 - 16 位元 `AX` 暫存器 到了 [8086](https://en.wikipedia.org/wiki/Intel_8086),GPR 變成 16 位元,並且多了 `SI`、`DI`、`RP`、`RB` 等暫存器: ![](https://i.imgur.com/i7OgVwL.png) 延伸後的暫存器名稱加上了 `X` 後綴,或可理解為 *extended*。而這個 `AX` 的最低 8 位元可以使用 `AL` 存取,地位類似與原先的 A 暫存器; 而最高的 8 位元則使用 `AH` (L 與 H 後綴分別是 *Low* 與 *High*)。 ### 80286 - 32 位元 `EAX` 暫存器 類似的道理,在 32 位元的架構中延伸為 `EAX` (或可理解為 *extended AX*)。而這時可以使用 `AX` 存取最低的 16 位元 ![](https://i.imgur.com/k78mewx.png) *(Intel, 3-12 Vol. 1)* ### AMD Operton/Intel Pentium 4 - 64 位元 `RAX` 類似,這時 `EAX` 是指 `RAX` 的最低 32 位元、`AX` 是指 `RAX` 最低的 16 位元,而 `AL` 與 `AH` 則是指 `RAX` 的最低 8 位元與次低 8 位元。 在 AMD 的文件中有不同的命名傳統,GPR 是用 `R<N>` 形式的命名,其中 `<N>` 為一個非負整數。舉例來說,AMD 文件中的 `R0` 在 Intel 文件中的 `RAX` 表示。 *24592—Rev. 3.23—October 2020, p.27* ![](https://i.imgur.com/LqsJHkP.png) > 除此之外,對於 `RSI`、`RDI`、`RBP`、`RSP` 這四個暫存器,現在也可以藉由類似的機制存取最低的 8、16、32 位元。 ## Intel recommended register conventions 在 Intel 手冊中的 *3-12 Vol. 1* 中,有提到暫存器使用的「傳統」。 ### `RAX` --- 回傳值 *Accumulator for operands and results data* 通常用於存放函式回傳值。另外,如果某些指令的運算元中,應該指定暫存器的部分沒有指定暫存器時,通常就是以 `RAX` 作為運算元。 ### `RSI`, `RDI`, `RCX` --- 字串操作 在操作兩個字串相關運算時,`SI` 通常用於存放來源字串的位址: *...source pointer for string operations* 而 `DI` 通常用於存放目的地字串的位址: *...destination pointer for string operations* 而 `CX` 則用於迴圈的 *index*: *destination pointer for string operations* ### `RSP`, `RBP` --- Stack 頂端與起始位置 命名上來看,`SP` 可理解為 *stack pointer* 的縮寫。手冊中的說法是: *Stack pointer (in the SS segment)* 而 `BP` 則可理解為 *stack Base Pointer* 的縮寫: *Pointer to data on the stack (in the SS segment)* ### `RIP` --- Instruction Pointer `IP` 可理解為 *Instruction Pointer* 的縮寫。對於 32 位元的 `EIP` 有以下說明 (Intel, Vol. 1 3-11): *The EIP register contains a 32-bit pointer to the next instruction to be executed.*