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.
Can you believe that we're almost done with the RISC-V ISA and the assembly language?
I'm getting tired too. So let's get going with wrapping it up. The one last thing that we need to do is quite exciting.
It is going to tie the memory allocation that we have seen in C with what is happening with memory allocation in RISC-V. So that will hopefully really paint a clear picture of what is happening with these mallocs and things like those in C.
So let's get to it.
There are two types of variables that we have seen in C. There are automatic variables that are local to a function and appear when the function is called, and then are discarded when the function exits.
And then there are static variables that exist all the time across entries and exits into the procedures.
So when we need these local variables that don't fit in the registers, we will need to start them on the stack.
So this stack frame that we have mentioned before not only has the return address and some of the registers that need to be saved, but also contains all these variables that cannot fit into the registers.
So that's why it's called the procedure frame or procedure activation record. It's a segment of a stack that has all the same registers and local variables that are needed to run that procedure.
Let's take a look at how does it look like. We have seen this before. Before we call function, stack pointer is sitting at some address.
When we call a function, we allocate the space on the stack to save the return address, to save the arguments because they may get overrun by a nested call, and then save the registers need to be on the stack as well.
And in addition to that, any of the variables that don't fit in the registers will be there.
And that piece can be fairly large if you're working with matrices or some large chunks of data.
And then when you're done with the call, we clean up by incrementing the stack back to the starting position here.
Remember, stack decrements when we add a frame to it and increments when we remove the frame. Don't mix this up.
So how do we actually do this? How do we implement? What happens with the stack when we implement this nested function that we have seen before?
Remember, we have a function some square that works on integers x and y and calls within itself another function that multiplies s with x, and then adds y before it exits.
So it's a nested function call. Let's take a look at what do we need to put on the stack when we make this call.
What is going to be really helpful is to understand what is where and what is going to get overwritten, what is going to be clobbered.
So what are the arguments of sumSquare?
X is going to be living in a0 and y will live in a1.
Now when we call mult, mult will have also two arguments. Both times it will be x sitting in a0 and a1.
So we can leave one value of x in a0, but the value of y that was in a1 will get overwritten. What else is going to get overwritten during a nested call?
The return address.
Remember when sumSquare gets called by the main, we put the return address where the sumSquare will be going back, where will it jump back, but will it return.
But now when we have a nested call, sumSquare becomes a called and mult is a callee. We need to save the return address for mult, where will mult go back.
And that's the same spot. It's the register x1. So the old return address is going to get overwritten. sumSquare will have idea where to go back when it's done.
So the two things that we need to save, therefore, are the return address and the y that lives in a1.
So we need to make room for two registers on the stack, therefore we decrement the stack by 8. Never make that mistake.
And then we're going to write the return address and a1 on the stack.
In order to set up the mult call, in order to call mult, we need to put its arguments in the right place.
So we need to copy the value of x from a0 into a1.
And remember, move instruction is a pseudo instruction that essentially just copies the value from one register to another. There is no move.
The original value still stays in a0. We just get a copy of it in a1. It's an add immediate with a0. So why is it called mult?
It's a tough question that I think belongs to that originates from Motorola's microprocessors in '70s. And why did they call it mult?
We don't know, but everyone now calls it multi, although it's just a copy. So we are ready.
We have x in both a0 and a1 registers, so we can call a mult with those arguments. So we are going to call mult here.
And mult is going to return the result back in a0. Now, what do we need in order to complete our sumSquare instruction?
We need to get y back from the stack because y is gone to the stack. We don't know.
We saved it to a stack so that we can call it back, so we're going to put it back in a1.
We are going to finish our additional the result of mult to y or y to the result of mult, and then we need to clean up.
We put back, we recall from the stack the return address. We clean up the stack pointer.
We increment the stack pointer by 8 to put it to the previous value, and then we are ready to jump back to the return address.
These three instructions at the top is what corresponds to a push to the stack.
And the three red instructions in the bottom correspond to the pop.
In some CISC architectures, you'll find out that one push instructions pushes multiple resistors to a stack and similarly, pop instruction recalls from the stack.
What we have here is basically a procedure of saving these registers and the arguments of the function on the stack and recalling them back so they can be used to complete our original function.
And remember here, sumSquare is a caller. Malt is a callee.
A few more things to tie this together with C. When we run C, there are three basic memory regions that get allocated.
We have static variables that are declared once per program. There is a heap that is dynamically allocated by a malloc. And there is stack that is used during procedure calls.
Where does this live in memory? In RISC-V, this is set by the convention. It is specific to RV32. It's a little bit different for RV64 and RV128.
One thing to keep in mind all these memory regions need to be aligned with a 16-bit boundary such that everything is compatible also with RV128, but you don't have to worry too much about that and I was not paying attention to that.
So stack starts with the top of the memory, which is bfff_fff0 aligned with 16 in RV32.
RV32 programs are on the bottom of the memory — very close to the bottom of the memory and not at the very bottom. The very bottom is reserved for special stuff.
I'll mention that in a second. Programs go from the address 0001_0000.
And then static data segment is right above the text, what we call above the program. And this global pointer points to the static.
So it is 1000_0000 hex. And then finally, there is a heap that is above static.
And that's where we place our data structure that we malloc. So heap will go up towards the stack. Stack will go downwards. If they meet, we are out of memory for now.
Let's see this pictorially. On the top, we have the stack going downwards, and then having data on the heap goes up.
Static data is allocated in the middle and hex is above from the address 1000_000 hex. What is in the bottom?
Well, that's where some of these system calls are.
That's where we would write to a printer or a display and things like those IO devices live down there, things that we'll see later — interrupts and so on.
Generally stuff that you should not be scribbling over, otherwise some strange things may happen. And that's it.
I've just wrapped up the entire ISA that we need to know for RISC-V. We have learned most of the instructions, and we're going to go and summarize that in just a bit.
See you there.
Đă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.