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: Welcome back to RISC V instruction formats. We are almost done.
We have covered five different instruction formats. We just have one more to cover. That's the one that covers jumps in RISC V. And guess what? There is only one jump instruction, which is jal.
So the format for jal is very similar to our U format that we have seen before.
It essentially needs room for a long immediate and a destination register and then, of course, the opcode. So the format goes like this.
We have the opcode that is 7-bit, the destination register that is rd that is 5 bits, and a 20-bit offset.
However, shuffled a bit to look more like what we have seen in branches, because we would like this to behave more like branches.
What does jal do? Just to remind ourselves, it saves the next instruction, which is the value in PC plus 4, which points to the next instruction that will be executed in the memory.
So it will save PC plus 4 in the destination register — that's our return address after a function call — and sets the program counter to the program counter plus offset.
Because jal And RISC V support PC relative addressing for jumps as well as for branches.
And the code can be relocated across the memory locations.
Important things to remember here, the range here is actually 21 bits, because we follow the same convention that we have seen in branches where we will be jumping only to even addresses in the memory.
And that's where we are going to find valid instructions.
There is no need to jump to odd addresses, because we will not find the start of either 16 or 32-bit instruction at an odd address.
So we are going to jump only to even addresses. Therefore, the least significant bit of the offset has to be 0.
So we are using 20 bits to specify to cover the range that is 21 bits wide.
So the target is anywhere within 2 to the 19th locations, which are two bytes apart, plus minus 2 to the 19th locations to bytes apart, which means that this plus minus 2 to the 18th 32-bit instructions is our jump range relative to the current value of the program counter.
That gives us plus minus megabyte worth of code, which is quite a bit.
The only other thing to keep in mind here is that immediate encoding here is optimized in the same way as we had in the B format.
We also seen the common use pseudo instruction j, which is a jump where we do not save the return address.
So the way not to save the return address is to specify the rd as x0, which would discard the value stored there.
A couple of examples of using a jal — j pseudo instruction, j label is the same as jal without saving the return address to an address specified as a label.
And then, so that label will be calculated relative to the current PC.
And then, our other common use of jal is to call a function within 2 to the 18th instructions of a PC. So this would be done as jal ra with FuncName.
We are going to save the return address in ra so we know where to return. And then FuncName would be a label that is within the reach of this jump link.
And that's it. There is not much more to it. We have seen it before. There is one more jump instruction.
But we said, we had only one instruction in the j format. That's because jump and link register is of I type. It needs room for both the destination register and the source register.
So it uses the I format. So jalr is going to have its opcode, destination register, and the source register, and will be using only a 12-bit offset.
It could have had wider offset, but decided not to develop to add one more type of instruction format to RISC V. So we have seen that instruction.
Have been studying it in detail, so here it is in the detail. It is jal register specifying the destination the source register, and the immediate value where immediate is a 12-bit value.
So it again, does two things. It writes the next address, rd, the return address, of PC plus 4 in the rd and the destination register.
So we know how to return back. And then sets the program counter to a new value, which is equal to the value of the source register plus the immediate.
It's an important thing to notice here. This is the way to make a jump to an absolute address.
We will put a value in the source register, and specify any offset with respect to that.
Another important distinction, immediates here are not like in jal and in branches, because we are using the immediate format.
We can't just assume that the least significant bit is 0. It is there in the instruction. It is specified in the offset.
It is right there. There. So there is no multiplication by two bytes, so our range is a bit reduced.
Just keep that in mind. So the way how it does it, it calculates the sum of the sign extended immediate — like we always do in the immediate in the I format.
So we sign extend the immediate, add it to the source register, and then we always, by convention, set the least significant bit to 0 so that we always jump to even addresses.
Here are a couple of examples of use of jalr. So the one that we have already seen is the return or jr, jump register, with the return address.
That is essentially jalr x0, ra, and a 0. So our source is the ra, and we don't our return address. We are returning to our return address.
We don't need to save the destination. We don't have to save the current PC, and our offset is 0.
If you want to call function at any 32-bit absolute address, we can do that with jalr are with the help of lui.
So if we lui upper 20 bits of our target absolute address into register x1, and then jalr with the value of x1 and lower 12 bits that are going to be now added to that,
then we are there. We have called a function that is specified by a 32-bit absolute address.
Now jalr can also do relative addressing. The way how we do that — you said jalr — isn't that contradiction, you just said that jalr always uses obsolete addresses.
But we can fake it. We can load the current value of a PC by using auipc. So we can load upper 20 bits in x1.
And then jalr with a lower 12 bits to our relative destination with full 32-bit offset.
And that's it. That sums up all RISC V instruction formats. When I come back, I'll just sum up.
Đă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.