Hi, in this lecture
we are going to learn how to get memory map. We still use bios function to do it.
The service we use is called system map service
by interrupt 15, it returns a list of memory blocks to us.
Each block is 20 bytes.
The structure of blocks is shown here.
The first qword is 64-bit physical start address of the memory region.
The second qword is length in bytes of the region. The last dword indicates the type of memory with 1
being free memory which we can use. 2 being not available to us, etc. In our project,
we only collect memory region of type 1.
After we get the free memory information, the memory module will use the information to allocate memory
for our system.
OK, let's get started.
First off, we define the label.
get memory info start.
We pass e820 to eax
and the ascii code for smap to edx.
Then we save 20 which is the length of memory block to ecx.
Next we save the memory address in which we saved the memory block returned in register edi
and ebx should be 0 before we call the function. So we clear ebx using xor instruction.
At this point, we can call the service through interrupt 15.
If the carry flag is set for the first call to the function, the service e820 is not available.
We simply jump to not support.
If it returns the memory info successfully, we continue to retrieve the memory info.
So we define another label, get mem info.
First off, we adjust edi to point to the next memory address
to receive the next memory block. Each memory block is 20 bytes, here we add 20 to edi.
Then we pass the same parameters to eax, ecx and edx.
Note that ebx must be preserved for the next call of the function. So we don�t change ebx value
.
Call the service again
and test carry flag.
If carry flag is set this time, it means that we reach the end of memory blocks.
So we jump to get memory done
we define the label get memory done.
And change the message
to get memory info done.
If it is not set,
we test ebx,
if ebx is nonzero, we jump back to label get memory info
So here we use jnz instruction, which jump if zero flag is not set.
When we jump back to get memory info,
we continue querying the memory info.
Otherwise, we reach the end of the memory block and we are done.
OK, let's build our project.
As you can see, get memory info done message is printed. Before we wrap up this lecture,
one thing I need to mention is that since they haven't implemented print formatted function such as
print function in C language, we don't print the detailed information about memory map
we just retrieved in this lecture.
If you want to see the details about memory map, you can check section memory management
and use the image to boot the computer
and see the memory map info.