1 00:00:00,730 --> 00:00:08,410 Hi, in this section we will see how to handle the interrupts and exceptions. At this point, 2 00:00:08,410 --> 00:00:12,700 the kernel file is loaded into memory and we are going to jump to the kernel in this lecture. 3 00:00:13,240 --> 00:00:18,040 we relocate the kernel from 10000 to the memory address 200000. 4 00:00:18,910 --> 00:00:22,120 We open the loader file and start from where we left off. 5 00:00:23,360 --> 00:00:28,040 There is only one task we will actually perform here, that is, relocate our kernel. 6 00:00:29,090 --> 00:00:35,000 Remember we have read the kernel file from disk into memory 10000. Let’s check it out. 7 00:00:40,830 --> 00:00:44,400 As you see, the code here is loading the kernel into memory. 8 00:00:50,380 --> 00:00:53,410 We remove the code of printing characters. 9 00:00:55,330 --> 00:00:59,850 Loading the kernel is really simple. Here we use instruction move qword. 10 00:01:00,730 --> 00:01:01,780 Let's get started. 11 00:01:02,260 --> 00:01:04,420 First off, we clear direction flag 12 00:01:08,110 --> 00:01:13,750 so that the move instruction will process the data from low memory address to high memory address, 13 00:01:14,140 --> 00:01:17,230 which means the data is copied in forward direction. 14 00:01:18,790 --> 00:01:26,230 The destination address is stored in rdi register and source address is in rsi register. 15 00:01:27,040 --> 00:01:27,760 so we move rdi 16 00:01:28,860 --> 00:01:30,030 Two hundred thousand. 17 00:01:31,870 --> 00:01:33,760 and this is a hexadecimal number. 18 00:01:38,790 --> 00:01:39,810 move rsi 19 00:01:41,370 --> 00:01:42,330 Ten thousand. 20 00:01:44,720 --> 00:01:51,470 The kernel is in the memory 10000 right now and we want to copy it into the location 200000. 21 00:01:52,780 --> 00:02:00,220 Register rcx acts as a counter, since we want the move instruction to execute multiple times. 22 00:02:00,220 --> 00:02:03,220 Move qword will copy the 8-byte data each time. 23 00:02:06,000 --> 00:02:07,680 So we divide the value by 8 24 00:02:08,870 --> 00:02:11,810 and move the result to rcx. 25 00:02:13,470 --> 00:02:20,010 When we load the kernel from disk into memory, we read 100 sectors. Each sector is 512 bytes. 26 00:02:20,010 --> 00:02:24,780 So the value is equal to the size of 100 sectors. 27 00:02:26,590 --> 00:02:34,300 Now rcx specifies the times move qword instruction will repeat. In order to repeat the move instruction, 28 00:02:34,300 --> 00:02:35,500 we use prefix repeat. 29 00:02:37,240 --> 00:02:37,960 and move 30 00:02:38,900 --> 00:02:39,560 quadword. 31 00:02:41,080 --> 00:02:47,410 Ok, after the move instruction executes, our kernel is copied into the address 200000. 32 00:02:48,620 --> 00:02:50,480 There is one instruction left to write, 33 00:02:52,840 --> 00:02:58,630 which is jump instruction. We jump to 200000, 34 00:03:00,830 --> 00:03:04,250 the execution is transferred to the kernel and we are good to go. 35 00:03:06,270 --> 00:03:10,800 We haven’t had a kernel file up to this point. Let’s write a simple one. 36 00:03:13,130 --> 00:03:14,450 we create a new file. 37 00:03:16,900 --> 00:03:17,770 and save it 38 00:03:19,260 --> 00:03:20,370 as kernel file. 39 00:03:32,150 --> 00:03:34,690 all the code written now is in 64-bit mode. 40 00:03:36,950 --> 00:03:38,990 and the base address of the kernel is 41 00:03:41,320 --> 00:03:42,400 200000. 42 00:03:45,620 --> 00:03:47,030 Just like we did before, 43 00:03:49,500 --> 00:03:54,630 we print the character K to show that we are in the kernel file. 44 00:04:04,470 --> 00:04:07,320 And also we end our kernel by the infinite loop. 45 00:04:12,680 --> 00:04:19,630 Before we build our project, we modify the build script and assemble the kernel file, so we save 46 00:04:19,640 --> 00:04:20,420 these two files. 47 00:04:25,050 --> 00:04:27,300 And open the build script to edit. 48 00:04:30,090 --> 00:04:32,600 The command we use is the same as loader file. 49 00:04:33,880 --> 00:04:35,320 So we copy and paste here. 50 00:04:36,710 --> 00:04:39,320 the output file is called kernel.bin 51 00:04:42,730 --> 00:04:44,920 and the input file is kernel.asm 52 00:04:47,600 --> 00:04:49,010 And also, we need to 53 00:04:50,440 --> 00:04:54,000 write the kernel.bin into the boot image. 54 00:04:59,210 --> 00:05:04,700 And we specify we want to write 100 sectors into the boot image. 55 00:05:05,760 --> 00:05:13,290 Since the first sector is the boot.bin and then the next 5 sectors are the loader.bin. 56 00:05:14,850 --> 00:05:20,790 So the value we use is six, which means we skip the first 6 sectors of the output file. 57 00:05:22,000 --> 00:05:26,230 OK, we save the file and run the script in the terminal. 58 00:05:32,450 --> 00:05:36,440 OK, now we can open the boot folder and run the bochs. 59 00:05:39,610 --> 00:05:42,400 We can see character K is printed on screen.