gpt4 book ai didi

assembly - 从程序集引导加载程序调用C内核

转载 作者:行者123 更新时间:2023-12-04 22:30:47 26 4
gpt4 key购买 nike

编辑:请跳到我下面的第二篇文章...

我正在寻找一种从Bootloader进入内核的极简方法。
您有任何可行的例子吗?

这是进入保护模式的 bootstrap :
“boot.asm”

[org 0x7C00]

mov bp , 0x9000
mov sp , bp

cli
lgdt [gdt_descriptor]

; Enter PM
mov eax, cr0
or eax, 0x1
mov cr0, eax

jmp 0x8:init_pm


[bits 32]
init_pm :
mov ax, 0x10
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax

;Tried to call my C function from here
call 0x8000 ; Kernel entry point

jmp $


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[bits 16]

GDT:
;null :
dd 0x0
dd 0x0

;code :
dw 0xffff ;Limit
dw 0x0 ;Base
db 0x0 ;Base
db 0b10011010 ;1st flag, Type flag
db 0b11001111 ;2nd flag, Limit
db 0x0 ;Base

;data :
dw 0xffff
dw 0x0
db 0x0
db 0b10010010
db 0b11001111
db 0x0

gdt_descriptor :
dw $ - GDT - 1 ;16-bit size
dd GDT ;32-bit start address


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Bootsector padding
times 510-($-$$) db 0
dw 0xaa55

在这里,我想从 bootstrap 中调用我的内核入口点:
“内核.c”
void main() {
char * vga = (char *) 0xb8000 ;
*vga = "X";
}

谢谢你的帮助,

到目前为止,这是我尝试执行的操作,但是不起作用:
我只是使用ld将内核设置为0x8000并使用cat进行连接,然后从引导加载程序跳转至0x8000
nasm boot.asm -f bin -o boot.bin
gcc -ffreestanding -c kernel.c -o kernel.o
ld -o kernel.bin -Ttext 0x8000 kernel.o --oformat binary
cat boot.bin kernel.bin > os-image

最佳答案

哦,这绝对可以。确实,这似乎只是您对C的了解。

我对您的C内核进行了一些更改,并得到了以下信息:

void main (void) 
{
unsigned char* vga = (unsigned char*) 0xb8000;
vga[0] = 'X'; //need to make sure that this is a character
vga[1] = 0x09; //append the attribute byte
for(;;); //make sure our kernel never stops, with an infinite loop
}

这似乎为我解决了问题!现在一切工作正常,只需在该文件中编写一个shell,然后再执行BAM!你去!

关于assembly - 从程序集引导加载程序调用C内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27051471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com