All things in all posts will help you visualize about the way that computers work generally. Note that it is not the real structure of modern computers, but the way that modern computers work is nearly the same as in my posts. I am sure that if you understand about the way that computers work in my posts, you will easy to learn about all fields of computer afterward. Thank you !!!
We have learned about the arithmetic and logic unit (ALU) in a previous post. We have also known that the ALU will have eight operations: ADD, SHL, SHR, NOT, AND, OR, XOR, CMP. ADD, AND, OR, XOR, CMP will be operations that have two bytes as inputs. SHL, SHR, NOT will be operations that have one byte as input.
Now, we will use instructions to conduct operations in ALU. This is also the first type in nine types of instruction that we will learn. How many instructions can this type of instruction have? Let’s calculate it! First, we have eight operations in the ALU. Second, we have four registers (R0 to R3), so we can choose one in four registers for the first register. We can also choose one in four registers for the second register. Those first and second registers are used in ALU operations.
Note that, we can choose the same register for the first and the second register. Finally, we can have: 8 x 4 x 4 = 128 instructions that we can have in this type of instruction. This is also the type of instruction that has the largest amount in the computers. It contributes half of all instructions in the computers.
For easier to illustrate, let’s look at the model of bits parts of this type of instruction below. All 128 instructions will be represented in only one byte model:
In the diagram, you can see the first bit of this byte is “1”. It tells us this is the ALU instruction. The next three bits will tell us what is the operation in the instruction. It can be ADD if three bits are “000”. It can be CMP if three bits are “111” and so on. The next two bits will be the code for the first register (Reg A). If these two bits are “00”, the first register will be “Register 0”. If these two bits are “01”, the first register will be “Register 1” and so on.
The remaining two bits are code for the second register (Reg B). If these two bits are “00”, the second register will be “Register 0”. If these two bits are “01”, the second register will be “Register 1” and so on.
OK! Now, let’s illustrate the way that an instruction works in computers. First, we want to add the binary number of “00000001” in the “Register 0” (Reg A) to the binary number of “00000010” in the “Register 1” (Reg B) and save the result in the “Register 1”. We can create this thing easily with the instruction of “10000001”.
Assuming that the instruction of “10000001” have the address of “00010000” (16) in the RAM. First, we will save this address in the IAR. Then, we set the computer to work. The address in the IAR of “00010000” will be loaded in RAM to choose the address of the instruction.
Then, the instruction of “10000001” will be loaded in the IR that will connect to the control unit for detecting the ALU operation in the instruction. Then, through the connection to ALU, the sum of “Register 0” and “Register 1” will be calculated. Finally, the result of summation will be saved in the “Register 1” again.
So, you can see that with two inputs of bytes, the ALU operations will be executed on both bytes of the two registers (Reg A and Reg B) in the instruction for the result. The result then will be saved to the second register (Reg B) again. Note that we can choose the same register for the first and the second register.
We also have an exception, that is for CMP of ALU operation. This ALU operation also receives two bytes of the registers in the instruction for comparison. This ALU operation will output the bits that we can see whether two bytes are equal or not, whether the first byte in the first register is larger. But the result will not be saved in any register.
That is for two inputs of bytes, what about for one input of byte in the ALU operation: SHL, SHR, NOT? In this case, the ALU operations will be executed on the byte of the first register (Reg A) for the result. Then, the final result will be saved to the second register (Reg B). Also note that we can choose the same register for the first and the second register.
In previous posts, we connect a step directly to the AND gates for registers because we have only two registers for one step in the stepper. But in this ALU instruction, we must use bits in the instruction to choose one in many registers, so we need more complex structures that help process this work. Look at the diagram below to understand more:
So, these two bits will choose only one output for the input of one in four AND gates of registers from R0 to R3 because all the outputs of the decoder connect to the inputs of all AND gates of registers. Two more things that we can also see in this part are the “Reg B” and the “clk s”.
The “Reg B” represent for the connection of the step in the stepper to the structure and the “clk s” represent for the connection of the “clk s” of the clock in the cycle of the step that is connecting to the structure. So, you can illustrate the way this structure work like this: 6 and 7 bits of the IR will always choose one input for one AND gate of register through decoder, then if stepper run to the step that connect to the “Reg B”, the “Reg B” input and the “clk s” input of that cycle of step will all be “on” simultaneous in a specific interval of time. So, the one register will be chosen.
So, you can see that the last two bits (6 and 7) choose for the second register and in this structure the second register will perform a function of saving the byte of result on the bus because we use the “clk s” in the structure. In conclusion, the structure on the right of the diagram will help choose the register to save the byte result on the bus.
How about the left part of this diagram? You can see that we have two structures that are similar to the right part of the diagram. Then, we connect corresponding outputs of each AND gate of each structure as the inputs of an OR gate. For example, the outputs of the first AND gates of the two structures will be the inputs of one OR gate.
The two pairs of bits that connect to these two structures are 4,5 and 6,7. So, the 4,5 bits are used for the first structure and the 6,7 bits are used for the second structure.
The two more things is that we have two steps “Reg A” and “Reg B” that connect to each structure and the “clk e” will connect to the two structures instead of “clk s”. You can see that two structures will choose two registers for transmitting the byte of them on the bus in two different steps of the stepper because each structure connects to a different step (“Reg A” and “Reg B”) and they connect to “clk e” instead of “clk s” of the clock.
In conclusion, the left part of the diagram will help to choose two registers that transmits the byte in register onto the bus. Each register will work in a separate step in the stepper.
We have just known the structure that connects to the last four bits of the IR (4,5,6,7). Now, we will come to the part of explaining the way an ALU operation works. We will use bits from 0 to 3 for conducting this operation work on the registers that we choose by bits from 4 to 7.
First, we will briefly mention the process of ALU operation like this: We will set the byte of data in the second register “Reg B” (that is chosen by sixth and seventh bits) into the TMP register first. Then, we will choose the ‘op’ code for the ALU operation and concurrently transmit the byte of data in the first register “Reg A” (that is chosen by fourth and fifth bits) onto the bus.
The calculation will occur in ALU part of the CPU and the result will be set in the ACC register. Finally, the byte of data in the ACC register will be set to the second register “Reg B” again. Look at the diagram below:
In the diagram, you can see steps from 4 to 6 in the stepper for ALU instruction. In a previous post, you have known that steps from 1 to 3 in the stepper will be used for loading the instruction from the RAM to the IR in the CPU. The 4, 5 and 6 steps will be used for different types of instruction and the ALU instruction will be the type of instruction that I will introduce in this post.
Now, we will explore steps from 4 to 6 in the stepper for ALU instruction. Look at the step 4 in the stepper, this step will connect to one input of an AND gate for the signal that helps enable “Reg B” and set the TMP register. The remaining input of the above AND gate will connect to the bit “0” of the IR in the diagram.
As you know, the ALU instruction will have the first bit as “1”, so the output of the AND gate will be “on” because this is an ALU instruction. Then, the “Reg B” will be enabled and the TMP register will be set, so the byte in the “Reg B” will be saved in the TMP register. The way that computer chooses the “Reg B”, I mentioned above.
Step 5 also connects to an input of an AND gate for the signal that helps enable “Reg A” and set the ACC register. The remaining input of this AND gate will also connect to the bit “0” of the IR in the diagram and that input is always “on” because this is an ALU instruction. So, enabling the “Reg A” and setting the ACC register will occur.
Also in the step 5 of the stepper, the step 5 will connect to three inputs for three AND gates that determine the “op” code for the ALU operation. Each of these AND gates also have two more inputs that connect to bit “0” of the IR instruction in the diagram and to one of the bits from 1 to 3 of the IR instruction in order.
Two inputs of AND gates above that connect to the step 5 and to the first bit (0) of the IR instruction will always be “on” in the step 5 of the stepper. So, the output of these AND gates will depend on three bits from 1 to 3 of the IR instruction. The states of these bits will be copied as outputs for three AND gates. So, the ALU operation will be chosen by bits from 1 to 3 of the IR instruction.
In conclusion, step 5 will help to enable the “Reg A” on the bus. Then, choose the ALU operation for “Reg A” and “Reg B”. Finally, performing the calculation and setting the result into the ACC register. The way to choose the “Reg A”, I mentioned above.
In step 6 of the stepper, we also have two connections to the two inputs of an AND gate that are one from step 6 and another from the first bit of IR instruction. And these two inputs alway be “on” in step 6.
One more input for this AND gate is from the output of a NOT gate. The input of this NOT gate is from the output of an AND gate that has three inputs of bits from 1 to 3 of the IR instruction. So, the output of NOT gate will only be “off” when all bits from 1 to 3 of the IR instruction are “1” and will be “on” in other cases of these three bits.
The output of this AND gate will help to decide whether to enable the ACC register and set the “Reg B” register.
From three inputs of the above AND gate, we will have a conclusion about step 6 in the stepper is that when three bits from 1 to 3 of ALU instruction in the IR are “111” the result will not be saved in the “Reg B” again. And the result will be saved in the “Reg B” again in the remaining case.
You can note that the “111” bits will choose the ALU instruction as “CMP” (comparison), so the result is not necessary to save in the “Reg B” again. The important output for “CMP” operation will be the outputs of “equal” and “‘a’ larger” bits, not the result in the ACC register.
In reality, we have one more step: step 7. This step I mentioned in a previous post. This step will only be used to reset the stepper and not used to conduct anything in the control unit of the CPU.
From all things above, we understand instructions in computers clearly. But, we must always use the binary codes for instructions in computers. Yes! Computers need to know the binary code to execute instructions. However, we also have a thing called “computer language”. This language can be used to write programs easier than instruction codes for programmer. Look at the table below to see these instructions:
You can easily understand them. And you may wonder why we must use them? The answer is when we use them, we can write programs very fast. If you use the binary code for instructions such as “10000001” to ADD R1 and R2, then save the result to the R2. But with this computer language, we only need to write “ADD RA,RB”.
One more interesting question is how can computers understand that computer language? Yes, computers only understand binary codes, so we will need one compiler for translating computer language to the binary code. That compiler I will mention in another post. Hope you like this post. Thanks for reading and see you later!!!
0 Nhận xét