我正在编写自己的内核(目前为 32 位),如果我在 main 上运行,命令就可以工作,但是当在不同的函数上运行时,它的行为会有所不同。知道如何以及为什么吗?编辑:问题在于其他函数内的指针不起作用。
int foo(const char *str)
{
*(char*)0xb8000 = *str; // clears the screen probably because of 0 padding at the end
}
extern void main() {
const char str[4] = {'A', 'B', 'C', '\0'};
*(char*)0xb8000 = *str; // works good
for(int i=0;i<1000000000;i++); // for delay
foo(str);
for(;;);
return;
}
我期望它表现相同,但事实并非如此
启动代码在这里
[org 0x7c00]
KERNEL_LOCATION equ 0x1000
mov [boot_disk], dl
; set up stack
xor ax, ax
mov es, ax
mov ds, ax
mov bp, 0x8000
mov sp, bp
// GDT loading to memory and other stuff
[bits 32]
start_protected_mode:
mov ax, DATA_SEG
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ebp, 0x90000 ; 32 bit stack base pointer
mov esp, ebp
jmp KERNEL_LOCATION
times 510-($-$$) db 0
dw 0xaa55
生成文件
all:
x86_64-elf-gcc -Wall -nostdlib -ffreestanding -m64 -g -c "kernel.c" -o "kernel.o"
x86_64-elf-gcc -Wall -nostdlib -ffreestanding -m64 -g -c "idt.c" -o "idt.o"
x86_64-elf-gcc -Wall -nostdlib -ffreestanding -m64 -g -c "system.c" -o "system.o"
nasm "kernel_enrty.asm" -f elf64 -o "kernel_entry.o"
nasm "start.asm" -f elf64 -o "start.o"
x86_64-elf-ld -nostdlib -o "full_kernel.bin" -Ttext 0x1000 "kernel_entry.o" "start.o" "idt.o" "system.o" "kernel.o" -j .text --oformat binary
nasm "boot.asm" -f bin -o "boot.bin"
cat "boot.bin" "full_kernel.bin" > "everything.bin"
nasm "padding.asm" -f bin -o "padding.bin"
cat "everything.bin" "padding.bin" > "OS.bin"
问题是我将 32 位代码编译为 64 位
更新后的make文件:
all:
x86_64-elf-gcc -Wall -nostdlib -ffreestanding -m32 -g -c "kernel.c" -o "kernel.o"
x86_64-elf-gcc -Wall -nostdlib -ffreestanding -m32 -g -c "idt.c" -o "idt.o"
x86_64-elf-gcc -Wall -nostdlib -ffreestanding -m32 -g -c "system.c" -o "system.o"
nasm "kernel_enrty.asm" -f elf32 -o "kernel_entry.o"
nasm "start.asm" -f elf32 -o "start.o"
x86_64-elf-ld -nostdlib -o "full_kernel.bin" -Ttext 0x1000 "kernel_entry.o" "start.o" "idt.o" "system.o" "kernel.o" --oformat binary -m elf_i386
nasm "boot.asm" -f bin -o "boot.bin"
cat "boot.bin" "full_kernel.bin" > "everything.bin"
nasm "padding.asm" -f bin -o "padding.bin"
cat "everything.bin" "padding.bin" > "OS.bin"