软件在哪里以及如何与硬件相遇? [英] Where and how does software meet hardware?

查看:62
本文介绍了软件在哪里以及如何与硬件相遇?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在计算机的什么地方抽象(屏幕上的字母)符合真实的(通过存储器和处理器中的电路的电流).进化是如何发生的?

Where in a computer does the abstract (letters on the screen) meet the real (electrical current passing through circuits in memory and processors). How did that evolution happen?

我想,我在这里比汇编程序更深入地讨论.也许比处理器指令更深一层?在某些时候,命令实际上是由硬件解释的,但是我不知道发生在哪里/如何发生.

I'm talking deeper than assembler here, I think. Maybe a level deeper than processor instructions? At some point commands are actually interpreted by hardware, but I don't understand where/how that happens.

我没有上大学,所以也没有参加CS课程或其他任何课程.像许多人一样,我是一个业余爱好者,变成了专业人士".结果,我想念了许多拼图.我了解一点C ++,并且了解指针等的概念(尽管我并没有很生气地使用过它们),但是我只是在较高水平上工作过.我不确定是否知道这种东西对我有帮助,但确实很有趣.

I didn't go to university, so didn't take a CS course or anything. Like many, I am a hobbyist turned "pro". As a result, there are many pieces of the jigsaw I think I'm missing. I know a little C++, and understand the concept of pointers etc (though I've not used them in anger much), however I've only ever worked at a high level. I'm not sure knowing this kind've stuff will help me or not, but it's sure interesting.

推荐答案

处理器执行所谓的提取-解码-执行"周期.机器代码指令是相当低级的(即,它们在一条指令中并没有做很多事情).例如,将两个数字相加会得到一系列指令,这些指令的语义如下:

A processor operates what is known as a fetch-decode-execute cycle. Machine code instructions are fairly low-level (i.e. they don't do all that much in a single instruction). For example, adding two numbers would have a sequence of instructions with semantics like:

  • 将指向操作数1地址的指针加载到寄存器1
  • 将存储在寄存器1中的地址中存储的值加载到寄存器2中
  • 将指向操作数2地址的指针加载到寄存器1
  • 将存储在寄存器1中的地址中的值加载到寄存器3中
  • 添加寄存器2和寄存器3的内容并将其存储在寄存器4中
  • 将指向目标的指针加载到寄存器1
  • 将寄存器4的内容存储在寄存器1中指定的地址中

处理器内部有一组特殊的快速存储器,称为寄存器文件",其中包含处理器用来存储当时正在处理的数据的存储器.寄存器文件具有几个唯一标识的寄存器.指令通常在寄存器上工作,尤其是在RISC体系结构上;虽然并非总是如此,但目前它已经足够好抽象了.

Within the processor is a special set of fast memory known as a 'Register File', which contains the memory that the processor uses to store data that it is working on at the time. The register file has several registers, which are uniquely identified. Instructions typically work on registers, especially on RISC architectures; while this is not always the case it is a good enough abstraction for the moment.

通常,处理器必须将数据加载或存储到寄存器中才能对其进行任何处理.诸如对寄存器进行算术运算,从两个寄存器中获取操作数并将结果放入第三个寄存器之类的操作(出于花生画廊的利益,我已经使用了6502-不要混淆这个问题;- ).处理器具有用于将寄存器中的数据加载或存储到机器主存储器中的特殊指令.

Typically a processor has to load or store data into a register to do anything with it. Operations such as arithmetic work on registers, taking the operands from two registers and placing the result into a third (for the benefit of the peanut gallery, I have used a 6502 - lets not confuse the issue ;-). The processor has special instructions for loading or storing data from registers into the machine's main memory.

处理器具有一个称为程序计数器"的特殊寄存器,该寄存器存储要执行的下一个操作的地址.因此,一条指令的执行顺序大致如下:

A processor has a special register called the 'program counter' that stores the address of the next operation to execute. Thus, the sequence for executing an instruction goes roughly like:

  • 在程序计数器中获取存储在当前地址的指令.
  • 对指令进行解码,挑选出实际的操作,使用的寄存器,寻址模式"(如何确定在何处获取或存储数据)以及其他一些内容.
  • 执行指令.

执行指令将更改各个寄存器中的值.例如,加载"指令会将一个值复制到寄存器中.算术或逻辑(与",或",或")将取两个值,然后计算第三个值.跳转或跳转指令将更改程序计数器的地址,因此处理器开始从其他位置获取指令.

