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 assembly language.
We are going to continue with our function call example. Remember, we talked about six basic steps that we need to make when calling a function.
Let's go over them one more time because we understand much better what actually needs to happen. And we are going to reinforce that with an example. All right.
In the first step, the main program that calls the function needs to put the arguments in a place where the function will find them.
So we know that this is going to be a set of designated registers for that. Then it transfers control to the function by using a single instruction, which is jump and link.
What jump and link does, saves the return address and jumps to the location of the function, to the address of a function.
So at that point, the function is the one that has control of a processor, the sequence of execution of instructions, and its local registers.
So can just function makes itself comfortable, and at home, and just throw a party all over the space in the register? No. Right.
There may be variables that the main program cares about. They're stored in registers.
We cannot just destroy them and leave a mess inside the processor so the main program cannot continue, where it stopped, where it paused.
So we need to save variables that are in there somewhere in order to be able to use that space.
Of course, there'll be a little bit — some amount of a scratch space somewhere where we can scribble as much as we like.
But we cannot scribble over all the registers that are over there. It's like if you really want to throw a party, you need to put away all the planters, and vases, and so on.
And then after you're done with the party, you put them back on.
So the processor, when doing that — so the function will acquire the local storage resources that it needs to execute what it needs to do.
Then it will do its desired set of tasks and then needs to clean up.
So it will put the return value in the place where it has been agreed upon, in the register that has been agreed upon.
And then it will restore all the values of the variables that have been already in the processor before the function call and clean up everything else.
And then it will return control to the main processor by using a single instruction, which is return.
Return uses an address stored in a register to return to the place where it was called from, because there can be many locations in the main program that would be calling the same function.
All right. Let's take a look at the function call example. In this case, we are looking at the C function leaf. And leaf usually means a function that does not call any other function.
It is being called by other functions but does not call other functions. In this case, it has four arguments — g, h, i, and j. And it returns one value, which is f.
It is fairly simple. f is equal to g plus h minus the sum of i and j.
So these parameters, g, h, i, and j, will be in the argument registers a0 to a3.
And f will be using temporary space in s0 before it is placed back in a0. And perhaps we need one more temporary register, s1.
So we are going to be writing over s0 and s1. So where do the old values that were in s0 and s1 — or any other registers — go?
Where are they saved when we make a function call? And how do we restore them?
Well, you may think we can put them in registers. But we generally don't have enough registers for every function call. So they'll need to go to memory.
And there is an ideal place in memory, ideal structure in memory, organizational memory, that is being used for that. That's the stack.
And at this point, you should really make a connection between the meaning of a stack in C and how it is physically realized in assembly. It's the last in, first out structure.
What does that mean? Well, the last data that goes in stack is the first one that is going to go out. If you like, it's like a stack of plates.
So if you have a stack of plates, the last plate that you are going to put on top will be the first one that will remove from the stack of plates, unless you are my daughter from a few years ago that would like to somehow get the middle plate from the stack.
[INAUDIBLE] All right. There are two operations that we perform on the stack. Putting a plate on the stack, meaning piece of data — this is data — we put it on a stack.
That's called a push. When we remove a plate from the stack, it's a pop.
These things happen so quickly. And in some instruction languages, there are designated instructions that perform push and pop. But we don't have them in RISC-V.
The support that we have for the stack is in having a designated place where we keep the address of a stack. That is called the stack pointer, or sp.
By convention, in RISC-V, it is stored in the register x2. So x0 is a 0. x1 is our return address. x2 is the stack pointer.
But you may — well, stack is supposed to have the start and an end.
So how can we know where is the stack by just having a stack pointer, just one address, one 32-bit address?
Convention is to that the stack starts at the very top of the memory space and grows downwards.
So as we are increasing the stack, we are decreasing the address of a stack pointer. So when we push onto a stack, we decrement the stack pointer.
When we pop from a stack, we increment the stack pointer. All right. So what goes in the stack? Usually each function has a set of data that we'll need to put on stack.
And we'll call that a stack frame.
Stack frame will include the return address that may have already been saved if there is another function that is perhaps calling this function, that has called this function.
Then all the arguments need to go there and the space for local variables that are going to be used.
So if there are nested calls — which we are going to discuss in a little bit more detail in the next segment — these nested calls are going to be placing a set of stack frames onto the stack.
So these stack frames are contiguous blocks of memory. And the stack pointer tells where is the last one of them, where is the bottom of the stack.
So when we add a new stack frame, we move the stack pointer from one location to another. When the procedure ends, we move the stack pointer back.
Let's use yellow. We move the stack pointer here. And we free up that space on the stack.
The data may still be on the stack. But it is now available for the next function call to write over it. So that's the meaning of free, basically.
Now let's go back to our leaf. It needs to pass four arguments, as we have seen, for our four variables. And it needs to use register s0 and s1.
So let's take a look at what is the RISC-V code for leaf.
First, since there are two variables that need to be stored, we need to make room on stack for storing them.
So how much room do we need to — how many bytes do we need to start two integers? We need eight bytes. So we are going to decrement the stack pointer by 8.
We will write S1 and S2 on the stack. Remember the direction here. We are going from s1 to the address that is four bytes offset from the stack pointer.
And s0 is going to go for — at the location of a stack pointer. The previous frame is eight bytes away.
This sequence of instructions that prepares the function call, it is the first thing that we do in the function call is call the prologue.
Then we do our stuff. We are going to add the values. We'll add f and g, place it in s1.
We'll add i and j, place it in — the first one goes in s0, the second one in s1. And finally, we put the return value in a0.
Then we are ready to return. First, we are going to restore the two registers that we have saved.
We are taking them from their appropriate locations back to s0 and s1. We are going to put the stack pointer — we are going to free up the memory.
We are putting the stack pointer where it was before by incrementing it by 8. And we are ready to return. This is what wraps up a function call.
This is called the epilogue.
And finally, we jump back to the return address. This is also what we have called return.
And that is it. Now we have seen how does the function call look like.
Let's just one more time revisit how does the stack look like before, during, and after the function.
The stack pointer is pointing to a memory location before — to some memory location before the call.
When we make a call, we made room for two more, for saving two registers, by decrementing the stack pointer by 8.
Then when we are done with the call, we are incrementing the stack pointer and pointing to the old location that we have — where it pointed before. The data may stay in the memory.
But it is labeled as free. The next function call is going to scribble over it.
You'll find out that there are some security issues with that. Some people have figured out that one function's garbage may be another function's treasure.
And they may go and mine for some useful stuff in there. Malicious programs, for example. So that is it. I'll see you in a bit with nested function calls.
Đă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.