简体   繁体   English

参数未传递给c Kernel中的函数

[英]Arguments not passed to function in c Kernel

I'm currently working on a home-made OS project. 我目前正在开发一个自制的OS项目。 I've succesfully written a bootloader which switches the computer into protected mode and loads the kernel, which is written in c. 我已成功编写了一个引导加载程序,它将计算机切换到保护模式并加载内核,该内核用c编写。 My kernel loads fine, but I have a problem calling functions outside of main(). 我的内核加载正常,但我在调用main()之外的函数时遇到问题。 The function seems to execute fine, but from what I can tell none of the arguments are being passed. 该函数似乎执行正常,但从我可以告诉我没有传递任何参数。 I can't figure out why this is, and hope someone here can tell me what my problem is. 我无法弄清楚为什么会这样,希望有人能告诉我我的问题是什么。 Below is the code to my kernel, as well as the commands used to compiler it. 下面是我的内核的代码,以及用于编译它的命令。

EDIT: After rereading my question I think I need to clarify what I mean when I say that arguments are not being passed. 编辑:在重读我的问题后,我想我需要澄清我的意思,当我说没有通过论点。 When I call print_char, no matter what values I pass as arguments it has no effect. 当我调用print_char时,无论我作为参数传递什么值,它都没有效果。 However, if I modify the variables inside of the function it works fine. 但是,如果我修改函数内部的变量,它可以正常工作。

kernel.c kernel.c

#define VIDEO_ADDRESS 0xb8000
#define WHITE_BLACK 0x0f

void print_char(char character, int col, int row, char att_byte) {
    unsigned char* vid_mem = (unsigned char*) VIDEO_ADDRESS;
    int offset;

    if (!att_byte) {
        att_byte = WHITE_BLACK;
    }

    offset += 2*col;
    offset += 80*row;

    vid_mem[offset] = character;
    vid_mem[offset+1] = att_byte;
}

void start() {
    clear_screen();
    print_char('X', 0, 0, WHITE_BLACK);
}

build.bat 运行build.bat

nasm boot.asm -f bin -o boot.bin
nasm kernel_entry.asm -f elf -o kernel_entry.o
gcc -ffreestanding -c kernel.c -o kernel.o

ld -T NUL -o kernel.tmp -Ttext 0x1000 kernel_entry.o kernel.o

objcopy -O binary -j .text kernel.tmp kernel.bin 

type boot.bin kernel.bin > OS.bin

qemu-system-i386 OS.bin

You didn't initialize your variable: 您没有初始化变量:

int offset = 0;

Notice that the first use of offset was this: 请注意,第一次使用offset是这样的:

offset += 2*col;

which means that either you should have initialized offset to 0, or you should change that line to this: 这意味着您应该将初始化的offset为0,或者您应该将该行更改为:

offset = 2*col;

After a lot of research and messing around with gcc and ld for a few hours, I've solved my problem. 经过大量的研究和使用gccld几个小时后,我已经解决了我的问题。

I build kernel.c using the following commands: 我使用以下命令构建kernel.c

gcc -Wall -pedantic-errors -nostdlib kernel.c -o kernel.exe

ld -nostdlib -Ttext 0x1000 -o kernel.tmp kernel_entry.o kernel.exe
objcopy -O binary -j .text  kernel.tmp kernel.bin

I am not entirely sure why this works and my previous attempts didn't, but for now everything is working as it should. 我不完全确定为什么这样做,以及我以前的尝试没有,但是现在一切都正常。 Thanks for all the help I received. 感谢我收到的所有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM