简体   繁体   English

数据如何进入通用寄存器和其他寄存器,以便 CPU 可以使用它来计算值?

[英]How does data get into the General Purpose and other registers so that the CPU can use it for computing values?

I know that registers serve as storage units for the CPU to access data from in order to execute an instruction.我知道寄存器用作 CPU 访问数据以执行指令的存储单元。 The assembly language for these instructions look something like ADD R2, R1, R3 essentially asking us to add contents in R1 and R3 and place it in R2.这些指令的汇编语言类似于 ADD R2, R1, R3,本质上是要求我们在 R1 和 R3 中添加内容并将其放入 R2。 My question is, how does data get into the registers R1 and R3 so that the CPU can use those values to compute and store the result in R2?我的问题是,数据如何进入寄存器 R1 和 R3,以便 CPU 可以使用这些值来计算结果并将结果存储在 R2 中? And if all registers get full, is data evicted to main memory from the registers using an LRU method similar to how data from caches are evicted?如果所有寄存器都已满,是否使用类似于从缓存中清除数据的 LRU 方法将数据从寄存器中清除到主 memory?

Values get both into and out of registers using machine code instructions;值使用机器代码指令进出寄存器; machine code instructions can:机器代码指令可以:

  • Enter constants from the program into registers or memory将程序中的常数输入寄存器或 memory
    • These are usually called load immediate or move这些通常称为立即加载或移动
  • Enter user input like keystrokes into registers or memory将用户输入(如击键)输入寄存器或 memory
    • some processors have input instructions, others load from memory mapped I/O一些处理器有输入指令,其他从 memory 映射 I/O 加载
  • Load data from memory, where long-lived variables & data structures are located从 memory 加载数据,长寿命变量和数据结构所在的位置
  • Send register values to memory将寄存器值发送到 memory
    • to update data structures and other long-lived variables更新数据结构和其他长期存在的变量
  • Send data to an output device (like a console)将数据发送到 output 设备(如控制台)
  • Compute new values given existing values给定现有值计算新值

And if all registers get full如果所有寄存器都满了

It depends somewhat on what you mean by full.这在某种程度上取决于您所说的完整。 In one sense, the registers always hold values, there's no notion in the CPU of free / busy registers (modulo some deep concepts on floating point and vector registers) .从某种意义上说,寄存器始终保存值, CPU中没有空闲/忙碌寄存器的概念(以浮点和向量寄存器的一些深层概念为模) Similar is true for your hard drive.您的硬盘驱动器也是如此。 It has exactly N gigabytes of storage, as far as the hardware is concerned, this number never grows or shrinks.它正好有 N GB 的存储空间,就硬件而言,这个数字永远不会增长或缩小。

And if all registers get full, is data evicted to main memory from the registers using an LRU method similar to how data from caches are evicted?如果所有寄存器都已满,是否使用类似于从缓存中清除数据的 LRU 方法将数据从寄存器中清除到主 memory?

Yes, but virtually 100% under program control: the program knows what logical variables from our algorithms are in what CPU registers (and others in what memory locations), so there is a notion of an algorithm translated to assembly having either sufficient registers (leaving some unused) and also of wanting more registers than are available.是的,但几乎 100% 在程序控制下:程序知道我们算法中的哪些逻辑变量在 CPU 寄存器中(以及其他在 memory 位置),因此有一个算法转换为具有足够寄存器的程序集的概念(留下一些未使用的)并且还想要比可用的更多的寄存器。 When that happens (desiring more than available), compiler writers and assembly programmers simply turn to memory for the overflow, writing machine code instructions to transfer data back & forth as needed.当发生这种情况时(希望超过可用的),编译器编写者和汇编程序员只需转向 memory 进行溢出,编写机器代码指令以根据需要来回传输数据。 Unimportant things can live in memory and suffer slower access times, allowing the CPU registers to be used as desired.不重要的事情可以存在于 memory 中,并且访问时间变慢,从而可以根据需要使用 CPU 寄存器。

(In the hard drive analogy, all the bits are always there but not always in meaningful use.) (在硬盘驱动器的类比中,所有位始终存在,但并不总是有意义的使用。)

To be clear, the hardware has many LRU algorithms, but these generally surround cache architectures, including L1, L2, and TLB.需要明确的是,硬件有许多 LRU 算法,但这些通常围绕缓存架构,包括 L1、L2 和 TLB。

General Purpose Registers are very similar in concept to variables in a programming language通用寄存器在概念上与编程语言中的变量非常相似

R1 = 3
R3 = 5
R2 = R1 + R3

How you get those values into the registers is specific to the architecture and the value.如何将这些值放入寄存器取决于架构和值。 Some instruction sets have variable length instructions and can support any possible value一些指令集具有可变长度指令,可以支持任何可能的值

mov r1,0x12345678

Others can load half a register别人可以加载半个寄存器

lui r1,0x12340000 

Not proper syntax for a real processor, but the ISA would allow for control over the top half then you would follow it with an对于真正的处理器来说语法不正确,但是 ISA 将允许控制上半部分,然后您将使用

ori r1,0x5678

And the end result is r1 = 0x12345678最终结果是 r1 = 0x12345678

Another solution is to load from a nearby location另一种解决方案是从附近的位置加载

ldr r1,hello
...
hello: .word 0x12345678

This would use pc-relative addressing, the assembler would do the math for you when it generates the instruction, but for example the actual instruction would be something like load from memory at the address of pc+16 into the register r1.这将使用 pc 相对寻址,汇编程序会在生成指令时为您进行数学运算,但例如实际指令将类似于从 pc+16 地址的 memory 加载到寄存器 r1 中。

Registers are discrete and there is a fixed number of them, they are not cached or a fifo or anything like that.寄存器是离散的,它们的数量是固定的,它们没有被缓存或先进先出或类似的东西。

From a programmer or compiler perspective, you can run out of them if you only have say 8 registers, and you are managing more than 8 data items, variables, addresses, etc. AND you are using an architecture that requires you to use registers for everything, then you would typically use the stack to temporarily store variables.从程序员或编译器的角度来看,如果您只有 8 个寄存器,并且您管理的数据项、变量、地址等超过 8 个,并且您使用的架构要求您使用寄存器一切,那么您通常会使用堆栈来临时存储变量。

r1 = 0x12345678  (variable hello)
r2 = 0x55443366  (variable world)
...
I need another variable as the programmer I decide that I can live with out hello for a while
push {r1}
r1 = r2+r3     (variable foo)
str r1,[r4]    (the computed value was generated to store in memory)
foo is no longer neaded, but I need hello back
pop {r1}

Compilers that take a higher level language and generate a lower level language, often assembly language or machine code, will track variables through a function and select which registers to use when.采用高级语言并生成低级语言(通常是汇编语言或机器代码)的编译器将通过 function 和 select 来跟踪变量,这些寄存器在何时使用。 A particular register may through the course of the execution of a function may hold several variables or addresses or other data...obviously only one thing at a time.在 function 的执行过程中,一个特定的寄存器可能会保存多个变量或地址或其他数据……显然一次只能保存一件事。 The same thing we would do as humans by hand, but according to an algorithm.我们会像人类一样用手做同样的事情,但要根据算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM