‫In this lecture, we're going to cover potential alignment issues that you could end up getting if you
‫don't follow this lecture is lectures designed to avoid them.
‫So if we go to link L.D., you'll notice our output format is binary.
‫This is all fine.
‫But when we start mixing C with assembly, it can cause alignment issues.
‫Now, C requires functions to be aligned in a certain way.
‫And when we are writing assembly in Canada, HSM, for example, and we're linking with object files
‫created with a C compiler.
‫Right, our assembly might not align correctly.
‫And this will then mean that all those functions compiled, all those functions that were compiled.
‫The compiler assumed the it'll always be aligned, but because our assembly wasn't aligned, it ended
‫up making it not aligned when it's loaded into memory.
‫So then it can.
‫So then there can be problems such as call instructions, go into the wrong addresses.
‫And all this sort of stuff is really nasty stuff.
‫And the reason this is happening is because our assembly shares the same section as our object files
‫created with the C compiler.
‫So you can change the alignment of the C compiler, but we're going to keep all the defaults.
‫Now, our assembly does need to be aligned to 16 bytes because that's what the C compiler is using by
‫default.
‫Right.
‫So if we did five, 12 and we did modulus 16, we can see that gives us a zero, which means that if
‫we use the value, which means that if we align our kornelius file to 512 bytes, it will be aligned
‫by default.
‫Now, we're going to use another method after this.
‫And what our method is, we're going to create a new section for assembly files.
‫OK, and then in our linker script, we're going to make sure that section is at the very end here,
‫because that ensures that all of our C code in the text section will never have the alignment damaged
‫due to our assembly, because our assembly is way past the data, way past here, all the way at the
‫end is the last section.
‫So so it can't move anything out of alignment, which is a good idea.
‫Right.
‫But the only problem is we can't use our assembly section in Canada Assembly because our output format
‫is binary.
‫So it's very important.
‫Our kernel HSM file is always the first file to be linked because then that way when we jump to zero
‫one zero zero zero and so on, it will jump to the kernel diesem file and not some C code.
‫OK, because the idea is when we start writing our C-code, we will actually jump in our assembly here
‫to the C to the C function.
‫That's that's the idea.
‫Right.
‫But obviously in our makefile, if we have a bunch of C object files created in C before this, then
‫Canada ESM will not be the first thing to run.
‫So that is why we can't put it in the ASRM section, which we're about to create, because it will be
‫at the back here and we can't have that.
‫So how do we align Col Diesem then?
‫Because if we can't print the back then Col Diesem might damage the alignment.
‫So how do we get around it.
‫Well that's very simple.
‫We got about Diesem scroll down.
‫Remember how we aligned it to five hundred and ten bytes here times five 10 DB zero.
‫Just copy and paste this.
‫At the bottom here.
‫Change it to five, 12.
‫It's that easy and we know that five 12 MODULA 16 gives us zero.
‫So that that alignment is perfect, right?
‫Five, 12, 16.
‫There we are, because it gives us zero 512 bytes.
‫Aline's into 16 perfectly fine.
‫There's no problem.
‫So this solves any alignment issues.
‫Coal is caused by a kernel diesem file, but now we need to create the ASRM section.
‫So go to Link Aldy and we're just going to go to s.M.
‫And it's really that simple.
‫There we go, and now whenever we want to have our assembly file in the same section at the very top
‫of the file, we just go section diesem.
‫It's that simple.
‫But obviously this because this is Colonel Diesem, as I just explained, we need that in the tech section
‫because if we did do section DOT, as it would be at the very back here so that it would not be the
‫first code that gets run when we jump to our colonel protected mode in our bootloader here where we
‫jump to zero one zero zero zero and so on, it jumps Colonel Diesem, because first place in the tech
‫section and secondly, because it's the first object file here to be linked.
‫So that's incredibly important.
‫All other object files have to go after this one.
‫If they're in front, they will be the first to be run.
‫And that is not what we want.
‫And again, you know, if HSM was at the beginning of the file here, then we could do section DIESEM.
‫But then obviously what then happens is we risk a damaged alignment for our C-code.
‫So it has to be at the back.
‫OK, so now you've done that, that should solve any nasty alignment problems that you might face later
‫on in the course.
‫So let's just make clean that and we're going to go build that.
‫S.H. can see it builds absolutely fine.
‫So that worked.
‫And you know what?
‫We're going to do one more thing.
‫We're going to align the sections themselves as well, just for good measure.
‫So we just go a line four nine six here, a line four nine six, align four nine six.
‫Because really we want to be working with aligned memory anyway.
‫Right.
‫Especially when we start working with pages.
‫But we'll get to that and then we just go line four, nine, six.
‫And if we now make clean build, that can see that works fine.
‫So now all of the sections are aligned as well.
‫So just an overview of what's happening then.
‫We've ensured everything in the text section is aligned by 16 bytes.
‫We know this because the C compiler does this by default.
‫And secondly.
‫Because in our Colonel ASEM, we just aligned it by here because by doing the times five, 12 to zero
‫were basically forcing it to be 512 bytes long.
‫Always, even if it's slightly under, say, 256 bytes long, while it will get extended to 512 bytes
‫long, as explained in the bootloader tutorials, so that ensures that there's alignment with all our
‫C code.
‫Now, this ensures alignment with our sections themselves, OK?
‫And assume all of our HSM code inside there.
‫It's not aligned, but that's absolutely fine because it's not going to interfere with anything because
‫it's at the back by here.
‫OK, and obviously we do align the section, however.