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 one more time to RISC-V assembly language.
This time, we'll talk about nested procedures and register conventions that support them. Remember how we do a function call in RISC-V.
Main program places arguments in known locations, registers a0 to a7 and does a jump-in link.
And that process saves the return address in x1 and then transfers control to the function by jumping to its location.
Then, the function will have to save those registers that have some value that the main relies on, well, by putting them on the stack.
Then, the function will do its own thing. It will put the return value in a0 and a1 and will return those restored registers from the stack.
And then, we'll do the return back to the main.
Now, what happens if there is another function, say function 2, that this function that has been called by the main needs to call. So there is basically a nested function.
The main call is a function 1, and function 1 needs to call function 2.
What will happen here is that we have only one place where we can put the return address where we know where to put the return address, which is the register x1.
That's the one that holds the return address. So it will be holding the return address from the main when we go to the function 1.
When function 1 calls function two, it will clobber that value, and function 1 will not know where to go back. Similar holds for the argument's registers.
So what are we going to do? Well, let's take a look at — if this was not clear, let's look a little bit more into an example.
So here is a function sumSquare that works on the integers x and y, and it in turn calls another function mult of x and then adds a y to that.
So if something calls sumSquare — some program, maybe it's another function — something called sumSquare, and now sumSquare calls mult. So there is a value in ra that sumSquare wants to jump back to be able to return, but this will be overwritten by that call to mult.
So what are we going to do? Well, we have a standard solution. We shall use stack. Actually, there is a straightforward way of doing things.
We take all the registers, all 31 of them, and we just put them on stack every time there is a nested call. But that is inefficient.
We'll rarely be using all 31 registers, and memory operations are expensive. They take time. And sometimes, we may get away without at all having to put things in the stack.
We may be able to reuse local register for something. So let's take a look at what we actually do.
We rely on putting some of the registers on the stack during a nested call and having some of them being clobbered.
In order to understand how this works, we have to always understand the relationship between the nested functions.
So the one that calls the function we'll call our caller. The one, the function that is being called, we'll call callee.
So caller — once again, caller — is the calling function. Callee is the function that is being called.
So when the callee returns from executing, the caller needs to know which registers have been clobbered and which ones are guaranteed to be safe to store the value that was there before the function call.
So the register convention essentially splits the registers into these, the ones that are saved and then the other ones that are volatile, or called temporary.
So every time there is a jal, we need to split the registers into these two groups. And that is done by convention in RISC-V.
So let's take a look at what is happening in these conventions. And, again, this is happening in order to reduce the number of unnecessary rights to the memory.
It tries to optimize some average number of registers that need to go to the stack, assuming that sometimes all of them will have to go to the stack or many of them will have to go to the stack, sometimes none or almost none.
So there are two categories of registers, those that are preserved across function calls. And caller can rely on these values being unchanged.
So the function, the caller function that calls the callee, doesn't have to worry about those. Whatever is left in those registers stays that way.
So the things that are automatically saved, you guessed one of them is clearly the stack pointer. Stack pointer is the one that is preserved across because of the mechanism of how the stack works.
There are a few other pointers that we are not going to touch here. There is a global pointer and a thread pointer and a frame pointer.
Those are generally touched by the compiler in the operating system. And then, there are saved registers.
There is a dozen registers, s0 to s11, that are saved, whose values we should not be — that are going to be untouched.
So they're not going to be scribbled over. And if we need to use them like we did in the previous example of a function call, the callee will need to restore their value.
And then, there's the second set of registers that are not preserved across function calls. So the caller cannot rely on these staying the same.
We'll call those volatile or temporary registers. So we already know that the return address and the argument return registers a0 to a7 are not going to be preserved.
They will need to go to a stack across the nested calls.
And there are also so-called temporary registers t0 to t6 if there is something that the caller has that does not want to be overwritten in the temporary registers.
A caller will need to save them before calling a function. Got that?
So here is a summary of which registers are saved and which ones are temporary.
They have their numbers that hardware understands, and then they have something that is called the application binary interface. These are their symbolic names.
These are human-friendly symbolic names in assembly code. That's how you can call them by in assembly. And then, here are their descriptions.
Some of these are familiar, and you have seen. The other ones that you haven't seen, you probably don't need to get deep into detail with them. But the important thing is here.
Who is the one that needs to save the contents of that register if there is a nested call?
Well, nobody needs to save x0. Return address needs to be saved by the caller.
Stack pointer is saved by the callee, taken care of. We don't care about these.
And then, the temporary registers, if there is something in the temporary registers that the caller would like to have saved during the function call, the caller needs to save them.
So the caller will need to put them in the locations where they are going to be preserved, stack or the saved registers.
And then, if after a function call the callee wants to use or needs to use the saved registers, they need to save those values and restore them because the basic logic here is caller
assumes that the values that are going to be in the saved registers are not going to be touched.
If they have something that matters to them in the temporary registers, in a scratch space, they better take care of that and put it somewhere where they can restore it from after the function call.
And that is basically it. We are going to take a look a little bit more of the operational memory after this 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.