自制操作系统4:内核突破512字节

 参考:

https://blog.csdn.net/tyler_download/article/details/51970921

上一节软盘可读就是为此做的准备,上节是内核读取其他扇区的文本显示出来。本节将实验第一扇区是个引导程序,负责把其他扇区的内核读入内存,
然后跳转到内核所在内存运行,移交控制权。
引导程序:主要功能就是读取其他扇区的内核到内存中。
依次创建boot.asm, kernel.asm然后编译
nasm kernel.asm -o kernel.bat
nasm boot.asm -o boot.bat
然后制作启动盘:工具也要改造一下
https://github.com/wycl16514/OS-Kernel-from-loader-to-kernel/blob/master/src/Floppy.java
这文件与上节相同
https://github.com/wycl16514/OS-Kernel-from-loader-to-kernel/blob/master/src/OperatingSystem.java
这个文件有所改动。
writeFileToFloppy(String fileName, boolean bootable, int cylinder,int beginSec) 
区分boot扇区还是普通扇区。因为boot扇区要写0x55 0xaa
            if (bootable) {
                buf[510] = 0x55;
                buf[511] = (byte) 0xaa;
            }
增加了kernel.bat的写盘
    public void makeFllopy()   {
        writeFileToFloppy("kernel.bat", false, 1, 2);
        floppyDisk.makeFloppy("system.img");
    }
运行试试:

自制操作系统4:内核突破512字节_第1张图片

boot.asm

org 0x7c00;

LOAD_ADDR EQU 0x8000

entry:
  mov ax, 0
  mov ss, ax
  mov ds, ax
  mov es, ax
  mov si, ax
readFloppy:
  mov CH, 1
  mov DH, 0
  mov CL, 2
  mov BX, LOAD_ADDR
  mov AH, 0x02
  mov AL, 1
  mov DL, 0
  INT 0x13
  JC  fin
  JMP LOAD_ADDR
fin:
  HLT
  JMP fin

kernel.asm

org 0x8000;

entry:
  mov ax, 0
  mov ss, ax
  mov ds, ax
  mov es, ax
  mov si, msg
putloop:
  mov al, [si]
  add si, 1
  cmp al, 0
  je  fin
  mov ah, 0x0e
  mov bx, 15
  int 0x10
  jmp putloop
fin:
  HLT
  jmp fin
msg:
  DB "This is Hello World from kernel"

OperatingSystem.java

package fromloader;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class OperatingSystem {
    private Floppy floppyDisk = new Floppy();
    private int  MAX_SECTOR_NUM = 18;

    private void writeFileToFloppy(String fileName, boolean bootable, int cylinder,int beginSec) {
        File file = new File(fileName);
        InputStream in = null;

        try {
            in = new FileInputStream(file);
            byte[] buf = new byte[512];
            if (bootable) {
                buf[510] = 0x55;
                buf[511] = (byte) 0xaa;
            }
            while (in.read(buf) > 0) {
                //将内核读入到磁盘第0面,第0柱面,第1个扇区
                floppyDisk.writeFloppy(Floppy.MAGNETIC_HEAD.MAGNETIC_HEAD_0, cylinder, beginSec, buf);
                beginSec++;

                if (beginSec > MAX_SECTOR_NUM) {
                    beginSec = 1;
                    cylinder++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    public OperatingSystem(String s) {
        writeFileToFloppy(s, true, 0, 1);
    }

    public void makeFllopy()   {
        writeFileToFloppy("kernel.bat", false, 1, 2);
        floppyDisk.makeFloppy("system.img");
    }

    public static void main(String[] args) {
        OperatingSystem op = new OperatingSystem("boot.bat");
        op.makeFllopy();
    }
}

你可能感兴趣的:(自制操作系统,内核,操作系统)