首页 > 解决方案 > 如何使用 c/c++ 获取用户的输入(适用于操作系统)

问题描述

我正在用 C++ 编写操作系统来学习(包括 C)我学习了一些功能,例如打印到屏幕,但我无法学会接收用户的输入并打印到屏幕。我查看了很多资源,但找不到我想要的。我想要的是接收用户的输入并将其打印在屏幕上并等待某个键被按下以执行操作。我该怎么做?如果您能提供示例代码,我会很高兴。

加载器.s

.set MAGIC, 0x1badb002
.set FLAGS, (1<<0 | 1<<1)
.set CHECKSUM, -(MAGIC + FLAGS)

.section .multiboot
    .long MAGIC
    .long FLAGS
    .long CHECKSUM
    


.section .text
.extern kernelMain
.extern callConstructors
.global loader

loader:
    mov $kernel_stack, %esp
    call callConstructors
    push %eax
    push %ebx
    call kernelMain

_stop:
    cli
    hlt
    jmp _stop


.section .bss 
.space 2*1024*1024;
kernel_stack:

内核.cpp

#include "includes/types.h"
#include "includes/functions.h"
#include "includes/colors.h"
#include "includes/keyboard.h"
using namespace std;

typedef void (*constructor)();
extern "C" constructor* start_ctors;
extern "C" constructor* end_ctors;
extern "C" void callConstructors()
{
    for(constructor* i=start_ctors;i!=end_ctors;i++){
        (*i)();
    }
}

extern "C" void kernelMain(void* multiboot_structure, uint32_t magicnumber)
{ 
    print(COLOR_RED, "Hello!");
    while(1);
}

函数.h

#include "keyboard.h"
#include "colors.h"


static unsigned short* VideoMemory=(unsigned short*)0xb8000;

int pos_print = 0;

void print_title( unsigned char colour, const char *string) {
    volatile unsigned char *vid = (unsigned char*) VideoMemory;
    while(*string != 0)
    {
        *(vid) = *string;
        *(vid+1) = colour;
        ++string;
        vid+=2;
    }
}


void setChar(char character, short col, short row, unsigned char attr) {
    volatile unsigned char* vid_mem = (unsigned char *) VideoMemory;
    int offset = (row*80 + col)*2;
    vid_mem += offset;
    if(!attr) {
        attr = 0x0f;
    }
    *(unsigned short int *)vid_mem = (attr<<8)+character;
}

void print_line( unsigned char colour, const char *string, int pos ) {
    volatile unsigned char *vid = (unsigned char*) VideoMemory;
    vid+=pos*160;
    while(*string != 0)
    {
        *vid = *string;
        *(vid+1) = colour;
        ++string;
        vid+=2;
    }

}

void print( unsigned char colour, char string ) {
    volatile unsigned char *vid = (unsigned char*) VideoMemory;
    pos_print+=1;
    vid+=pos_print*160;
    while(string != 0)
    {
        *vid = string;
        *(vid+1) = colour;
        ++string;
        vid+=2;
    }

}

标签: c++coperating-systemkernel

解决方案


推荐阅读