系統:ubuntu22.10
----------kernel.cpp----------void printf(char* str) {
unsigned short* VideoMemory = (unsigned short*)0xb8000;
for (int i = 0; str[i]; i++) {
VideoMemory[i] = (VideoMemory[i] & 0xFF00) | str[i];
}
}
extern "C" void kernelMain(void* multiboot_structure, unsigned int magicnumber) {
printf("code name:freedom");
while(1);
}
----------makefile----------
GPPPARAMS = -m32 -fno-user-cxa-atexit -fleading-underscore -fno-exceptions -fno-builtin -nostdlib -fno-rtti
ASPARAMS = --32
LDPARAMS = -melf_i386
objects = loader.o kernel.objects
%.o: %.cpp
g++ ${GPPPARAMS} -o $@ -c $<
%.o: %.s
as ${ASPARAMS} -o $@ $<
mykernel.bin: linker.ld ${objects}
ld ${LDPARAMS} -T $< -o $@ ${objects}
install: mykernel.bin
sudo cp $< /boot/mykernel.bin
----------loader.s----------
.set MAGIC, 0x1badb002
.set FLAGS, (1<<0 | 1<<1)
.set CHECKSUM, -(MAGIC + FLAGS)
.section .multboot
.long MAGIC
.long FLAGS
.long CHECKSUM
.section .text
.extern kernelMain
.global loader
loader:
mov $kernel_stack, %esp
push %rax
push %rbx
call kernelMain
_stop:
cli
hlt
jmp _stop
.section .bss
.space 2*1024*1024
kernel_stack:
----------linker.ld----------
ENTRY(loader)
OUTPUT_FORMAT(elf32-i386)
OUTPUT_ARCH(i386:i386)
SECTIONS
{
. - 0x0100000;
.text :
{
*(.multiboot)
*(.text*)
*(.rodata)
}
.data :
{
start_ctors = .;
KEEP(*(.init_array));
KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*)));
end_ctors = .;
*(.data)
}
.bss :
{
*(.bss)
}
/DISCARD :
{
*(.fini_array*)
*(.comment)
}
}
代碼顯示可能有問題,請諒解
Bookmarks