简体   繁体   中英

Arguments not passed to function in c Kernel

I'm currently working on a home-made OS project. I've succesfully written a bootloader which switches the computer into protected mode and loads the kernel, which is written in c. My kernel loads fine, but I have a problem calling functions outside of 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. However, if I modify the variables inside of the function it works fine.

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

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 += 2*col;

which means that either you should have initialized offset to 0, or you should change that line to this:

offset = 2*col;

After a lot of research and messing around with gcc and ld for a few hours, I've solved my problem.

I build kernel.c using the following commands:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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