2

嗨,我正在尝试在我的操作系统中使用 vesa 模式,我正在使用本教程: 在保护模式下绘图

我得到了切换的分辨率,但我不知道如何绘制像素。

这是我的代码:

内核.asm

bits    32
section .text
align 4

dd 0x1BADB002
dd 0x04
dd -(0x1BADB002 + 0x04)

dd 0 ; skip some flags
dd 0
dd 0
dd 0
dd 0

dd 0 ; sets it to graphical mode
dd 800 ; sets the width
dd 600 ; sets the height
dd 32 ; sets the bits per pixel

push ebx
global start
extern kmain
start:
    cli
    call kmain
    hlt

内核.c

#include "include/types.h"
kmain(){

}

先感谢您

4

1 回答 1

0

首先,您是否使用 grub 和 multiboot 来加载内核?如果是这样,那么 VESA 信息(连同视频内存地址(也称为线性帧缓冲区地址)将被复制到您可以在“kernel.c”文件中访问的多引导头结构中。帧缓冲区地址被存储在索引 22 的结构内。如果您想要关于 multiboot 及其结构的完整规范,可以在此处查看它们:https ://www.gnu.org/software/grub/manual/multiboot/multiboot.html

否则,您可以在“kernel.c”文件中执行此操作:

void kmain(unsigned int* MultiBootHeaderStruct)
{
    //color code macros:
    #define red   0xFF0000
    #define green 0x00FF00
    #define blue  0x0000FF

    /*store the framebuffer address inside a new pointer
      the framebuffer is located at MultiBootHeaderStruct[22]*/
    unsigned int* framebuffer = (unsigned int*)MultiBootHeaderStruct[22];

    /*now to place a pixel at a specific screen position(screen index starts at 
      the top left of the screen which is at index 0), write a color value
      to the framebuffer pointer with a specified index*/
    
    //Sample Code:
    framebuffer[0] = red; //writes a red pixel(0xFF0000) at screen index 0
    framebuffer[1] = green; //writes a green pixel(0x00FF00) at screen index 1
    framebuffer[2] = blue; //writes a blue pixel(0x0000FF) at screen index 2
    /*this code should plot 3 pixels at the very top left of the screen
      one red, one green and one blue. If you want a specific color you can
      use the link I will provide at the bottom.*/

    
    //Sample code to fil the entire screen blue:
    int totalPixels = 480000; //800x600 = 480000 pixels total
    
    //loop through each pixel and write a blue color value to it
    for (int i = 0; i < totalPixels; i++)
    {
        framebuffer[i] = blue; //0x0000FF is the blue color value
    }
}

一个有用的颜色代码生成器:https ://html-color-codes.info/ 注意。不要使用放置在您生成的颜色前面的“#”标签,只使用它后面的值并将颜色值放在代码中的“0x”之后,这意味着 #FF0000 应该放在代码中: 0xFF0000

我希望这会有所帮助。我也在 VESA 上苦苦挣扎,我想帮助那些也在苦苦挣扎的人。如果你想直接联系我寻求帮助,你可以在 discord 上加我,我可以尽我所能提供帮助。我的用户名和标签是:Yeon#7984

于 2020-11-10T03:12:27.293 回答