Is there a method for getting a keyboard input thru ASM with protected mode in C++?

I’ve been working on a kernel that my system needs. I’ve implement some functions that acts as a wrapper for I/O.
But somehow, the get_char() only returns 0.

Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <cstdint>
#include <cstdio>
extern "C" void cout(char c, int position) {
// 0xb8000 is the starting address for video memory in text mode
// Each character on the screen is represented by 2 bytes:
// - The first byte is the ASCII character code.
// - The second byte is the attribute byte (color).
// position: the index on the screen where the character will be displayed.
// 80 characters per line, so a position of 0 is the top-left corner.
char* video_memory = (char*)0xb8000;
video_memory[position * 2] = c; // Set the character
video_memory[position * 2 + 1] = 0x0F; // White text on black background
}
extern "C" void cout_string(const char* str, int start_pos) {
int pos = start_pos;
while (*str) {
cout(*str++, pos++);
}
}
extern "C" int strlen(const char* str) {
int length = 0;
while (str[length] != '') {
length++;
}
return length;
}
uint8_t inb(uint16_t port) {
uint8_t result;
__asm__ __volatile__ ("inb %1, %0" : "=a"(result) : "Nd"(port));
return result;
}
void wait_for_keypress() {
// Poll the keyboard status port (0x64) until the output buffer is full
while (!(inb(0x60) & 0x01)) {
// Busy-wait until data is available in the output buffer
}
}
// Simple scancode to ASCII conversion (only handling basic characters for example purposes)
char scancode_to_ascii(uint8_t scancode) {
static char scancode_map[256] = { //testing
46, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
};
if (scancode > 255) {
return 0; // Unknown scancode
}
return scancode_map[scancode];
}
char get_char() {
uint8_t scancode;
// Wait for a key press by polling the keyboard status port
wait_for_keypress();
// Now read the scancode from the keyboard data port (0x60)
scancode = inb(0x60);
// Convert the scancode to ASCII, ignoring key releases
if (scancode & 0x80) {
return 0; // Ignore break codes (key releases)
}
return scancode_to_ascii(scancode);
}
void cin(const char* prompt, int length, char* buffer) {
int pos = 0;
// Display the prompt
cout_string(prompt, pos);
pos += strlen(prompt);
// Read input from keyboard
int index = 0;
char c;
while (index < length - 1) { // Leave space for null terminator
c = get_char();
// Check for Enter key (ASCII 13)
if (c == 'n') {
break;
}
// Check for Backspace (ASCII 8)
else if (c == 'b') {
if (index > 0) {
pos--;
// Clear the character on screen
cout(' ', pos);
index--;
continue;
}
}
// Prints the char to the screen
else {
if (c > 0) {
cout(c, pos);
pos++;
buffer[index++] = c;
}
if (c == 0) {
cout('A', pos);
pos++;
buffer[index++] = c;
}
}
}
buffer[index] = ''; // Null-terminate the string
cout('s', 0);
}
extern "C" void main() {
// Example usage:
char input[20]; // Buffer for up to 19 characters + null terminator
// Prompt the user for input
cin("Enter something: ", 20, input);
// Display the input back on the screen
cout_string("You entered: ", 0);
cout_string(input, 13);
return;
}
</code>
<code>#include <cstdint> #include <cstdio> extern "C" void cout(char c, int position) { // 0xb8000 is the starting address for video memory in text mode // Each character on the screen is represented by 2 bytes: // - The first byte is the ASCII character code. // - The second byte is the attribute byte (color). // position: the index on the screen where the character will be displayed. // 80 characters per line, so a position of 0 is the top-left corner. char* video_memory = (char*)0xb8000; video_memory[position * 2] = c; // Set the character video_memory[position * 2 + 1] = 0x0F; // White text on black background } extern "C" void cout_string(const char* str, int start_pos) { int pos = start_pos; while (*str) { cout(*str++, pos++); } } extern "C" int strlen(const char* str) { int length = 0; while (str[length] != '') { length++; } return length; } uint8_t inb(uint16_t port) { uint8_t result; __asm__ __volatile__ ("inb %1, %0" : "=a"(result) : "Nd"(port)); return result; } void wait_for_keypress() { // Poll the keyboard status port (0x64) until the output buffer is full while (!(inb(0x60) & 0x01)) { // Busy-wait until data is available in the output buffer } } // Simple scancode to ASCII conversion (only handling basic characters for example purposes) char scancode_to_ascii(uint8_t scancode) { static char scancode_map[256] = { //testing 46, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, }; if (scancode > 255) { return 0; // Unknown scancode } return scancode_map[scancode]; } char get_char() { uint8_t scancode; // Wait for a key press by polling the keyboard status port wait_for_keypress(); // Now read the scancode from the keyboard data port (0x60) scancode = inb(0x60); // Convert the scancode to ASCII, ignoring key releases if (scancode & 0x80) { return 0; // Ignore break codes (key releases) } return scancode_to_ascii(scancode); } void cin(const char* prompt, int length, char* buffer) { int pos = 0; // Display the prompt cout_string(prompt, pos); pos += strlen(prompt); // Read input from keyboard int index = 0; char c; while (index < length - 1) { // Leave space for null terminator c = get_char(); // Check for Enter key (ASCII 13) if (c == 'n') { break; } // Check for Backspace (ASCII 8) else if (c == 'b') { if (index > 0) { pos--; // Clear the character on screen cout(' ', pos); index--; continue; } } // Prints the char to the screen else { if (c > 0) { cout(c, pos); pos++; buffer[index++] = c; } if (c == 0) { cout('A', pos); pos++; buffer[index++] = c; } } } buffer[index] = ''; // Null-terminate the string cout('s', 0); } extern "C" void main() { // Example usage: char input[20]; // Buffer for up to 19 characters + null terminator // Prompt the user for input cin("Enter something: ", 20, input); // Display the input back on the screen cout_string("You entered: ", 0); cout_string(input, 13); return; } </code>
#include <cstdint>
#include <cstdio>
extern "C" void cout(char c, int position) {
    // 0xb8000 is the starting address for video memory in text mode
    // Each character on the screen is represented by 2 bytes:
    //   - The first byte is the ASCII character code.
    //   - The second byte is the attribute byte (color).
    
    // position: the index on the screen where the character will be displayed.
    // 80 characters per line, so a position of 0 is the top-left corner.
    
    char* video_memory = (char*)0xb8000;
    video_memory[position * 2] = c;    // Set the character
    video_memory[position * 2 + 1] = 0x0F; // White text on black background
}
extern "C" void cout_string(const char* str, int start_pos) {
    int pos = start_pos;
    while (*str) {
        cout(*str++, pos++);
    }
}
extern "C" int strlen(const char* str) {
    int length = 0;
    while (str[length] != '') {
        length++;
    }
    return length;
}
uint8_t inb(uint16_t port) {
    uint8_t result;
    __asm__ __volatile__ ("inb %1, %0" : "=a"(result) : "Nd"(port));
    return result;
}

