Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

c - String literals turning into nullchars on passing to a function

I am in the process of making a simple kernel, and I have run into an issue with passing a string literal to a function as a char*.

Printing any char from the string that is passed into the function results in a nullchar, I tested this my printing the first char + 48, which printed a 0. I am not sure weather this may be due to the command i am using to compile, I am compiling with the following GCC flags -fno-stack-protector -mgeneral-regs-only -mno-red-zone -ffreestanding -fno-pie -m32 -nostdinc -nostdlib

I have checked that the string literal is in the assembly by compiling to assembly.

Main file:

#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
break", 0, 20);
    kprint("There is a line
break");
    kprint_at("What happens when we run out of space?", 45, 24);
    */
}

in the screen.c file

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);
    }
}

I have verified the correct operation of all the functions called from the kprint_at function.

question from:https://stackoverflow.com/questions/65945108/string-literals-turning-into-nullchars-on-passing-to-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...