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ả.
DAN GARCIA
And welcome back. We're at our final lecture. CALL, we saw the whole process. Let's actually take an example.
Let's take the most complicated program ever — not. Let's just do Hello, World. So this Hello.c is not your normal Hello, World. I've got a main.
I'm ignoring my argc, argv, for now. I've got a printf. But my printf isn't just Hello, World, my printf has two arguments.
It is "Hello" string, and then I have a percent, s, and then the "world" is going to be fed into that percent, s. So I really have two arguments to that printf, two strings.
And one string has a percent, s formatting directive. And the other is the other string, "world." And that's going to fit into that Hello, World to make Hello, World.
And I return 0. That's it. So I say I'm done. I save this.
I include my library, stdio, so I know about printf. And now I compile this. What do I get? This beautiful piece of code you could have hand-authored.
You understand this. But let's actually take a deep dive into what every single line does. Let's do this together. So first I have a .text segment.
A text segment says that the following is going to be my code. So .text says that, the directive for that.
.align 2 says I need to align this code to "2 to the whatever that number is" bytes. So I'm saying this is going to be word-aligned, aligned to every 32 bits.
This is saying the following is a global symbol. That main label that you're seeing here, I'm declaring that to be a global symbol other people can know about.
Okay, I've got my main label.
As I have with most functions, if you're going to make any function call from there and then return, I've got to be able to make room for ra and maybe other arguments.
So here I am. I'm going to reduce the stack pointer by 4 bytes to allocate some stuff here.
Now, first thing I always do is I store ra into sp 12, the top guy, the top of that frame. That's my frame. I'm going to store ra there.
So if I make a function call, I'll be able to store that and get back to where I was. That's pretty cool. Now I've got two strings I'm going to reference.
If you remember this line — let's go back. I got two strings. Here's two strings. These two strings needs to be set up as arguments to printf.
So how do I set those up? Well, let's compute the address of string1. Here's string1 down here. Here's string2 here.
Notice the formatting string is in string1. Okay, so I say lui addi.
I get that high 20 bits, put them in the upper immediate area, and then I addi the low 12 bits and I put that in there. Okay, then I do the same thing here.
So this is, in some sense, this is going to be my load address of string1 and string2 into a0 and a1. Because remember, I want to call printf.
Printf has two arguments, a0 and a1. So here it is. Call printf. I've set up a0 and a1.
I call printf. Now I'm done. So now I restore this, restore ra, fix the stack pointer, load into a0 my return value.
I put that 0 in here, and I say return. It's pretty clean. .section .rodata says I'm going to be in a read-only data area from here on in.
Okay, so .rodata says — not 0, r-o. R-o means read-only data. I'm saying this is going to be — I'm aligning this to 4 bytes. This is a little bit different than this align.
This says how many bytes I want to align the following. And now I align these based on a word align. And now below here is my data. Okay, that's it.
Now, you're going to say, am I done? Well, that's the .s file. Is there more work to do at the next stage? Well, let's see what we've got.
Let's see what the next stage is. We assemble this down. Here's my main code. Okay, starts at 0.
Every single guy starts at 0. This is a file by itself. So I start this at 0, and then eventually the final resting place has a different value.
But I'm starting here from 0. And now this is assuming that this is like the first instruction. This is 4. That means that's going to be this instruction.
Okay, so it's basically saying, these are the addresses of what these guys are. And this is just the file. So in this file is just this.
Okay, just make sure you know this is all that contains in that file, nothing else. This is just for you to see that. This is just for you to see what's happening.
And this is just for you to remember where things are. But only this stuff here, this is all that's stored in a .o file. Okay, does that make sense?
All right, so remember, we had to make room on the stack. We had to make room for 4 bytes on the stack. I store ra. Now, I've got my lui addi.
That's for my first string. That's for my a0. That's for string1. Here's my lui dddi for string2.
I still got 0's here. I don't know where those values are going to be. So I'm just putting 0's. I'm letting you know that this is what's going to be here.
And what's this one? Do you remember what this is right here? What's this line?
Do you guys remember what that next thing — after I set up a0 and a1, what's the next thing I do? You're right, I've got to jal, I've got to call printf.
This is the call to printf right here. Okay, got to remember what that is. Then I restore my stack.
Sorry, I load back ra, I restore my stack, I load in a0 as my return value, and I return. Okay, so that's what main does.
And all I got to do is figure out how to then — so those guys in red, that's the job of the? Linker. Well done. So what do I do?
I fill those in. This is the value of string1. Bloop.
This is the value — not really the value, but this is what has to be there in those values to be able to handle string2.
Now a0 has some combination of this, the upper 20 and lower 12, upper 20 lower 12 for string1 and string2, i.e. a0 and a1. This is where printf lives.
Okay, love that. And well, that's at least that 20 bits there for that. And then that's all I need to do.
Now, you're going to say, well, that was great, except that how did you calculate those values for this?
Okay, so the target address for string1 one is 20 in hex in the upper 20 bits and A10 in the lower 12 bits. Sounds good. That doesn't seem crazy.
So here's my instruction sequence. lui, here's 20 here, and addi the lower 12. Seems reasonable there, except it doesn't work. Why doesn't it work?
Immediates in RISC-V are always sign-extended. We knew that, but maybe we forgot about it. A — if you remember, A is 1010 — has a 1 in the upper bit.
So the highest, the 11th bit by index, but the 12th bit by counting from 1, that's a 1. Well, when I sign-extend, that's going to be all 1's.
So it looks like this. I sign-extend, it's going to be all like this, all 1's. See that? All 1's.
By the way, what's all 1's? If I were to say, think about, remember two's complement, what's all 1's? Negative 1.
So if I just look at the upper 20 bits, and just add this value to this value, just forget it. Again, add only those guys. This is all zeros.
So that's not going to affect the upper 20 bits. Okay, A10 is going to be the lower 12 bits. So I'm fine with that.
But if I just consider those two numbers, even though they're the upper 20, if I just consider them by themselves, all Fs, which is really minus 1, and 20, what do you get?
You get 1 less than 20, which is 1F. So I'm off. I'm off by exactly that 1.
This is actually — because this counts for a negative 1, I'm actually off by that 1 if that makes any sense.
So to fix it, I take my 20, I add 1 to it to compensate for the fact that, when I sign-extend it, I'm going to lose the 1.
And then when I add the A10 on the lower 12, sign extend it — remember, you can only add 32 bits to 32 bits in the system.
So it's not like, oh, I'll only add the 12. No, you got to add 32 to 32 to get 32. It's what our ALUs do, our arithmetic logic units.
They only know how to add 32 to 32 to get 32. So then I'll get the right answer, 20 A10.
So I've got to have this compensating factor of this extra — it's actually 2 to the 12. I got this extra 2 to the 12 that's up there to do that.
So what is this? So by the way, how do I figure out what this value is? What do I figure out what this total value was? This is going to be the immediate, right?
The immediate that I put in there is going to be the A10. Well, if I sign extend it to all Fs what does that become?
Well, if I have a number and it looks like it's negative, I have had this number, what is it? Let's figure out what it is. All Fs means the upper guy is a 1.
If the upper guy's a 1, two's complement means it's a negative number. How do I figure out what that value is? Invert the bits and add 1, and that's what the negative number was.
Okay, so let's take a look at this. Let's complement all Fs and A10. Well, if I flip all those, it becomes a 0. Flip A. A is 1010.
Remember, 1010 for A. Well, the opposite of that is 101. So there's a 5 there. So that 101 becomes this five.
This 1 is 1 off of — well, flip that, it's all 1's except for the lowest guy. That's an E. So this guy is an E.
If it's a 0, flip those bits, it's an F. 5EF is my value. Let's add that 1 now again.
5EF, remember, to figure out what the two's complement is, I flip it and add 1. So 5EF plus 1 is 5F0 is 1520.
Okay, so that means this number was negative 1520. Okay, so minus 1520. So here we go.
Instruction sequence is minus 1520. Addi. And by the way, let's do this, here we go. Bloop.
This calculates 20 A10. So I go back here. So therefore what I have is this number is minus 1520, which is what I want.
I want that number. And I want this one to be 21. Remember? So the plus 1 goes up there.
I need to add 2 to the 12. That's 21. The lower side is minus 1520. And that calculates the address of string1.
So remember, lui, 21 in nex, addi, minus 1520.
And that's why — look, look — this is lui, 21, addi, 1520. Huh? Okay.
Pretty cool, right? Boop. In conclusion, what does a compiler do? It takes a high-level language — here's C — converts it down to the assembler language.
Which you can have different assembler languages. If you change to a different ISA, it'd be a different assembly language. RISC-V is different than MIPS. Okay, so those are different assembler languages.
We're living in the RISC-V world now. Assembler removes the pseudoinstructions.
It sets up two tables, a symbol table of all the symbols it knows about in that file, and a relocation table of all the things that need to be fixed in the linker stage.
The linker goes to the symbols, takes all of them in aggregate, figures out, are there any duplicate symbols? Error. Are there any missing symbols? Error.
Okay, if I'm calling somebody but I have no symbols there, error also.
Once that's all covered, I have to now place them all together, concatenate all the text first, all the data next, and fixing those links.
I've got a, now, working a.out, and makes use of whatever debugging that persists, that continue there. Okay, so whatever other header information in an a.out file. Done.
The loader takes that, as we said before, sets up the stack, takes a disk file, makes some memory for the system, copies the text and data into that memory.
This is a summary of the whole lecture in one minute. Copies all that text and data from a.out into that memory. Sets the stack pointer with any arguments on the command line.
Sets up a0 and a1 to be the value of argc and argv, and sets the stack pointer, clears the registers, and says go, and watches back, happily as a clam, as this thing continues to run.
And when it's all done, whatever the return value was, it passes it back through, through the OS to the top level, and we're all done. Shoo, amazing. That's the goal.
That's the high-level picture. And you now understand how compiling, assembling, linking, and loading works in a RISC-V system. Congratulations. Give yourselves a hand.
High five. Boop. Well done. Good stuff.
We'll see you folks at the next module. Take care.
Đă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.