CPU中寄存器是如何设计的
How is the register designed in a CPU
我知道有一些寄存器属于进程。
我有一些关于它们的基本问题:
当我使用gdb
调试一些项目时,我可以看到函数的参数被压入了一些寄存器。那么每个函数都有自己的参数寄存器吗?还是同一进程中的所有函数为其参数共享相同的寄存器?
这些寄存器是否在线程之间共享?进程之间?如果不是,当CPU进行上下文切换时,其他寄存器中的值如何存储?
您似乎缺乏基本的了解或 CPU 的工作原理。阅读计算机体系结构书籍,例如“Computer Organization and Design: the Hardware/Software Interface" by Patterson and Hennessy, or watching Ben Eater's "Build an 8-bit computer from scratch" video series 可能会有所帮助。
I can see that the arguments of functions are pushed into some registers.
有许多不同的调用约定。 加载参数到特定的寄存器(你不能push任何东西到寄存器),或者pushing参数到堆栈上很常见。
So does each function have its own registers
没有。处理器有一组固定的寄存器。如果函数使用例如$RDI
作为它的第一个参数(x86_64
调用约定)并且想要调用其他函数,那么它可能需要在覆盖它之前暂时将 $RDI
的值保存在内存中,以便将参数传递给另一个函数。
Or do all functions in the same process share the same registers for their arguments?
所有进程中的所有函数共享同一组 CPU 寄存器(无论寄存器用于什么)。
Does these registers are shared between threads?
寄存器是共享的,但每个线程都有自己的值寄存器。 OS(或者在某些情况下是线程库)在线程之间切换时保存和恢复值。
between processes?
与线程相同。
If not, when the CPU does a context switch, how do the values in the other registers are stored?
CPU 不进行上下文切换(CPU 没有上下文的概念),OS 通过将当前的一组值存储到一个位置来实现在内存中并从另一个位置加载不同的集合。
我知道有一些寄存器属于进程。
我有一些关于它们的基本问题:
当我使用gdb
调试一些项目时,我可以看到函数的参数被压入了一些寄存器。那么每个函数都有自己的参数寄存器吗?还是同一进程中的所有函数为其参数共享相同的寄存器?
这些寄存器是否在线程之间共享?进程之间?如果不是,当CPU进行上下文切换时,其他寄存器中的值如何存储?
您似乎缺乏基本的了解或 CPU 的工作原理。阅读计算机体系结构书籍,例如“Computer Organization and Design: the Hardware/Software Interface" by Patterson and Hennessy, or watching Ben Eater's "Build an 8-bit computer from scratch" video series 可能会有所帮助。
I can see that the arguments of functions are pushed into some registers.
有许多不同的调用约定。 加载参数到特定的寄存器(你不能push任何东西到寄存器),或者pushing参数到堆栈上很常见。
So does each function have its own registers
没有。处理器有一组固定的寄存器。如果函数使用例如$RDI
作为它的第一个参数(x86_64
调用约定)并且想要调用其他函数,那么它可能需要在覆盖它之前暂时将 $RDI
的值保存在内存中,以便将参数传递给另一个函数。
Or do all functions in the same process share the same registers for their arguments?
所有进程中的所有函数共享同一组 CPU 寄存器(无论寄存器用于什么)。
Does these registers are shared between threads?
寄存器是共享的,但每个线程都有自己的值寄存器。 OS(或者在某些情况下是线程库)在线程之间切换时保存和恢复值。
between processes?
与线程相同。
If not, when the CPU does a context switch, how do the values in the other registers are stored?
CPU 不进行上下文切换(CPU 没有上下文的概念),OS 通过将当前的一组值存储到一个位置来实现在内存中并从另一个位置加载不同的集合。