组装指令如何变成CPU上的电压变化? [英] How does an assembly instruction turn into voltage changes on the CPU?

查看:299
本文介绍了组装指令如何变成CPU上的电压变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去3 - 5年,我一直在C和CPython工作。考虑我在这里的知识基础。



如果我使用汇编指令,如 MOV AL,61h 到支持它的处理器,处理器内部究竟是什么来解释这个代码并将其分派为电压信号?这样一个简单的指令可能如何执行?



当我尝试想到包含在<$ c $中的大量步骤时,大会甚至会感觉像高级语言c> MOV AL,61h 甚至 XOR EAX,EBX



编辑:我读了几个意见,问我为什么把这个嵌入式嵌入式系统在嵌入式系统中并不常见。欢迎来到我自己的无知。现在我认为,如果我对此无知,那么有可能其他人也是无知的。



考虑到努力,我很难选择一个最喜欢的答案你们都放在你的答案中,但我觉得不得不作出决定。没有伤害的感觉,堕落。



我经常发现,我越了解电脑,我意识到我实际上知道的越多。谢谢你打开我的微码和晶体管逻辑!



编辑#2:感谢这个线程,我只是理解为什么 XOR EAX, EAX MOV EAX,0h 要快。 :)

解决方案

我最近开始阅读查尔斯·佩佐尔德(Charles Petzold)的书代码,该书目前涵盖了我认为的几种事物对...好奇。但是,在购买/借用之前,我一直没有一直通过这本书的方式。



这是我相对较短的答案,而不是Petzolds ...而且希望在你所听到的是晶体管我想。使用晶体管的最初方式是像晶体管收音机。它基本上是一个放大器,将微小的无线电信号放在空中,并将其馈入晶体管的输入端,该晶体管打开或关闭旁边的电路上的电流。而且,您可以以较高的功率连接该电路,因此您可以采取非常小的信号,放大并将其馈入扬声器并收听电台(还有更多的是隔离频率并保持晶体管平衡,但是你得到我希望的想法)。



现在,导致晶体管存在的方式是使用晶体管作为开关,如灯开关。收音机就像一个调光灯,你可以把它转到任何地方,一直到一路上。一个非调光灯开关全部关闭或全部关闭,在交换机中间有一些魔术场所,它会改变。我们在数字电子学中使用晶体管是一样的。取一个晶体管的输出并将其馈入另一个晶体管输入。第一个的输出肯定不是像无线电波那样的小信号,它强制第二个晶体管一路或全部关闭。这导致TTL或晶体管晶体管逻辑的概念。基本上,您有一个驱动高电压的晶体管或者将其称为1,并且在这种情况下,可以将零电压称为0,并将其与其他电子设备进行排列,以便创建与门(如果两个输入是1,那么输出是1),或门(如果一个或另一个输入是1,那么输出是一个)。逆变器,NAND,门,或非门(一个或一个逆变器)等。曾经是一个TTL手册,你可以买到8个左右的pin芯片,有一个或两个或四个某种门(NAND,NOR, AND等)功能内部,两个输入和每个输出。现在我们不需要那些用数百万个晶体管来创建可编程逻辑或专用芯片便宜的东西。但是我们仍然以AND,OR或NOT为硬件设计思路。 (通常更像nand和nor)。



