代码之家  ›  专栏  ›  技术社区  ›  Yahya

解释以下程序是如何工作的?

  •  -4
  • Yahya  · 技术社区  · 7 年前

    这是C语言的程序:(我删除了一些简单初始化VGA\U颜色变量的代码)

    static inline uint8_t vga_entry_color(enum vga_color fg, 
    enum vga_color bg) {
        return fg | bg << 4;
    }
    
    static inline uint16_t vga_entry(unsigned char uc, uint8_t color) {
        return (uint16_t) uc | (uint16_t) color << 8;
    }
    
    size_t strlen(const char* str) {
    size_t len = 0;
    while (str[len])
        len++;
    return len;
    }
    
    static const size_t VGA_WIDTH = 80;
    static const size_t VGA_HEIGHT = 25;
    
    size_t terminal_row;
    size_t terminal_column;
    uint8_t terminal_color;
    uint16_t* terminal_buffer;
    
    void terminal_initialize(void) {
        terminal_row = 0;
        terminal_column = 0;
        terminal_color = 
        vga_entry_color(VGA_COLOR_LIGHT_GREY, 
        VGA_COLOR_BLACK);
        terminal_buffer = (uint16_t*) 0xB8000;
        for (size_t y = 0; y < VGA_HEIGHT; y++) {
            for (size_t x = 0; x < VGA_WIDTH; x++) {
                const size_t index = y * VGA_WIDTH + x;
                terminal_buffer[index] = vga_entry(' ', terminal_color);
            }
        }
    }
    
    void terminal_setcolor(uint8_t color) {
        terminal_color = color;
    }
    
    void terminal_putentryat(char c, uint8_t color, size_t x, 
    size_t y) {
        const size_t index = y * VGA_WIDTH + x;
        terminal_buffer[index] = vga_entry(c, color);
    }
    
    void terminal_putchar(char c) {
        terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
        if (++terminal_column == VGA_WIDTH) {
            terminal_column = 0;
            if (++terminal_row == VGA_HEIGHT)
                terminal_row = 0;
        }
    }
    
    void terminal_write(const char* data, size_t size) {
        for (size_t i = 0; i < size; i++)
            terminal_putchar(data[i]);
    }
    
    void terminal_writestring(const char* data) {
        terminal_write(data, strlen(data));
    }
    
    #if defined(__cplusplus)
    extern "C" /* Use C linkage for kernel_main. */
    #endif
    void kernel_main(void) {
        /* Initialize terminal interface */
        terminal_initialize();
    
        /* Newline support is left as an exercise. */
        terminal_writestring("Hello, kernel World!\n");
    }
    

    它来自OSdev wiki,虽然我确实理解这个程序的某些部分,实际上是内核。c-我对函数“terminal\u initilize”、“terminal\u putentryat”、“terminal\u putchar”有问题。欢迎任何解释。

    1 回复  |  直到 7 年前
        1
  •  -1
  •   sg7 Krafty Coder    7 年前

    我希望这些解释能给你一些提示。通过分析代码函数,您应该能够找出其余的。

    VGA_WIDTH -屏幕宽度; VGA_HEIGHT -屏幕高度

    x -字符在屏幕上的水平位置

    y -字符在屏幕上的垂直位置

    由于VGA卡将所有字符保存在线性内存中,因此必须使用 x , y ,
    VGA_宽度 VGA_宽度 为其内部缓冲区计算索引,以将字符存储在适当的内存位置。

    void terminal_initialize -此函数用于初始化终端。它只会在整个屏幕上留下空白。

    void terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
    

    放置角色 c ,在屏幕上的位置( x , y ).

    void terminal_putchar(char c) -它放置了一个角色 c ,在屏幕上。

    此函数具有 terminal_column terminal_row 用于跟踪当前端子列和行的变量。

    推荐文章