1 00:00:00,470 --> 00:00:01,740 Hi, in this lecture 2 00:00:01,940 --> 00:00:06,920 we are going to learn how to get memory map. We still use bios function to do it. 3 00:00:07,640 --> 00:00:09,970 The service we use is called system map service 4 00:00:10,220 --> 00:00:15,860 by interrupt 15, it returns a list of memory blocks to us. 5 00:00:15,980 --> 00:00:17,660 Each block is 20 bytes. 6 00:00:18,820 --> 00:00:20,980 The structure of blocks is shown here. 7 00:00:22,370 --> 00:00:28,340 The first qword is 64-bit physical start address of the memory region. 8 00:00:28,340 --> 00:00:35,120 The second qword is length in bytes of the region. The last dword indicates the type of memory with 1 9 00:00:35,120 --> 00:00:40,610 being free memory which we can use. 2 being not available to us, etc. In our project, 10 00:00:40,610 --> 00:00:43,310 we only collect memory region of type 1. 11 00:00:43,850 --> 00:00:49,460 After we get the free memory information, the memory module will use the information to allocate memory 12 00:00:49,460 --> 00:00:50,670 for our system. 13 00:00:51,650 --> 00:00:53,080 OK, let's get started. 14 00:00:54,580 --> 00:00:58,450 First off, we define the label. 15 00:01:01,700 --> 00:01:03,140 get memory info start. 16 00:01:06,240 --> 00:01:10,260 We pass e820 to eax 17 00:01:16,420 --> 00:01:20,050 and the ascii code for smap to edx. 18 00:01:25,610 --> 00:01:29,930 Then we save 20 which is the length of memory block to ecx. 19 00:01:32,640 --> 00:01:38,310 Next we save the memory address in which we saved the memory block returned in register edi 20 00:01:43,390 --> 00:01:51,040 and ebx should be 0 before we call the function. So we clear ebx using xor instruction. 21 00:01:51,940 --> 00:01:55,540 At this point, we can call the service through interrupt 15. 22 00:02:00,220 --> 00:02:07,390 If the carry flag is set for the first call to the function, the service e820 is not available. 23 00:02:07,390 --> 00:02:08,889 We simply jump to not support. 24 00:02:11,770 --> 00:02:16,450 If it returns the memory info successfully, we continue to retrieve the memory info. 25 00:02:17,540 --> 00:02:22,130 So we define another label, get mem info. 26 00:02:25,490 --> 00:02:30,380 First off, we adjust edi to point to the next memory address 27 00:02:35,680 --> 00:02:42,580 to receive the next memory block. Each memory block is 20 bytes, here we add 20 to edi. 28 00:02:44,030 --> 00:02:48,830 Then we pass the same parameters to eax, ecx and edx. 29 00:02:54,460 --> 00:03:00,820 Note that ebx must be preserved for the next call of the function. So we don’t change ebx value 30 00:03:00,820 --> 00:03:01,210 . 31 00:03:02,170 --> 00:03:03,280 Call the service again 32 00:03:06,650 --> 00:03:08,210 and test carry flag. 33 00:03:11,070 --> 00:03:15,750 If carry flag is set this time, it means that we reach the end of memory blocks. 34 00:03:17,030 --> 00:03:18,670 So we jump to get memory done 35 00:03:23,350 --> 00:03:25,900 we define the label get memory done. 36 00:03:28,790 --> 00:03:30,110 And change the message 37 00:03:32,500 --> 00:03:34,600 to get memory info done. 38 00:03:38,830 --> 00:03:40,240 If it is not set, 39 00:03:44,660 --> 00:03:45,890 we test ebx, 40 00:03:50,890 --> 00:03:55,000 if ebx is nonzero, we jump back to label get memory info 41 00:03:56,230 --> 00:04:01,690 So here we use jnz instruction, which jump if zero flag is not set. 42 00:04:05,380 --> 00:04:07,630 When we jump back to get memory info, 43 00:04:08,850 --> 00:04:11,010 we continue querying the memory info. 44 00:04:12,100 --> 00:04:15,430 Otherwise, we reach the end of the memory block and we are done. 45 00:04:17,010 --> 00:04:18,930 OK, let's build our project. 46 00:04:30,610 --> 00:04:36,820 As you can see, get memory info done message is printed. Before we wrap up this lecture, 47 00:04:36,880 --> 00:04:43,150 one thing I need to mention is that since they haven't implemented print formatted function such as 48 00:04:43,150 --> 00:04:49,870 print function in C language, we don't print the detailed information about memory map 49 00:04:49,870 --> 00:04:51,430 we just retrieved in this lecture. 50 00:04:52,030 --> 00:04:56,920 If you want to see the details about memory map, you can check section memory management 51 00:04:57,850 --> 00:04:59,980 and use the image to boot the computer 52 00:04:59,980 --> 00:05:01,750 and see the memory map info.