我不知道他们现在教什么,但概念是一样的,因为记忆一个触发器可以被认为是两个这些TTL对(NANDS)与一个的输出绑定到另一个的输入。让它离开它。这基本上是我们所说的SRAM或静态RAM的单一位。 sram基本上采用4个晶体管。戏剧或动态的ram记忆棒你放在你的电脑自己每一点一个晶体管,所以为了初学者,你可以看到为什么戏剧是你买的千兆字节值。只要电源没有出来,Sram位会记住你设置它们的位置。戏剧开始忘记你告诉它,只要你告诉它,基本上是以三分之一的方式使用晶体管,有一些电容(如电容,不会进入这里),就像一个微型的可充电电池,一旦你充电并拔掉充电器,它就开始流失。想想在一个架子上的一排眼镜,每个玻璃杯里都有一些小洞,这些都是你的戏剧,你想让他们中的一些成为一个,所以你有一个助手填满你想成为的眼镜。该助手必须不断填满投手,下排,并保持一位眼镜足够充满水,让零位眼镜保持空白。所以在任何时候,你想看看你的数据是什么,你可以通过寻找一个绝对高于中间的水位来查看和读取这些零和零,而中间的水平肯定是零。所以甚至有权力,如果助理无法保持足够的足够的眼镜从零点告诉一个,他们最终将看起来像零和排出。它的优势在于每芯片更多的位。这么简短的故事就是在处理器之外,我们使用了大量的内存,而且还有一些辅助逻辑可以保持一个零的零。但在芯片内部,AX寄存器和DS寄存器例如使用触发器或sram保持您的数据。而且,对于AX注册表中的各位,您知道的每一位都有可能有数百或数千或更多的数据被用于从AX寄存器中进出。



你知道处理器以某种时钟速度运行,这些日子大约是2千兆赫或每秒20亿个时钟。想到时钟,这是由水晶产生的另一个主题,但逻辑看到时钟作为一个电压变高和零高,零在这个时钟速率2ghz或任何(gameboy进步是17mhz,旧iPod在75mhz左右,原装ibm pc 4.77mhz)。



所以用作开关的晶体管使我们能够承受电压,并将其变成我们熟悉的硬件工程师和软件工程师的零和零,并且直到给我们AND,OR或NOT逻辑功能。而且我们有这些魔法晶体让我们得到一个准确的电压振荡。



所以我们现在可以做一些事情,比如说,如果时钟是一个,而我的状态变量表示我处于获取指令状态,那么我需要切换一些门,使得我想要的指令地址(在程序计数器中)在存储器总线上出来,这样内存逻辑可以给我我的指令为MOV AL,61h。您可以在x86手册中查看,并发现其中一些操作码表示这是一个mov操作,目标是EAX寄存器的低8位,mov的源是一个立即值,这意味着它在该指令之后的内存位置。因此,我们需要将该指令/操作码保存在某处,并在下一个时钟周期中获取下一个内存位置。所以现在我们已经保存了mov al,立即数,并且我们从存储器读取值为61h,我们可以切换一些晶体管逻辑,使得61h的位0被存储在a1的位0触发器和位1到位1等。



如何发生这一切?想一下执行一些数学公式的python函数。您从程序的顶部开始,一些输入到公式中,作为变量,您可以通过程序单独的步骤,可以在这里添加常量或从库中调用平方根函数等。在底部返回答案。硬件逻辑以相同的方式完成,而今天使用的编程语言其中一个看起来很像C。主要区别在于您的硬件功能可能有数百或数千个输入,输出是单个位。在每个时钟周期,AL寄存器的位0正在使用巨大的算法来计算,这取决于你想要看多远。考虑你所要求的数学运算的平方根函数,该函数本身就是其中一个输入产生一个输出,它可以调用其他函数可能是乘法或除法。所以你可能会有一些地方可以想象为AL寄存器的第0位之前的最后一步,其功能是:如果时钟为1,则AL [0] = AL_next [0]; else AL [0] = AL [0];但是有一个更高的函数包含从其他输入计算的下一个位,并且更高的函数和更高的函数,其中大部分是由编译器创建的,就像你的三行python可以变成数百或数千的汇编程序。几行HDL可以成为数百或数千或更多的晶体管。硬件人员通常不会查看特定位的最低级别公式,以找出所有可能的输入和所有可能的AND,以及计算任何可能会检查程序生成的汇编程序所需的OR和NOT。但是如果你想要的话,你可以。