void wait_for_keypress() {
    // Poll the keyboard status port (0x64) until the output buffer is full
    while (!(inb(0x60) & 0x01)) {
        // Busy-wait until data is available in the output buffer
    }
}


// Simple scancode to ASCII conversion (only handling basic characters for example purposes)

char scancode_to_ascii(uint8_t scancode) {
    static char scancode_map[256] = { //testing
        46, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
        33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
        49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,

    };
    
    if (scancode > 255) {
        return 0;  // Unknown scancode
    }
    
    return scancode_map[scancode];
}
char get_char() {
    uint8_t scancode;

    // Wait for a key press by polling the keyboard status port
    wait_for_keypress();
    
    // Now read the scancode from the keyboard data port (0x60)
    scancode = inb(0x60);

    // Convert the scancode to ASCII, ignoring key releases
    if (scancode & 0x80) {
        return 0;  // Ignore break codes (key releases)
    }

    return scancode_to_ascii(scancode);
}
void cin(const char* prompt, int length, char* buffer) {
    int pos = 0;

    // Display the prompt
    cout_string(prompt, pos);
    pos += strlen(prompt);

    // Read input from keyboard
    int index = 0;
    char c;
    while (index < length - 1) { // Leave space for null terminator
        c = get_char();

        // Check for Enter key (ASCII 13)
        if (c == 'n') {
            break;
        }
        // Check for Backspace (ASCII 8)
        else if (c == 'b') {
            if (index > 0) {
                pos--;
                // Clear the character on screen
                cout(' ', pos);
                index--;
                continue;
            }
        }
        // Prints the char to the screen
        else {
            if (c > 0) {
                cout(c, pos);
                pos++;
                buffer[index++] = c;
            }
            if (c == 0) {
                cout('A', pos);
                pos++;
                buffer[index++] = c;
            }
        }
    }
    buffer[index] = ''; // Null-terminate the string
    cout('s', 0);
}

extern "C" void main() {
    // Example usage:
    char input[20]; // Buffer for up to 19 characters + null terminator
    // Prompt the user for input
    cin("Enter something: ", 20, input);
    // Display the input back on the screen
    cout_string("You entered: ", 0);
    cout_string(input, 13);
    return;
}

I tried renaming 0 on the scancode map to something readable, but it didn’t show up. I’ve also tried to detect whether the output is 0 and it is.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật