Tác giả giữ toàn bộ bản quyền (All Rights Reserved) — không phải giấy phép mở. Bạn vẫn xem và học miễn phí trên OpenER; muốn tái sử dụng cần xin phép tác giả.
[MUSIC PLAYING] BORA NIKOLIC: Welcome back to RISC-V machine language, which is our way of representing assembly instructions in a binary format, such that the processor can understand.
Keep in mind that we don't study this because we have a particular joy of staring at binary representation of instructions.
But we do that because it will be tremendously helpful later on, when we try to build our RISC-V processor that can execute these instructions.
We need to be able to speak at the language that the processor understands, because we don't have another design method.
We still don't have a system where we can do this — computer, here is the RISC-V specification. Build the processor for me so that it meets that spec.
It may happen at some point, but not quite right now. So let's get into these instruction formats. We have seen that all instructions in RISC-V fit 32-bit words.
So each one of them is going to be fully encoded as a 32-bit word.
It is going to follow a very rigid format, which is going to be somewhat different between six different instruction types.
The first type that you're are going to look at our register-to-register instructions for arithmetic and logic operations. So let's take a look at the R-format layout.
In the R-format format layout, we said each instruction is 32 bits wide. So it covers bits 0 to 31. And it has particular fields.
It is divided into variable width fields that sum up to a total of 32 bits. On the bottom — on the lowest, least significant bits — we have the opcode.
Opcode is 7 bits wide and occupies this position, 0 to 6. Then it's followed by the binary code for the destination register.
Notice it's 5 bits wide, which is enough to represent any of the 32 registers that we have in the RISC-V architecture — and occupies bit position 7 to 11.
Then it's followed by another field here that is called funct3. 3 bits wide, 12 to 14-bit positions. Then follows rs1 and rs2 register fields.
Each are 5 bits wide. And finally, there is a funct7 field that is 7 bits wide, from 25 to 31.
All register based arithmetic and logic instructions look alike. We said that before.
It always has the name of the instruction, then fields 1, 2, 3 — where 1 corresponds to the destination register, 2 corresponds to the first source register, 3 corresponds to the second source register.
And we see that here. We have the place to put which one of the registers is our destination register.
And we have spaces for our two source registers. Let's take a look a bit more. What are these opcodes and funct3 and funct7 fields?
Those are the ones that specify what kind of an instruction do we want to execute.
So opcode, funct3 and funct7 are going to tell us whether we want to do add, subtract, shift left — or xor and so on.
And these are going to be the registers that we are targeting. Notice one important thing — all the instructions that are of our type are going to have the same opcode.
That is going to be 0110011, every single one of them. n and r are going to have 0110011 in the opcode field.
That means the different instructions are going to be encoded in funct3 and funct7 fields. But then you may start to scratch your head.
Ooh, why do I need 10 bits to encode about 10 instructions? With 10 bits, I can encode 1,024 different instructions.
Are we going to encode them such that each one of them just has one bit equal to 1? No, but it is true that we don't need 1,024.
We don't need to utilize all bits in funct3 and funct7 fields to represent all arithmetic and logic instructions. So we have some redundancy there.
We have some options how we want to choose that encoding.
And you'll find out that we're going to use that to make the job of a processor in figuring out which instruction is it going to execute easier.
All right, the other question is — you may ask, why are these opcode and funct3 and funct7 fields spread all over the place? We'll see that later.
It is, again, to make processor's job easier.
Now, we have mentioned the teaching instruction has registered specifiers. And they're placed at rigid spots. We have a spot for the destination register, for the source register, and the other source register.
Each register holds a 5-bit unsigned integer, which is just enough that we need to identify one of the 32 registers that we have in the RISC-V ISA so there is no redundancy there and no need to have any redundancy there.
So let's take a look at a practical example how we do that. Let's encode the first instruction that we have learned for register-based operations. That's the Add.
And the format of the Add is add, rd, rs1, rs2. So in this case, add will operate on the contents of the register x19 and extend.
Take them, add them together, and place the result in the destination register, x18. So what we have here is relatively straightforward when you look at it.
First, we are going to write down the opcode. All our type instructions have the same opcode. That opcode is 0110011.
Then we're going to look up in the green sheet and find out that add instruction corresponds to all 0s in funct7 and funct3 fields.
And then finally, we need to fill out the numbers that correspond to the registers that we are using. So in this case, we look up, where does the destination register exiting go?
Oh, it's supposed to go into the rd field, so we write the binary value for 18, which is 10010 — 16 plus 2. All right, we got that one.
And we do the same with rs1 and rs2 registers. rs1 is 19, which is 16 plus 2 plus 1.
And then finally, rs2 right here is 01010, which is 10. All right, we figured that out.
Hopefully that made sense. How about you do this one? And what is the correct encoding of add x4, x3, x2. And ignore that I might be using some reserved registers for some things.
This is just an example of register-based addition. I'll pause here or you can pause me here. Do it yourself. And then I'm going to continue and work it out.
In this case, we actually don't need to write all 32 bits. Let's try to guess which one is the correct representation.
So let's take a look first — all register-based instructions end with 0011, which is a hexadecimal representation of 3.
And note that all our answers are offered here as hexadecimal numbers. So I should just go ahead and eliminate the one that does not end with 3.
The other ones, we have now options — the next significant digit to be either 3 or B. Which one is that? So let's take a look at rdrd is x4.
The last bit of x4 is 0. x4 is 0100.
So then it has to be — if I take these bottom three bits and add the 0 that has to be here, then that's going to be 0011.
Therefore, the next significant bit needs to be 3 as well. So I can cross these two off. So which one is it? 4021 or 0021?
Well, we know that add has all 0s in the leading position. So we can eliminate this one. And 4 is our correct answer.
But if you would like to verify that, you can go ahead and write down all 32 binary digits and convert them to hexadecimal. It's a short exercise.
Well, this is a summary of all of our r type instructions. We have seen them all. We have the encoding for all of them in one table.
So what did we find out? They all have the same opcode in the end — so 0110011.
And they have the same spots for the destination and the two source registers. And they have different funct7 and funct3 fields.
So the instructions that we have here are add, sub, sll, slt. Sll stands for shift left logical. What is slt?
That's a new instruction. It is set on less than. It compares the values of source registers rs1 and rs2.
And if contents of rs1 is less than rs2, it sets the destination register to 1. It is useful for comparisons there. And then there is the unsigned version of that.
Then we have xor, shift right logical, shift right arithmetic, or, and and. So we just added two more instructions over here that are very useful.
Let's take a look a little bit at the encoding here. One thing that you will spot is that add and sub are very similar to each other.
And that's done on purpose that way, because we are going to be using the same hardware to perform subtraction as we use for addition, because subtraction is just using additional tools complements number.
There is no different hardware needed. Except that we need to perform sign extension. So go ahead and circle this.
This bit basically says that they need to sign extend an operand before they perform subtraction. Which other instruction has that bit equal to 1?
This one. What's that instruction? Oh, that's shift right arithmetic. That's another one that does the sign extension.
So this bit here signifies that instruction is going to do sign extension.
It basically tells a processor you're doing registered type instruction and you're supposed to start with sign extension here.
So very early on, it can start extending the sign of the operand. Very useful hint over there. And then you're going to see some other similarities through this table.
You don't have to pay attention to that now, but there is some cleverness that is put in the way how they're encoded. I'm going to pause here.
And we'll be back in just a bit to talk about the next four instruction formats, which is our immediates. See you there.
Đăng nhập để đánh giá học liệu này.
Đăng nhập để bình luận — chưa ai bình luận về học liệu này.
Không có khóa học liên quan.