关于微码的说明,大多数处理器不使用微码。您可以使用x86进行比较,因为它是一天的精细指令集,但在表面上努力跟上现代。其他指令集不需要以上述方式直接进行微编码和使用逻辑。您可以将微码视为不同的处理器,使用不同的指令集/汇编语言来模拟表面上看到的指令集。不如当您尝试在Windows上的Mac或Linux上模拟窗口等复杂时,微编码层是专门针对该工作而设计的,您可能会认为只有四个寄存器AX,BX,CX,DX,但是里面还有很多当然,一个程序集程序可能会以一个核心或多个内核的多个执行路径执行。就像您的闹钟或洗衣机中的处理器一样,微码程序简单而小巧,调试和刻录到硬件中,希望永远不需要固件更新。至少理想但是像你的ipod或手机一样,你有时候会想要一个bug修复或者什么,有一种方法可以升级你的处理器(BIOS或其他软件在引导时加载一个补丁)。说你打开电视机的电视遥控器或计算器,你可能会看到一个洞,你可以看到一些裸露的金属触点在一排,也许三或五或许多。对于某些遥控器和计算器,如果您真的想要重新编程,请更新固件。通常,尽管如此,遥控器是完美的或完美的,足以超越电视机。微型编码提供了在市场上获得非常复杂的产品(数百万,数亿个晶体管)的能力,并在该领域解决了大而可修复的错误。想象一下,您的团队在18个月内写的2亿行Python程序,必须交付或公司将不会参加比赛产品。除了只有一小部分的代码,你可以在现场更新,其余的仍然被雕刻在石头上。对于闹钟或烤面包机,如果有错误或需要帮助的东西,您只需将其扔出来并获得另一个。



如果你通过维基百科或只是谷歌的东西您可以查看6502,z80,8080等处理器的指令集和机器语言。可以有8个寄存器和250个指令,您可以从晶体管数量中获得250个汇编指令仍然是非常高级别的语言,与每个时钟触发器中计算每个位所需的逻辑门序列相比较周期。你在这个假设是正确的。除了微编码处理器,这种低级别的逻辑不能以任何方式重新编程,您必须使用软件(对于将要或未被报废的硬件)来修复硬件错误。



查看Petzold的书,他做的很好,解释的东西,远远优于任何我可以写的。


I've been working in C and CPython for the past 3 - 5 years. Consider that my base of knowledge here.

If I were to use an assembly instruction such as MOV AL, 61h to a processor that supported it, what exactly is inside the processor that interprets this code and dispatches it as voltage signals? How would such a simple instruction likely be carried out?

Assembly even feels like a high level language when I try to think of the multitude of steps contained in MOV AL, 61h or even XOR EAX, EBX.

EDIT: I read a few comments asking why I put this as embedded when the x86-family is not common in embedded systems. Welcome to my own ignorance. Now I figure that if I'm ignorant about this, there are likely others ignorant of it as well.

It was difficult for me to pick a favorite answer considering the effort you all put into your answers, but I felt compelled to make a decision. No hurt feelings, fellas.

I often find that the more I learn about computers the less I realize I actually know. Thank you for opening my mind to microcode and transistor logic!

EDIT #2: Thanks to this thread, I have just comprehended why XOR EAX, EAX is faster than MOV EAX, 0h. :)

解决方案

I recently started reading Charles Petzold book titled Code, which so far covers exactly the kinds of things I assume you are curious about. But I have not gotten all the way through so thumb through the book first before buying/borrowing.

This is my relatively short answer, not Petzolds...and hopefully in line with what you were curios about.

You have heard of the transistor I assume. The original way to use a transistor was for things like a transistor radio. it is an amplifier basically, take the tiny little radio signal floating in air and feed it into the input of the transistor which opens or closes the flow of current on a circuit next to it. And you wire that circuit with higher power, so you can take a very small signal, amplify it and feed it into a speaker for example and listen to the radio station (there is more to it isolating the frequency and keeping the transistor balanced, but you get the idea I hope).

