"Reprint" CPU Gump

Source: Internet
Author: User

Original:CPU Gump

Preface God has closed a door for you, it will certainly open a window for you to describe the words I most suitable.
I am CPU, they call me Forrest Gump, because I and "Forrest Gump" in the Forrest Gump, a bit silly.
God made me, gave me a small brain volume, a few registers can temporarily remember something, but God opened me a special window, it is like Forrest Gump, run fast.
How fast is it? I have a metaphor for that, my work is in nanoseconds, and one second of your world, I might have done 1000,000,000 (1 billion) moves.
In comparison, the memory is 100 times times slower than me, the hard drive is slower than I 1000多万倍, you say I fast? Start I live in a chassis, every morning a current wake me up, but also mixed with a buzzing voice, I know my faithful escort fan again began to work, I am particularly afraid of heat, and running fast, if no fan to my cooling, I will soon be ill, the consequences of illness is very serious, that is my friends like memory, Hard drive up. All are going to strike, without me this system will fall into a dead silence.
I heard that some CPU benefits are very good, even in constant temperature and humidity, spotless hosting room, let me envy.
My brain was small, so I woke up with just a few things my creator told me: 1. Your job is to run command 2. You can't save the instructions, your instructions are all in memory 3. Your first instruction is at the top of the memory 0xfffffff0
So what else to say, call the memory to command, call through the system bus, but also through the I/O Bridge to the telephone office needs to be transferred, and then through the storage bus to connect the memory.
"Man, give me the instructions at this address."
"Who Are you?" "Memory has forgotten me, of course, he broke the electricity and I, like, Amnesia."
"I am Forrest Gump, we often chat, you forget?" "
Memory Mill It was half a day before the data was sent over (100 times times slower than me), this is a jump instruction, I immediately recalled, this is my old friend bios waiting for me to run his pile of instructions.
I called the BIOS: "Brother, what do you do today?" ”
"Good morning, Forrest." Bios never loses memory, all remember clearly "is not the same old Ah, nothing but do a system self-test, look at memory, hard disk, graphics card and other these older guys have no problem, if there is a problem with a small horn cue master"
These processes I have pro, quickly fix, as usual, no problem, I also put a thing called interrupt vector table is ready, I know for a while to use
These things are finished, the BIOS did tell: "Forrest, int 0x19"
I'm going to go to the just-finished interrupt vector table to check the 19th number, and then find the corresponding 0x19 a lot of instructions.
Execute it, this heap of instructions carries the first sector of the disk (512 bytes from the beginning of the disk) to the 0x0000:0x7c00 of memory, and then I proceed from here.
I remember, and then there was a whole bunch of neat instructions to wake the lost OS from the hard drive and transport it into memory. (This is really complicated, omit 100,000 words ....) )
You see, that's why they call me Forrest Gump, I do things fast, but I have to be told where to do it, or else I will just sit there in a quandary. Run the operating system once in memory, the boss is immediately, everyone has to listen to his command.
I also found that there was a room around me: a lot of things piled up in the house, the process of describing the information wrapped, the process control information wrapped, I do not care, I only care about the most important two things: 1. The necessary registers for my work are placed on the workbench in front of me. 2. Program counter, I use it to remember the next command address I want to execute.
"Forrest Gump, Unscarred Ah", the operating system for me is very good, first to greet me.
"Linux boss, what do you have today," I always show the positive initiative.
"Come on, put this Hello World program to run."
Hello World program is still asleep on the hard drive, you have to load him into memory, otherwise I how to execute ah.
So I picked up the phone to call the hard drive, the phone through the system bus to the IO Bridge Telephone office, and then transferred to the IO bus, this came to the hard disk here.
I asked him to bring the data to me on the phone, and then I sat there and waited for nothing.
Linux boss immediately angry: Forrest Gump, tell you how many times, you boy how still waiting for the hard drive to send you data!
Yes, I forgot one thing, hard drive is much slower than me, I execute an instruction is about 1ns, in the 16ms to read the disk, I can potentially execute 1600多万条 instructions AH.
I feel deeply guilty, hurriedly picked up the phone to call the hard drive: dude, according to our previous discussion, with direct memory access (DMA) Ah, you directly load the data into the memory bar, do not pass me, after loading to send me a signal.
"It's pretty much the same" Linux boss in a good mood "Forrest Gump, the data has not come, do not idle, there is a Fibonacci that cut the number of series, to calculate it?"
"Fat Wave concubinage series?" This name is very strange, boss, in fact, you also know that my brain is small, too lazy to understand what that means, you put the process of the house switch, the program counter set, pointing to the next command, I have a command to carry out "I am not pursuing."
"What a Forrest Gump!" The elder brother regrets to.
The process I was in was immediately changed (of course, this is what I did with the Linux boss), and the information for various packages changed, especially registers and program counters.
So I began to calculate what concubinage sequence, but this sequence seems endless, which brain-free programmer wrote an infinite loop.
Just then, I received a call that the HelloWorld data has been loaded into memory and let me handle it.
I put down the hands of the work, save the scene, to deal with the HelloWorld, sure enough data has been good, then switch the past to run it.
In fact, the boss does not know, anyone, as long as you run a considerable number of instructions, you can realize the secret of these procedures. I CPU Gump although silly, but also jiabuzhu implementation of this number to hundred million instructions to my edification ah.
The secret is that the program is made up of sequence, branching, and looping. In fact, branches and loops in my opinion are all jumps.
So my job is to call to ask the memory to a command, execute this command, if it is a jump command, I asked the memory to jump to the destination address of the command, continue to execute, life is so simple.
Ao Right, of course, there are complex, is the function call, I have to work closely with the memory to complete. Let's talk about this next. New equipment: Cache refers to memory, this is really my good friend, without him, I almost do nothing, more importantly, he is faster than the hard disk, read data, only 100 nanoseconds or so. It's a lot easier for us to talk about.
Every time he said: "Forrest Gump, fortunately, you give me a chat, or I must be alive to the stuffy death, the hard drive to speak is too slow"
"Why is it so slow?" "Every time I ask
"Hard disk is a machine is a thing, a head on a plate of high-speed rotating magnetic disk to move, light positioning is slow to death."
"Why would the owner use the hard drive?" "
"Although people are slow, but not afraid of power outages, which like you and me, a blackout all lost memory." "
It is true that people can't take all the good things.
Some of my instructions are completely complete with my registers, but there are a lot of data that need to read and write memory, plus all the instructions are stored in memory, although it's only 100 times times slower than me, but I'm still a little bit too much of a command.
I said to the memory: "Dude, can you hurry up!" "
Memory said: Please, this is my limit, Gump, you have to think of ways to do it! I'll tell you what, I noticed the instructions and data you visited recently, and I found a pattern.
"What rules?" "
"For example, you visit me a memory location after a few more visits, and a memory location is accessed, near the location will soon also access to" ( code Nong Turn over note: This is actually called the program's local principle
I thought it was a rule, but I've noticed it before.
"What's the use?" ”
"Since you often visit things in the same area, you think about what it would be like to have these things cached in you ...." "
I think it makes sense! Try adding a cache!
From then on, every time I read and write instructions and data, I asked the cache, the cache did not call memory.
Sure enough, due to the existence of the principle of locality, I found that it is a lot faster.
Of course, there are shortcomings, that is, the Linux boss in the process of switching, the cache will expire, because there are no links between the two programs, the local principle does not work, so need to rebuild the cache.
Self-improvement: Pipeline caching makes my work more efficient, got the praise of the Linux boss: "Forrest, I think you are very clever, will use the cache."
"I'm so smart, it's a memory idea. Boss, but I learned an important thing: When you can't change someone else's words, complaining is useless, or change yourself first"
"Very philosophical, I hope you will restart tomorrow can still think of" Linux boss jokes me.
"I recently found a problem, I am distressed, you see I have four hands, the first hand is responsible for calling memory to command, the second hand translation instructions, the third hand is really executed, the fourth hand sometimes have to write the results back to memory.  The problem is, I found that often only one hand is busy, others are idle, you see the first hand to take instructions, other hands can only wait. When the second hand translates the instruction, the other three will have to wait.
"I guess we can't call you Gump anymore, you're starting to think," The Linux boss laughed.
"This is a good problem to solve, give you an example, have you ever heard of washing cars?" Similar to you, is the first to spray water, then play detergent, and then scrub, and finally drying, but people's work and you are not the same, others are assembly line work, you think, a car in the drying time, the back is not still three cars, respectively in the water spray, playing detergent and scrub, each step will not be idle. ”
"How could I not have thought of such a simple truth?" I can also make an assembly line, so that every hand is used up. "
Others say we are high-tech, but in fact the principles are contained in life AH.
With the help of the cache and the pipeline, let my work greatly accelerated, everyone was impressed with me. They want to give me a new name: Superman, but I still prefer them to call me "Forrest Gump", how kind. At the end of my meticulous, conscientious running instructions, and occasionally chat with the partners, soon the day passed, and at night, I know the time to shut down, hurriedly to give them goodbye.
Soon the currents that had excited me vanished, the hum of the fan was gone, and I could no longer make the call, and the whole world was silent.
Tomorrow will be a new day.
Note: This article is mainly from the "in-depth understanding of computer systems", this book is very good, worthy of every programmer to study carefully. Public number: yards Nong turn over the "code Nong" public number was created by a former IBM architect who worked for 15 years, sharing fun, interesting programming knowledge.

"Reprint" CPU Gump

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.