关于寄存器的一些笔记

 

有次面试时,面试官问我,当一个进程(注意是活动进程而不是程序)hang住时,怎么知道它卡在哪里了?它当时在做什么?当时,我脑子一片糊涂,完全不知道这个问题要怎么回答,甚至包括面试官问我为什么linux命令行上一组用管道连接起来的多个命令能够直接串联起来进行输入输出,每个命令的代码开发时事先并不知道有管道这回事,这个问题都没有回答上来。后来静下心来回想这个问题时,一个进程在执行时一定有上下文,有堆栈,那么查看它的堆栈和上下文应该就能知道它在做什么了。不过即使我这么回答,面试官估计还会追问我具体使用什么工具去查看、如何查看,我还是回答不了,因为我对这些底层的东西只有一点点了解,大概也就知道APUE和操作系统中介绍的那些“理论概念”了,平时调试程序只是依赖日志,对底层并没有深刻的体验,也没有具体的认识。出于对底层的兴趣,也出于技术需要不断学习的认知,决定重拾汇编语言,顺便刷一刷《深入理解计算机系统》。

上面都是废话,下面记一下最近学习汇编语言时个人的一个好奇点,汇编当中少不了和寄存器打交道,寄存器命名在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.

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,好有意思。

不过,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寄存器的意思,但这个说法很明显不像前面那样贴切合理。

posted @ 2018-12-26 17:58  impluse  阅读(921)  评论(0编辑  收藏  举报