Now that the transistor exists that lead to was a way to use a transistor as a switch, like a light switch. The radio is like a dimmer light switch you can turn it to anywhere from all the way on to all the way off. A non-dimmer light switch is either all on or all off, there is some magic place in the middle of the switch where it changes over. We use transistors the same way in digital electronics. Take the output of one transistor and feed it into another transistors input. The output of the first is certainly not a small signal like the radio wave, it forces the second transistor all the way on or all the way off. that leads to the concept of TTL or transistor-transistor logic. Basically you have one transistor that drives a high voltage or lets call it a 1, and on that sinks a zero voltage, lets call that a 0. And you arrange the inputs with other electronics so that you can create AND gates (if both inputs are a 1 then the output is a 1), OR gates (if either one or the other input is a 1 then the output is a one). Inverters, NAND, gates, NOR gates (an or with an inverter) etc. There used to be a TTL handbook and you could buy 8 or so pin chips that had one or two or four of some kind of gate (NAND, NOR, AND, etc) functions inside, two inputs and an output for each. Now we dont need those it is cheaper to create programmable logic or dedicated chips with many millions of transistors. But we still think in terms of AND, OR, and NOT gates for hardware design. (usually more like nand and nor).

I dont know what they teach now but the concept is the same, for memory a flip flop can be thought of as two of these TTL pairs (NANDS) tied together with the output of one going to the input of the other. Lets leave it at that. That is basically a single bit in what we call SRAM, or static ram. sram takes basically 4 transistors. Dram or dynamic ram the memory sticks you put in your computer yourself take one transistor per bit, so for starters you can see why dram is the thing you buy gigabytes worth of. Sram bits remember what you set them to so long as the power doesnt go out. Dram starts to forget what you told it as soon as you tell it, basically dram uses the transistor in yet a third different way, there is some capacitance (as in capacitor, wont get into that here) that is like a tiny rechargeable battery, as soon as you charge it and unplug the charger it starts to drain. Think of a row of glasses on a shelf with little holes in each glass, these are your dram bits, you want some of them to be ones so you have an assistant fill up the glasses you want to be a one. That assistant has to constantly fill up the pitcher and go down the row and keep the "one" bit glasses full enough with water, and let the "zero" bit glasses remain empty. So that at any time you want to see what your data is you can look over and read the ones and zeros by looking for water levels that are definitely above the middle being a one and levels definitely below the middle being a zero.. So even with the power on, if the assistant is not able to keep the glasses full enough to tell a one from a zero they will eventually all look like zeros and drain out. Its the trade off for more bits per chip. So short story here is that outside the processor we use dram for our bulk memory, and there is assistant logic that takes care of keeping the ones a one and zeros a zero. But inside the chip, the AX register and DS registers for example keep your data using flip flops or sram. And for every bit you know about like the bits in the AX register, there are likely hundreds or thousands or more that are used to get the bits into and out of that AX register.

You know that processors run at some clock speed, these days around 2 gigahertz or two billion clocks per second. Think of the clock, which is generated by a crystal, another topic, but the logic sees that clock as a voltage that goes high and zero high and zero at this clock rate 2ghz or whatever (gameboy advances are 17mhz, old ipods around 75mhz, original ibm pc 4.77mhz).

So transistors used as switches allow us to take voltage and turn it into the ones and zeros we are familiar with both as hardware engineers and software engineers, and go so far as to give us AND, OR, and NOT logic functions. And we have these magic crystals that allow us to get an accurate oscillation of voltage.

So we can now do things like say, if the clock is a one, and my state variable says I am in the fetch instruction state, then I need to switch some gates so that the address of the instruction I want, which is in the program counter, goes out on the memory bus, so that the memory logic can give me my instruction for MOV AL,61h. You can look this up in a x86 manual, and find that some of those opcode bits say this is a mov operation and the target is the lower 8 bits of the EAX register, and the source of the mov is an immediate value which means it is in the memory location after this instruction. So we need to save that instruction/opcode somewhere and fetch the next memory location on the next clock cycle. so now we have saved the mov al, immediate and we have the value 61h read from memory and we can switch some transistor logic so that bit 0 of that 61h is stored in the bit 0 flipflop of al and bit 1 to bit 1, etc.