Executing the instruction will change the values in various registers. For example, a 'load' instruction will copy a value into a register. An arithmetic or logical (And, Or, Xor) will take two values and compute a third. A jump or branch instruction will change the address at the program counter so the processor starts to fetch instructions from a different location.

处理器可以具有特殊的寄存器.这样的一个例子是上述程序计数器.另一种典型的情况是条件标志寄存器.这将具有一些特殊含义.例如,如果最后一次算术运算的结果为零,则可能设置了一个标志.这对于条件操作很有用.您可以比较两个数字.如果它们相等,则设置零"标志.处理器可以有一个条件指令,只有在设置了此标志的情况下才可以执行.

The processor can have special registers. An example of such is the program counter described above. Another typical one is a condition flags register. This will have several bits with special meanings. For example it may have a flag that is set if the result of the last arithmetic operation was zero. This is useful for conditional operations. You can compare two numbers. If they are equal, the 'zero' flag is set. The processor can have a conditional instruction that is only executed if this flag is set.

在这种情况下,您可以递减寄存器中的计数器,如果计数器为零,则设置条件标志.条件(分支在零上)可用于循环,在该循环中,如果递减指令的结果为零,则递减计数器并退出循环.在某些处理器(例如ARM系列)上,所有指令都是有条件的,对于非条件指令具有特殊的始终执行"条件.

In this case, you could decrement a counter in a register and if it was zero, a condition flag is set. A conditional (branch on zero) can be used for a loop where you decrement a counter and exit the loop if the result of the decrement instruction is zero. On some processors (e.g. the ARM family) all instructions are conditional, with a special 'do always' condition for non-conditional instructions.

一些典型的处理器指令示例是:

Some examples of typical processor instructions are:

  • 增加或减少寄存器
  • 将寄存器的内容加载或存储到内存中.您还可以通过另一个寄存器的内容使要加载或存储的地址偏移.这样,您就可以通过增加另一个寄存器来轻松地遍历数据数组.
  • 加,减,乘,逻辑运算以计算值.它们从两个寄存器中取操作数,然后将结果放在第三个寄存器中.
  • 跳转到另一个位置-这会将位置的内容移动到程序计数器中,并开始从新位置获取指令.
  • 将值推送或弹出到堆栈上.

此stackoverflow帖子中有一个小例子已编译的C代码的片段以及该片段的汇编语言输出.它应该为您提供一个示例,说明高级语言与其所编译的机器代码输出之间的那种关系.

This stackoverflow post has an example of a small snippet of compiled C code and the assembly language output from that snippet. It should give you an example of the sort of relationship between a high-level language and the machine code output that it compiles to.

了解这一点的最好方法是找到一个汇编器并进行尝试.在较早,更简单的计算机(例如1980年代的8位微型计算机)上,这种操作过去要容易得多.如今,与此类架构最接近的是嵌入式系统.您可以相当便宜地获得用于嵌入式处理器(如Microchip PIC)的开发板.由于这种类型的体系结构比现代操作系统具有更少的负担,因此使用系统调用的i点和t交叉更少.这将使在这种类型的体系结构上引导汇编语言程序变得更加容易.更简单的架构也更容易理解.

The best way to learn this is to get an assembler and try it out. This used to be much easier on older, simpler computers like 8-bit micros of the 1980s. The closest thing to this type of architecture available these days are embedded systems. You can get a development board for an embedded processor like a Microchip PIC fairly cheaply. As this type of architecture has less baggage than a modern operating system there is less i-dotting and t-crossing to use system calls. This will make it easier to bootstrap an assembly language program on this type of architecture; the simpler architecture is also easier to understand.

另一种选择是获得一个仿真器,例如 SPIM .这将模拟一个CPU,并允许您在其上汇编和运行程序.这种仿真器的优势在于,它们还将具有用于单步执行程序(非常类似于调试器)并显示寄存器文件内容的功能.这可能有助于了解实际情况.

Another option is to get an emulator such as SPIM. This will emulate a CPU and let you assemble and run programs on it. The advantage of such an emulator is that they will also have facilities for single stepping programs (much like a debugger) and showing the contents of the register file. This may be helpful in gaining insight as to what's actually going on.

这篇关于软件在哪里以及如何与硬件相遇?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