關於寄存器的一些筆記

 

有次面試時,面試官問我,當一個進程(注意是活動進程而不是程序)hang住時,怎麼知道它卡在哪裏了?它當時在作什麼?當時,我腦子一片糊塗,徹底不知道這個問題要怎麼回答,甚至包括面試官問我爲何linux命令行上一組用管道鏈接起來的多個命令可以直接串聯起來進行輸入輸出,每一個命令的代碼開發時事先並不知道有管道這回事,這個問題都沒有回答上來。後來靜下心來回想這個問題時,一個進程在執行時必定有上下文,有堆棧,那麼查看它的堆棧和上下文應該就能知道它在作什麼了。不過即便我這麼回答,面試官估計還會追問我具體使用什麼工具去查看、如何查看,我仍是回答不了,由於我對這些底層的東西只有一點點了解,大概也就知道APUE和操做系統中介紹的那些「理論概念」了,平時調試程序只是依賴日誌,對底層並無深入的體驗,也沒有具體的認識。出於對底層的興趣,也出於技術須要不斷學習的認知,決定重拾彙編語言,順便刷一刷《深刻理解計算機系統》。linux

上面都是廢話,下面記一下最近學習彙編語言時我的的一個好奇點,彙編當中少不了和寄存器打交道,寄存器命名在16位是ax,32位是eax,64位是rax,那麼8位CPU時代,8位的寄存器名稱是怎麼樣的?面試

 

雖然16位CPU向後兼容8位CPU,16位寄存器能夠分爲2個8位寄存器,分別叫作ah和al,但很顯然ah、al確定不是8位CPU時代寄存器的名字,由於ah、al是16位時代的命名,而8位比16位出現的要早。最後在stackoverflow中找到其命名,內容以下:函數

The register names have evolved over the past 40 years. The Intel 8080 processor, introdced in 1974, had 8 bit registers named A, B, C, D, E, H and L. A thru E seem fairly obvious but H and L? Well, they were combined into the 16 bit HL register which was primarily used as a memory pointer, so H for high and L for low.工具

In 1979 Intel released the 8086 processor (the original IBM PC was based on its close cousin the 8088). The 8086 had 16 bit registers 4 "main" ones and 4 index registers. The main registers were called AX, BX,CX, DX a natural eXtension of the 8080's A thru D, each of these could also be referenced as 8 bit registers which were called AL, AH, BL, BH, etc. The 8086 index register, also 16 bit, were called SI, DI, BP and SP after their primary functions. SI and DI for Sorce and Destination Index, SP for Stack Pointer, and BP for (stack) Base Pointer.學習

The extension to the 32 bit world, with the introdction of the 80386 in 1986, brought us EAX, EBX, ECX, EDX, ESI, EDI, EBP and ESP, the 32 bit variants of the registers, the 80386 names remained for the (lower) 16 bits and the 8 bit accessing required to mainain comaptibility.ui

There things stood until AMD, beating Intel to market, defined 64-bit extensions.操作系統

It is perhaps interesting to note that binary code assembled for the 8086 processor is compatible with all the X86 processors which succeeded it.命令行

References:調試

http://everything2.com/title/CPU+history%253A+A+timeline+of+microprocessors

http://en.wikipedia.org/wiki/Intel_8080

http://en.wikipedia.org/wiki/Intel_8086

http://en.wikipedia.org/wiki/Intel_80386

 

從上面的回答能夠看到,寄存器名字的歷史涉及40多年的演化,計算機中不少東西都與歷史緣由有關。寄存器也是,8位的寄存器名稱居然就是A、B、C、D,好有意思。rest

不過,A、B、C、D寄存器的名字真的是按字母這麼選擇的嗎?就像數學裏面公式,選擇x和y並無什麼特別的含義?事實上,A、B、C、D寄存器的名字是一種縮寫,全稱是:

A: accumulator A寄存器一般是函數返回值的存放處。

B: base

C: count C寄存器一般是for循環中計數存放處

D: data

當寄存器從8位進化到16位時,命名從A變成了AX,X是什麼意思?下面來自stackoverflow的回答解答了這個問題:

The X means pair, and goes back to at least the 8080. It had 8-bit registers B,C,D,E,H,L (among others) which could also be used in pairs (BC, DE and HL). The BC and DE pairs were used mostly for 16-bit arithmetic; the HL pair generally held a memory address.

X的意思是一對,這個意思比較顯然,由於16位的AX能夠拆分爲2個8位寄存器:AH和AL。

當寄存器從16位進化到32位時,命名從AA變成了EAX,E是什麼意思?這個相關的說明比較容易找到,E表示 "extended" 或 "enhanced",即擴展或者加強的意思。

最後,64位寄存器RAX中的R是什麼意思?網上說就是register寄存器的意思,但這個說法很明顯不像前面那樣貼切合理。

相關文章
相關標籤/搜索