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] BORIVOJE NIKOLIC: Hi.
Welcome back to a new episode of RISC-V instruction encodings. We've seen four RISC-V instruction formats so far, so we're going to add one more.
That's the one that is going to help us work with long immediates.
What we have seen so far that are immediates have been limited to being 12 bits, but occasionally, we need them to be longer.
So let's try to see a mechanism how we can use all 32 bits as an immediate value.
But before then, let's recap how our branches work because that'll help us develop a bit of an intuition of what we are trying to do. Our branches have that immediate field.
In what we have done so far, we have hand-compiled the value that replaces the label in the branch.
That label corresponds to the offset — how many instructions away from our current program counter is our target branch.
Now, what happens if we move that instruction in the code?
Well, if you just move branch instruction ahead of another instruction that we had in our code, we have to clearly change the value that sits in the label.
That replaces the label because we have changed the relative distance between the two instructions, so the relative offset from the program counter is not the same.
But often what we are going to see will be dealing with a moveable code.
So most of these compilations are done by the assembly process, and we often will link different procedures in different functions with external libraries.
So the code may be moving in the memory or, most generally, will be moving in the memory.
So when we move a piece of our code — the entire piece of our code in memory that includes loops and branches inside of it — do we need to retarget all of our labels?
And the answer is no, and that's an advantage of PC-relative addressing, and there is also a term used for that that this is now position-independent code.
So if you move a whole procedure to a new location, all relative addressing is preserved.
There is another important thing to keep in mind is that the range of our branches, since we are really limited to 13-bit offsets where we drop the last bit, we keep the 12 bits in our instruction encoding, the range is limited to plus minus 1,024 instructions away
from the branch instruction. So what happens if we need to branch? Rarely, but it may happen that we have to branch to a location that is outside plus minus 1,024 instructions.
We need help. We can't do that with a single branch instruction and here is an example of how this will happen.
So in this case, we have an instruction branch if equal extend x0 to a faraway location that is outside our 1,024 range.
So this compares the value in the register x10 with a 0, and then branches to a far location if x10 is equal to 0.
If it is not equal to 0, that goes and executes the next instruction in sequence. Now, the way how we would do this, we are going to go flip this branch condition.
So we're going to branch on if not equal when x10 is not 0 to the next.
And next here is our next instruction that was supposed to be executed, but in between if x10 is actually equal to 0, we are going to put a jump.
And the idea here is that the jump is going to have a much longer reach than our branches.
So that's what we are going to see as a consequence of the next little section about long immediates. So there is a format that helps support long immediates.
This is a U-format that stands for upper immediate instructions or some people will say unusually long immediates in the instruction.
Remember, our immediates were limited so far to 12 bits in the instructions in the I-format, also in the branches and in stores.
And we got that by reusing one of the register fields and the funct7 field.
Now 12 bits is good, but if you want to load the entire 32-bit operand into a register — the entire 32-bit value into a register — then we are missing 20 bits.
So the U-format provides a way for getting these missing 20 bits into our register. So here is how the instruction looks like. It's fairly straightforward.
It has an opcode in the lowest 7 bits. There should be no surprise. This is the same field what we have seen before.
And there are two different opcodes for two instructions that we have there. The instructions are LUI and AUIPC. LUI stands for Load Upper Immediate.
Essentially, loads an upper immediate into a destination register rd and leaves the bottom 12 bits — resets the bottom 12 bits to 0's.
And then AUIPC adds upper immediate to PC to the program counter and stores the result in the destination register rd.
Now, there are two separate opcodes that we are using for these two instructions, and that's expensive. There is an interesting trick that is happening here. It's outside of this class.
That opcode is used across different variants of the RISC-V instruction set architecture RV32, 64, 128.
Now so what we need for this instruction is essentially 12 bits to say what kind of instruction it is and what is the destination register.
We have miraculously freed up the top 20 bits, and that's where we can put our 20-bit immediate. Very well, let's see how we use this.
So LUI writes the upper 20 bits in the destination with the immediate value and clears the lower 20 bits.
So when we would like to put in a 32-bit immediate into a destination register, we need to chop that immediate into two halves.
This has happened to some other Louis, which was a French king. Any similarities are accidental here. So we are going to cut this immediate into two halves.
First, we are going to LUI the upper 20 bits into the destination register that is shown in here in x10. That is going to put the upper five nibbles into that destination register.
It is going to leave the lower three nibbles as 0's. Then we can simply do add immediate of the value that contains the lower three nibbles.
So in this case, we would like to put the final value 87654321 into a register x10, and we did it in two steps.
In the first step, LUI put 87654 into the upper 20 bits or upper five nibbles, and then add immediate finish that by adding 321 to the bottom.
This worked fine, but there is a catch. It does not always work out of the box. Here is an issue. DEADBEEF is a completely legit hexadecimal number.
So how do we load DEADBEEF into the register x10? So let's try to follow the same procedure. First, we go and load the LUI — the first part, DEADB.
All right, that's good. So now, x10 contains the value DEADB000. And then if we add immediate EEF there, we end up with DEADAEEF.
Oh, what happened here? We were supposed to get DEADBEEF. We got DEADAEEF.
And the problem there is, remember, add immediate looks at the most significant bit of E and sign extends it.
So since it found a 1 in the most significant bit position of hexadecimal value E, it sign extended it all the way to the upper 32 bits.
So when adding these operands, in the upper five nibbles, it placed all 1's, which is equivalent to a minus 1.
That's why this bit B — the nibble that contained B — got decremented. So that's a problem. This doesn't sound right. So what do we do?
Well, we know that this is not right, but can't we just use add immediate unsigned? Isn't there an instruction like that?
Nope, there is no add immediate unsigned in RISC-V. Remember, we basically ran out of funct3 space and there was no room for another instruction in there.
So add immediate was voted off the island — add immediate unsigned was voted off the island. So we have to live with what we got here.
In order to add more instruction, we would have had to spend another opcode field. So what do we do? Well, we know what is going to happen.
We are going to have one bit lower value in the fifth nibble from the top, so we are going to, on purpose, make it a little bit bigger by a value of 1, so we have added plus 1 here.
This is supposed to be plus 1. So how do we set DEADBEEF in register x10?
We first write DEADC by using a LUI, and then we add immediate EEF value to that and that is going to set the correct value in register x10.
Now, there is good news. This is a known thing, known procedure, so every time we have that, a compiler can take care of things.
So there is a pseudo instruction, pseudo operation that is load immediate.
And whenever we try to load a long immediate into to register any, but in this case, x10, if we just write li x10 and the value, it is going to do the proper thing.
It is going to break it up into two proper instructions that will finish the job for us. So just don't do this LUI ADDI.
Just use li in assembly code, and you get yourself set with appropriate long immediate. That's it for LUI for now.
We are going to see a few more uses of LUI in the next segment, but for now, let's see one missing instruction that we have that we mentioned early on, which is AUIPC that adds upper immediate value to the current content of the program counter and stores the result in the destination register rd.
So this is great for PC-relative addressing because we can add offsets to the current content of the program counter. That sounds great.
In the simplest case, something that we really wanted to have early on, if we add a 0 — if we use 0 as the immediate value in AUIPC, we are simply storing the current value of the program counter in a destination register.
It's incredibly convenient. That's essentially our return address.
So by doing this, we can put an address of a label in register x10 by saying AUIPC x10, 0.
That's it. That wraps up upper immediates. We are going to see one final format after the break.
Đă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.