VGA Text Mode

This commit is contained in:
2021-12-09 10:34:40 +08:00
parent a5c98f5732
commit aab190baea
6 changed files with 212 additions and 11 deletions

View File

@@ -3,6 +3,8 @@
use core::panic::PanicInfo;
mod vga_buffer;
// 定义一个 byte string 类型的静态变量
static HELLO: &[u8] = b"Hello World!";
@@ -11,16 +13,7 @@ static HELLO: &[u8] = b"Hello World!";
// 函数名为 _start 是因为大多数系统默认用这个名字作为入口点名称
// -> ! 代表这是一个发散函数,不允许有任何返回值,因为它不会被任何函数调用,而是将直接被 bootloader 调用
pub extern "C" fn _start() -> ! {
// VGA Buffer 从 0xb8000 开始
let vga_buffer = 0xb8000 as *mut u8;
// 迭代 HELLO, 通过裸指针 vga_buffer 来定位待写入的缓冲区地址
for (i, &byte) in HELLO.iter().enumerate() {
unsafe {
*vga_buffer.offset(i as isize * 2) = byte; // ASCII 码字节
*vga_buffer.offset(i as isize * 2 + 1) = 0xb; // 颜色字节, 0xb 代表青色
}
}
println!("Hello World{}", "!");
loop {}
}
@@ -28,6 +21,7 @@ pub extern "C" fn _start() -> ! {
#[panic_handler]
// panic! 时将会进行栈展开,执行各种 drop 函数来回收垃圾
// 禁用标准库之后,这些东西就都没有了,我们需要把它重写一遍
fn panic(_info: &PanicInfo) -> ! {
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
loop {}
}