How does all that happen you ask? Think about a python function performing some math formula. you start at the top of the program with some inputs to the formula that come in as variables, you have individual steps through the program that might add a constant here or call the square root function from a library, etc. And at the bottom you return the answer. Hardware logic is done the same way, and today programming languages are used one of which looks a lot like C. The main difference is your hardware functions might have hundreds or thousands of inputs and the output is a single bit. On every clock cycle, bit 0 of the AL register is being computed with a huge algorithm depending how far out you want to look. Think about that square root function you called for your math operation, that function itself is one of these some inputs produce an output, and it may call other functions maybe a multiply or divide. So you likely have a bit somewhere that you can think of as the last step before bit 0 of the AL register and its function is: if clock is one then AL[0] = AL_next[0]; else AL[0] = AL[0]; But there is a higher function that contains that next al bit computed from other inputs, and a higher function and a higher function and much of these are created by the compiler in the same way that your three lines of python can turn into hundreds or thousands of lines of assembler. A few lines of HDL can become hundreds or thousands or more transistors. hardware folks dont normally look at the lowest level formula for a particular bit to find out all the possible inputs and all the possible ANDs and ORs and NOTs that it takes to compute any more than you probably inspect the assembler generated by your programs. but you could if you wanted to.

A note on microcoding, most processors do not use microcoding. you get into it with the x86 for example because it was a fine instruction set for its day but on the surface struggles to keep up with modern times. other instruction sets do not need microcoding and use logic directly in the way I described above. You can think of microcoding as a different processor using a different instruction set/assembly language that is emulating the instruction set that you see on the surface. Not as complicated as when you try to emulate windows on a mac or linux on windows, etc. The microcoding layer is designed specifically for the job, you may think of there only being the four registers AX, BX, CX, DX, but there are many more inside. And naturally that one assembly program somehow can get executed on multiple execution paths in one core or multiple cores. Just like the processor in your alarm clock or washing machine, the microcode program is simple and small and debugged and burned into the hardware hopefully never needing a firmware update. At least ideally. but like your ipod or phone for example you sometimes do want a bug fix or whatever and there is a way to upgrade your processor (the bios or other software loads a patch on boot). Say you open the battery compartment to your TV remote control or calculator, you might see a hole where you can see some bare metal contacts in a row, maybe three or 5 or many. For some remotes and calculators if you really wanted to you could reprogram it, update the firmware. Normally not though, ideally that remote is perfect or perfect enough to outlive the TV set. Microcoding provides the ability to get the very complicated product (millions, hundreds of millions of transistors) on the market and fix the big and fixable bugs in the field down the road. Imagine a 200 million line python program your team wrote in say 18 months and having to deliver it or the company will fail to the competitions product. Same kind of thing except only a small portion of that code you can update in the field the rest has to remain carved in stone. for the alarm clock or toaster, if there is a bug or the thing needs help you just throw it out and get another.

If you dig through wikipedia or just google stuff you can look at the instruction sets and machine language for things like the 6502, z80, 8080, and other processors. There may be 8 registers and 250 instructions and you can get a feel from the number of transistors that that 250 assembly instructions is still a very high level language compared to the sequence of logic gates it takes to compute each bit in a flip flop per clock cycle. You are correct in that assumption. Except for the microcoded processors, this low level logic is not re-programmable in any way, you have to fix the hardware bugs with software (for hardware that is or going to be delivered and not scrapped).

Look up that Petzold book, he does an excellent job of explaining stuff, far superior to anything I could ever write.

这篇关于组装指令如何变成CPU上的电压变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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