2.1 Design your own computer

  What is the instruction set architecture it? To answer this question, in fact, very simple. But I want to explain, not so easy. We started from a small story.
  One day two small partners touched the surface, find each other very sorrow, asked a another "Hey, what are you distress it?" This said, "Oh, recently encountered a lot of problems on the operation." Good "computation big ah. "the other said," yes, I have also encountered similar problems. " " how do we solve it? " " Why do not we go and design a computer now! " said Well, two men and a shoot . "We designed the computer how the division of it?" Said one. "I obviously software programmers, I write software instructions on how to operations." Another said, "That's just, I'm a hardware engineer, I design computer hardware, mainly CPU is, that we will go separately to work! " " this is something estimate would take a year to put out the CPU design, but also to the software written. it can be really start yet? year after we met, how to ensure that you will be able to write software running on the CPU do I do? " " so not worry, we have to get better, we have to set the rules of the common good, then it can be split up to design software and hardware, thus ensuring then we can smoothly together in time to meet the software and hardware. " well, these two small partners began to discuss, what they want to discuss what is it? what we want to talk about the instruction set architecture.
  Computing tasks they have to face is not complicated, so just a very simple computer command system on it. We want to design the number of instructions, which instructions to design, first to be determined according to demand. It looks like we just need some simple addition. So first of all, we need to design an addition instruction. That these instructions and data to be operated, are to be placed among the memory, if the two digital memories which are added, may be more complicated. Therefore, we designed instruction when it, this addition instruction is a do: the number of a register among, memory and a number of them are added, which is then stored into the register. We register denoted by R, from among the memory address represented by M. So among our instruction, instruction contains a type of operation. However, the number among registers come from? Naturally to them from memory. Among the contents of the memory, loaded into the output register, this instruction is LOAD. The contents of the memory unit is operating behind a number M, is directed into the front a pointed R operand register them. With these two instructions later, we can be adding a number of memory among. But the result of the operation still register them, so we also need a directive, the number among registers, which put back into storage. This instruction, we write STORE, its role is the register R among the number, stored in the storage unit designated by the M. These instructions for the intermediate registers and memory data transfer, so we call transfer instruction class. 
  We already know from the CPU memory of them, according to the address, and then click Remove instruction begins execution, that sometimes we want to change seats fetch, this time you need to use this instruction, denoted by JMP L, when the CPU executes this article after the command, the memory cell will be transferred to the L pointed to fetch the next instruction to execute, so we call transfer instruction type instructions. Then we design a good instruction set computer we want, although it is very simple, but enough to complete the tasks we want operations. But this is described in English words and the alphabet, binary code is not type computer can recognize. So we have to make provisions taking a step forward. This is the specific format instructions, first of all we agreed, each instruction are of equal length, are two bytes. The first byte, we take four high as the operation code, the operation code is what indicates that this is a type of instruction. We now have four instructions, the LOAD, the ADD, STORE and JMP, we were assigned to it four different opcodes. Expressed as a decimal is 0,1,2,3, because we reserved four bits, so the future can be extended, it can be extended up to 16 instructions. But now we only define the four instructions, then see the lower four bits of the first byte, the four register number as we agreed, we now provide four registers, numbered to 0000 0011, respectively, refer to the CPU among these four registers R0 to R3, because we reserved four bits, so the future can continue to expand up to sixteen registers. This time it is for us to design the second generation, third generation and subsequent development, providing space for expansion. The second byte of the instruction, as we agreed address storage means, so that eight binary bits, So we can use a total of eight square 2, which is 256 bytes of memory.
   We look at an example. If the software designers to write such a directive: 0010 0001  0000 1001 then we can see from these four opcode out it is an add instruction. From four specified register number, you can see that he wants to visit is the R2 register. Then the address of the memory cell, the word is translated into a decimal 9. This instruction is encoded so want to complete the operation, that the contents of R2 and content storage unit 9 are added, to keep them register R2. But if the members wrote such software instructions 1010 0000 0101  1001 CPU to see if this instruction is that it does not recognize, it does not know what kind of operation. Since this operation code 0101 is not defined. The register number 1010, is also not defined. Of course, maybe in the second or third generation of design, we can then define the subtraction is 0101, 1010 may be the 10th register, then at that time on the CPU, you can execute this instruction up. But we agreed on the first-generation CPU is clearly unenforceable.
   That good agreement specific format instructions, let us look at the tasks we can do operations. Suppose we want to accomplish such a task, the content of the memory address M1, the memory by adding the contents of the address M2, M3 and finally stored into the storage unit which, after the operation is completed, the program branches to the memory pointed to seat which L continue. But such a task, how do we achieve it? Use this command system we have now we can use only these four instructions, we do not provide the number of two storage units directly incense plus instructions. This procedure should be so written, the contents of the first step M1 is sent to a register, the time being referred to as RX, then the contents of the content and M2 RX addition, the operation result stored in the RX, the RX content to memory and then M3 them, this completes the operation. And then transferred to L, proceed to fetch the next instruction. We assume that in reality the first task, M1 to the storage unit address five, M2 point 6, M3 point 7, L final destination address to be transferred is 18, and these are described in decimal. It is based on the task, our software programmers can write such a program in machine language, of course, at first glance all 01 of the code, it is difficult to distinguish, in order to facilitate learning, I put it a different meaning in different color represented. The same colors represent the corresponding relationship thereof, so that we can see the first instruction opcode 0000, should be a LOAD instruction, the next register number designated 0011, so that it points to R3. The third part is the address of the memory, this is actually a binary decimal 5, which can be seen we use this instruction LOAD, among the memory cells, corresponding to the contents of the address 5, is transferred to the register which the R3 . So that we can analyze the function of each instruction in turn, of course, I must first emphasize that the actual order of writing programs.
  First programmer software, machine language instructions are required to write directly, then it is written to the middle of the 01 encoding such, and to such a coding 01 on punched paper tape, the corresponding drill holes, to the computer were to go, such work is obviously very tedious, error-prone and inefficient. Later that as technology advances, programmers can write assembly language programs similar to the left of this category. Assembly language program and the machine language program substantially can be done one to one, then we pass some tools, it is possible first assembly language program into a machine language program corresponding to the machine language program and then transporting them to the computer. Further, people can write programs for various high-level languages. That of course requires more sophisticated tools, high-level language through several steps, eventually to be converted into machine language the middle of our table shown in this column. That is, a series of binary code. Now that we have written such a program, we need to put such a program into memory were to go.
   我们要注意的是,这只是存储器的一个片段,最左边这一列只是要存储器的地址。 我们展示了存储单元从地址5一直到地址18,所保存的内容。 我们从上往下依次看,在地址5和6这两个存储单元中, 存放了两个我们需要进行运算的源操作数,现在存放的是12和34, 地址单元7准备用来存放运算结果的。 现在被初始化为0。地址单元8和9这两个字节,存在了LOAD这条指令。 为了便于观看,我还是用不同的颜色标出了不同含义的二进制位,再往下的两个字节,是加法这条指令。 然后是STORE这条指令。然后是转移JMP这条指令。我们知道JMP这条指令会让CPU 转向地址单元18,去取出指令来。所以这里所描述的第五条指令是不会被执行的。 在执行完JMP这条指令以后,CPU会取出第六条指令进行执行。
  我们还是用大家熟悉的模型机的图示进行说明。 假想我们已经把刚才的程序和数据输送到了这个模型机的存储器当中, 与此同时,硬件工程师也设计完成了CPU,并将 CPU和存储器进行连接,构建成了完整的计算机的系统。 为了便于阅读,我将存储器当中二进制码进行了转换。我们可以看到,在存储 单元中,依次存放着我们要进行运算的源操作数,准备存放结果的空间, 还有包含了四条指令的这段程序。然后看右边CPU当中,如果PC寄存器已经装入了0000  1000这样一个地址,接下来的过程大家应该很熟悉了,PC中的这个地址会通过MAR寄存器,再通过 地址总线送到存储器,存储器就会找到这个单元所对应的那条指令, 
