为什么我不能在C中创建长度超过61个字符的char数组?

时间:2017-10-15 20:54:49

标签: c arrays x86 nasm osdev

我正在关注这个tutorial来创建一个简单的32位操作系统。我已经到了第4节,我正在写帧缓冲区。基本上我正在尝试创建自己的println函数。这是我的函数的代码:

/** fb_write_cell:
 * Writes a character with the given foreground and background to position i
 * in the framebuffer.
 *
 * @param i The location in the framebuffer
 * @param c The character
 * @param fg The foreground color
 * @param bg The background color
 */
static void fb_write_cell(unsigned int i, char c, unsigned char fg, unsigned bg) {
  fb[i] = c;
  fb[i + 1] = ((fg & 0x0F) << 4) | (bg & 0x0F);
}

/** fb_print:
 * Print a string of text to the framebuffer
 * @param *buf The character array to print
 */
int fb_print(char *str, unsigned int length) {
  unsigned int i = 0, x = 0;

  // print the message to the framebuffer
  for(; i < (2 * length); i+=2) {
    fb_write_cell(i, str[x], FB_BLACK, FB_GREEN);
    x++;
  }
  return 0;
}

/** fb_println:
 * Print a string of text to the framebuffer and move to the next line
 * @param *buf The character array to print
 */
int fb_println(char *str, unsigned int length) {
   fb_print(str, length);

  return 0;
}

我称之为:

char array[] = "Hello world!";
fb_println(array, sizeof(array));

但是,如果我使数组长度超过61个字符,我就会停止向屏幕输出任何内容。实际上,数组创建后的任何代码都不会被执行。我想也许它与我裸机系统中的有限RAM(可能只有64字节?)有关,但我不确定。

loader.s中我的多重引导标头和启动代码调用我的 C 入口点main

global loader                   ; the entry symbol for ELF

MAGIC_NUMBER equ 0x1BADB002     ; define the magic number constant
FLAGS        equ 0x0            ; multiboot flags
CHECKSUM     equ -MAGIC_NUMBER  ; calculate the checksum
                                ; (magic number + checksum + flags should equal 0)

KERNEL_STACK_SIZE equ 4096      ; size of stack in bytes

extern sum_of_three             ; the function is defined elsewhere
extern main

section .text:                  ; start of the text (code) section
align 4                         ; the code must be 4 byte aligned
    dd MAGIC_NUMBER             ; write the magic number to the machine code,
    dd FLAGS                    ; the flags,
    dd CHECKSUM                 ; and the checksum

section .bss:
align 4                     ; align at 4 bytes
kernel_stack:               ; label points to beginning of memory
    resb KERNEL_STACK_SIZE  ; reserve stack for the kernel

loader:                                           ; the loader label (defined as entry point in linker script)
    mov eax, 0xCAFEBABE                           ; place the number 0xCAFEBABE in the register eax
    mov esp, kernel_stack + KERNEL_STACK_SIZE     ; point esp to the start of the
                                                  ; stack (end of memory area)
    ;Example of how to call a function and send args
    ;push dword 3                                  ; arg3
    ;push dword 2                                  ; arg2
    ;push dword 1                                  ; arg1
    ;call sum_of_three                             ; call the function, the result will be in EAX


.loop:
    call main
    jmp .loop                   ; loop forever

我的链接描述文件link.ld是:

ENTRY(loader)                /* the name of the entry label */

SECTIONS {
    . = 0x00100000;          /* the code should be loaded at 1 MB */

    .text ALIGN (0x1000) :   /* align at 4 KB */
    {
        *(.text)             /* all text sections from all files */
    }

    .rodata ALIGN (0x1000) : /* align at 4 KB */
    {
        *(.rodata*)          /* all read-only data sections from all files */
    }

    .data ALIGN (0x1000) :   /* align at 4 KB */
    {
        *(.data)             /* all data sections from all files */
    }

    .bss ALIGN (0x1000) :    /* align at 4 KB */
    {
        *(COMMON)            /* all COMMON sections from all files */
        *(.bss)              /* all bss sections from all files */
    }
}

我的整个源代码可以找到here

1 个答案:

答案 0 :(得分:3)

此处的问题与您的 C 代码无关,但文件loader.s中存在问题。如果节末端的冒号成为节名称的一部分,则会出现问题,并且您无法将代码放入可执行文件.text部分。

这些行在部分名称上有额外的冒号:

section .text:                  ; start of the text (code) section

section .bss:

他们应该是:

section .text                   ; start of the text (code) section

section .bss

这些额外冒号会导致事物以意外方式放置,并且可能受特定部分中数据量的影响(例如 C 代码中的字符串)。这可能导致您的kernel.elf并不总是显示为多重引导兼容的引导加载程序。

同样代码需要放在.text部分。您将堆栈及其后面的代码放在.bss部分中,这是不正确的。要修复此位置,请在代码开头上方使用section指令,如下所示:

section .text
loader:                        ; the loader label (defined as entry point in linker script)

您还将内核置于无限循环中:

.loop:
    call main
    jmp .loop                   ; loop forever

你可能打算在你的内核完成后进行无限循环:

    call main
.loop:
    jmp .loop                   ; loop forever

LittleOS Book Tutorial中的错误

我注意到,您在Compiling the Operating System部分中使用的教程的作者似乎是错误的,因为此代码中出现错误:

section .text:                  ; start of the text (code) section
align 4                         ; the code must be 4 byte aligned

有人可能希望向作者提出此问题。