Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

hardware - How do assembly languages work?

I'm very curious how assembly languages work- I remain general because I'm not talking only about intel x86 assembly (although it's the only one I'm remotely familiar with). To be a bit more clear...

mov %eax,%ebx

How does the computer know what an instruction like "mov" does? How does it know that eax and ebx are registers? Do people write grammars for assembly languages? How do they write this? I imagine nothing is stopping someone from writing an assembly language that substitutes the mov instruction with something like dog or horse etc., (obviously this isn't semantic at all)

Sorry if this isn't too clear, but it's something I find a bit puzzling- I know it can't be magic, but I can't see how it works. I've looked up some stuff on wikipedia, but all it seems to say is it translates it down to machine code, well, what I'm asking is how that translation occurs I suppose.

Thoughts?

EDIT: I realize that this stuff is defined in reference manuals and things, I guess what I wish to know is how you tell your processor "Okay, when you see mov you're gonna do this". I also know that it's a sequence of probably a ton of logic gates..but there has to be some way for the processor to recognize is that mov is the symbol that means "use these logic gates"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Computers are basically built out of logic gates. Though this is an abstract idealization of the real physical machinery, it is close enough to the truth that we can believe it for now. At a very basic level, these things work just like true/false predicates. Or if you've ever played minecraft, it works a lot like redstone. The field which studies how to put together logic gates to make interesting complex circuits, like computers, is called computer architecture. It is traditionally viewed as a mixture of computer science and electrical engineering.

The most basic logic gates are things like AND, and OR which just take bits together and smash out some boolean operation between them. By creating feed back loops in logic gates you can store memory. One type of standard memory circuit is called a flip-flop, and it is basically a little loop of wire together with some AND gates and power to keep it stable. Putting together multiple latches lets you create bit vectors, and these things are called registers (which are what things like eax and ebx represent). There are also many other types of parts, like adders, multiplexors and so on which implement various pieces of boolean logic. Here is a directory of some circuits:

http://www.labri.fr/perso/strandh/Teaching/AMP/Common/Strandh-Tutorial/Dir.html

Your CPU is basically a bunch of these things stuck together, all built out of the same basic logic gates. The way that your computer knows how to keep on executing instructions is that there is a special piece of machinery called a clock which emits pulses at regular intervals. When your CPU's clock emits a pulse it sets off a sequence of reactions in these logic gates that causes the CPU to execute an instruction. For example, when it reads an instruction that says "mov eax, ebx", what ends up happening is that the state of one of these registers (ebx) gets copied over to the state of another (eax) just in time before the next pulse of comes out of the clock.

Of course this is a gross oversimplification, but as a high level picture it is essentially correct. The rest of the details take awhile to explain, and there are a few things here that I neglected due to unnecessary subtlety (for example, in a real CPU sometimes multiple instructions get executed in a single clock; and due to register paging sometimes eax isn't always the same thing; and sometimes due to reordering occasionally the way that instructions get executed gets moved around, and so on). However, it is definitely worth learning the whole story since it is actually quite amazing (or at least I like to think so!) You would be doing yourself a great favor to go out and read up on this stuff, and maybe try building a few circuits of your own (either using real hardware, a simulator, or even minecraft!)

Anyway, hope that answers a bit of your question about what mov eax, ebx does.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...