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] PROFESSOR: Hi, welcome back to RISC-V instruction and coding.
We have just covered the R type instructions that perform register arithmetic and logic operations.
So let's see how we can support similar kind of an instruction, which are those that work with the immediates. Ideally we would like to use the same instruction format for all possible instructions.
But that may not be quite practical. However, we would like to reuse the placement of most of the fields across different formats.
So what we'll see here, we will need a different format for I type instructions, which are instructions that work with the immediates.
But we'll try to see how we can make it to be similar to the R type.
So let's review how do the register based add — how does the register based add instruction look like, and how does its immediate counterpart look like.
So register based add takes the value from register rs1, adds it to the value of register rs2, and stores the result in the register destination, register rd.
Its immediate counterpart, add immediate, takes the value from the register rs1, adds the immediate, and stores the result in the destination register.
So where is that immediate coming from? Well, it has to be encoded as a part of the instruction.
We clearly cannot use all 32 bits in the instruction to encode an immediate, so we need to use just part of an instruction. What can we repurpose from the R type?
Well, we can see that rs2 is not being used. So we can use that. But rs2 only has five bits. Five bits is just not enough, or not enough to arrange for an immediate.
It would give us only 32 different values covering the range of signed integers from minus 16 to plus 15.
So we need to add a new instruction format where we will repurpose other fields in the instruction format.
So let's review what we see in the R type. We have the opcode on the bottom.
Then we have five bits for the destination register, three bits for funct3, five bits for rs1 and rs2 two fields each, and then we have the top seven bits dedicated to the funct7 field.
What are we going to do in the I type instructions? Well, we already agreed that we're going to repurpose the rs2 field. But we are going to add the funct7 field to that.
So what is going to happen we drop that, and make one 12-bit field that is going to be storing the immediate value.
Notice that in R type instructions, we use one opcode. We are going to have a new opcode for I type instructions.
Also with R type instructions we were kind of sloppy in we didn't use everything that — all the bits that we had in the instruction. We had left over some of the funct7 fields.
We have to be careful how we're encoding things for I types, because if you are sloppy, we will lose the range for the immediates.
One thing that we will ask ourselves, how many different instructions do we need to support in this case. There are seven instructions that we have seen so far.
We are going to see that we need to add two more to our base instruction set. So we'll have nine. Funct3 field can encode eight different instructions.
So we'll use the trick to add the ninth one. But nevertheless, we are now able to support 12 bits immediates. And they cover a good range.
That is something that we are often going to see in the assembly code, this range of immediates is going to cover a wide range of, say, loops.
The range of this immediate, since it's 12 bits, covers for 4,096 values going from minus 2048 to plus 2047.
Keep in mind that since we are always using that immediate together with a value from a register rs1 which has 32 bits, we have to sign extend that value to 32 bits.
What is sign extension?
It is an automatic operation the processor performs where it extends that top most significant bit of the immediate across all the most significant bits.
Basically remember that thing where we smear that sign bit over all the bits to preserve the sign of that integer.
So if that sign of the most significant bit of the immediate is zero, we copy zero. If it is a one, we copy all ones.
Keep in mind that some things will need wider range than 12 bits of immediates. And we'll see that later. It will use another instruction to cover that.
Let's take a look at an example for the I format.
So let's take a look at an assembly instruction add immediate x15, x1, minus 50, which takes the value from the register x1, adds minus 50, and starts in the x15.
This is essentially a subtraction, because remember we don't have sub immediate.
Immediate types have a different opcode. Opcode here is 0010011.
That is different than what we have had in R type or register based instructions that had 0110011. Now what we need to do is to fill the rest of the fields.
So we will put the rd value of 15 in the rd register. And rs1 value of 1 in the rs1 register.
We'll take a look at the green sheet and find the add has a funct3 tree field of 000, which happens to be the same as what we have seen in R type instructions.
Finally, we need to encode the immediate value in the top 12 bits. So here immediate is minus 50.
So we need to write to complements value in the top 12 bits of minus 50, which you can verify is true.
This one has a value 32, 16, plus 1, 49, plus 1, to get it inverted as minus 50.
Let's recap what we have seen of I format arithmetic and logic instructions. There are nine of them. Seven of them we have seen before.
So this is add immediate, xor immediate, or immediate, and immediate, shift left logical, shift right logical, and shift right arithmetic, all of them immediate.
There are two new instructions that we have here — slti and sltiu.
slti compares the value that is in the source register with an immediate value.
And if the source register value is less than the immediate value, sets the destination registered to one.
Similarly sltiu does that with unsigned numbers. Take a look at a few interesting things in this table.
First, all funct3 fields are the same as what we have seen in registers type instructions.
That is very convenient. That basically means that we are going to recycle that part of a microprocessor that will have to figure out what kind of instruction we have there.
Then take a look at this way. What are we doing with the shift amounts? The amount of shift is limited to five bits.
Because if we shift either left or right by more than five bits, we have already inserted all the zeros into the register, and using larger shift amounts is meaningless.
We are not going to do anything other than just keep shifting in zeros. So we don't need to support that. That's why our shift amount is limited to five bits.
And then we have these top seven bits of the immediate value free to do something else.
In this case, we're using bit 30 of the instruction to encode whether we are doing logical or arithmetic shift right, which basically means whether we are sign extending or not.
And notice that it is done in the same position as we have had for the register based instructions. And that's it.
And that summarizes the I type format. And we are going to move on to the next section in a bit.
Đă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.