也就是LOAD这条指令,把这条指令的编码送回CPU。接下来就会依次完成我们所编写的这个程序。 当然最后还有一个问题,为什么PC寄存器当中的地址是这个呢? 其实这是不一定的,这也是我们进行指令系统体系结构设计时。必须要约定的一个内容。 就是CPU在启动时,或者说在复位完成之后,第一条指令从哪里开始取出。 这也是最开始,软硬件双方必须商量好的事情。 至于这个地址到底应该是什么,并没有明确的规则。 但通常情况下,我们会约定为这个体系结构所能访问的存储单元 的最小地址,也就是0,或者是接近最高地址的地方。 那大家也可以思考一个很简单的问题,如果我们这个体系结构约定的CPU的 起始地址是全0的话,存储器当中的程序应该做怎么样的改动呢? 如果要求现在左边这个图中,已经存放的程序和数据的位置都不允许变动呢。 其实解决方法很简单。 留给大家思考。
  现在我们已经对什么是指令系统体系结构有了初步的了解, 也知道如何着手开始设计一个属于自己的计算机。 从下一节开始,我们将一起分析几个真实的体系结构。
 
Reference:北京大学陆俊林老师计算机组成原理课程
Notice:如有侵权,请告知我,我会删除,谢谢!

Guess you like

Origin www.cnblogs.com/morwing/p/11794182.html