我构建了一个包含三个文件的简单操作系统,但遇到ogg问题,文件在这里:
loader.s
.extern kernel_main
.global _start
.set MB_ALIGN, 1<<0
.set MB_MEMINFO, 1<<1
.set MB_MAGIC, 0x1BADB002
.set MB_FLAGS, MB_ALIGN | MB_MEMINFO
.set MB_CHECKSUM, - (MB_MAGIC + MB_FLAGS)
.section .multiboot
.align 4
.long MB_MAGIC
.long MB_FLAGS
.long MB_CHECKSUM
.section .bss
.align 16
stack_bottom:
.skip 4096
stack_top:
.section .text
_start:
mov $stack_top, %esp
call kernel_main
hang:
cli
hlt
jmp hang
.size _start, . - _start
和kernel.c
#include <stddef.h>
#include <stdint.h>
const int a = 10;
void kernel_main()
{
uint8_t* video = (uint8_t*) 0xB8000;
const char* hello = "Hello World! From MYOS";
for (size_t i = 0; i < 23; i++)
{
video[2 * i] = hello[i];
video[2 * i + 1] = 0x07;
}
}
和linker.ld
ENTRY(_start);
SECTIONS {
. = 1M;
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
}
.text BLOCK(4K) : ALIGN(4K)
{
*(.text)
}
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
}
}
我编译这些代码:
i686-elf-gcc -std=gnu99 -ffreestanding -g -c start.s -o start.o
i686-elf-gcc -std=gnu99 -ffreestanding -g -c kernel.c -o kernel.o
i686-elf-gcc -ffreestanding -nostdlib -g -T linker.ld start.o kernel.o -o mykernel.elf -lgcc
我运行此内核,但没有找到多引导头。 如果我将kernel.c修改为:
#include <stddef.h>
#include <stdint.h>
const int a = 10; // Just add this line.
void kernel_main()
{
uint8_t* video = (uint8_t*) 0xB8000;
const char* hello = "Hello World! From MYOS";
for (size_t i = 0; i < 23; i++)
{
video[2 * i] = hello[i];
video[2 * i + 1] = 0x07;
}
}
它的工作!为什么呢?