首页 > 解决方案 > 字符串文字在传递给函数时变成空字符

问题描述

我正在制作一个简单的内核,并且遇到了将字符串文字作为char*.

从传递给函数的字符串中打印任何字符都会导致 nullchar,我对此进行了测试,我打印了第一个 char + 48,它打印了一个 0。我不确定这可能是由于我用来编译的命令,我正在使用以下 GCC 标志进行编译-fno-stack-protector -mgeneral-regs-only -mno-red-zone -ffreestanding -fno-pie -m32 -nostdinc -nostdlib

我通过编译为程序集检查了字符串文字是否在程序集中。

主文件:

#include "../drivers/ports.h"
#include "../drivers/screen.h"

void main()
{
    clear_screen();

    kprint_at("hhgfhehthdtydhgf", 1, 6);
    /*
    kprint_at("This text spans multiple lines", 75, 10);
    kprint_at("There is a line\nbreak", 0, 20);
    kprint("There is a line\nbreak");
    kprint_at("What happens when we run out of space?", 45, 24);
    */
}

在 screen.c 文件中

void kprint_at(char *message, int col, int row)
{
    print_char(message[0] + 48, col, row, WHITE_ON_BLACK); // prints a 0 to the screen, ascii 48

    int offset;
    if (col >= 0 && row >= 0)
    {
        offset = get_offset(col, row);
    }
    else
    {
        offset = get_cursor_offset();
        row = get_offset_row(offset);
        col = get_offset_col(offset);
    }

    int i = 0;
    while (message[i] != 0)
    {
        offset = print_char(message[i++], col, row, WHITE_ON_BLACK);
        row = get_offset_row(offset);
        col = get_offset_col(offset);
    }
}

我已经验证了从函数调用的所有函数的正确操作kprint_at

标签: cstringoperating-systemliteralsbare-metal

解决方案


推荐阅读