简体   繁体   English

在x86中使用BIOS中断

[英]Using BIOS interrupts in x86

I am trying to implement string operations on QEmu in real-mode. 我正在尝试以实模式在QEmu上实现字符串操作。 Here is the read and print function that I made: 这是我所做的读取和打印功能:

int readString(char* line)
{
 int i = 0;
 char in = 0x0;
 while (in != 0xd)
   {
    in = interrupt(0x16, 0x0, 0x0, 0x0, 0x0);
    *(line + i) = in;
    interrupt(0x10,0xe*0x100+in,0x0,0x0,0x0);
    i++;
   }
 *(line + i) = 0x0;
 return i;
}

int printString(char* string)
{
 int i = 0;
 while (*(string + i) != '\0')
   {
    char al = *(string + i);
    char ah = 0xe;
    int ax = ah * 256 + al;
    interrupt(0x10,ax,0,0,0);
    i++;
   }
 return i;
}

These functions are called in the following main program: 这些功能在以下主程序中调用:

void main()
{
 char* line;
 printString("Reading from input:\n\r");
 readString(line);
 printString("Line read is:\n\r");
 printString(line);
}

The readString function takes the input from the keyboard, prints it to the screen (on QEmu) as we type the input string and stores the result in the argument passed (which is pointer to char). readString函数从键盘获取输入,在我们输入输入字符串时将其打印到屏幕上(在QEmu上),并将结果存储在传递的参数中(这是指向char的指针)。 But the cursor doesn't seem to move after the readString function. 但是光标在readString函数之后似乎并没有移动。 And the call to printString function afterwards (in the main function) causes the string to be overwritten. 之后调用printString函数(在主函数中)将导致字符串被覆盖。 For example, if I write "Hello ", then I expect output to be: 例如,如果我写“ Hello”,那么我期望输出为:

Reading from input:
Hello Line read is:
Hello _

Here "_" is the cursor. 这里的“ _”是光标。 But instead the actual output coming is: 但是实际输出的是:

Reading from input:
Line read is:
Hello

The cursor is under the "H" of the Hello above (in actual output) and the initial "Hello" in the second line of the expected output is overwritten. 光标在上方Hello的“ H”下(在实际输出中),并且预期输出第二行中的初始“ Hello”将被覆盖。 Why is the cursor not moving as I print the string? 为什么在打印字符串时光标不移动?

When the user presses enter, you will get the CR character (code 13, \\r ). 当用户按下Enter键时,您将获得CR字符(代码13, \\r )。 However the bios output function interprets this as strictly a carriage return, that is it moves the cursor back to the start of the line. 但是,BIOS输出函数将其严格解释为回车符,即它将光标移回行的开头。 You need to add the LF (code 10, \\n ) yourself. 您需要自己添加LF (代码10, \\n )。 For example: 例如:

int readString(char* line)
{
 int i = 0;
 char in = 0x0;
 while (in != 0xd)
   {
    in = interrupt(0x16, 0x0, 0x0, 0x0, 0x0);
    *(line + i) = in;
    interrupt(0x10,0xe*0x100+in,0x0,0x0,0x0);
    /* add LF to CR */
    if (in == 13) interrupt(0x10,0xe*0x100+10,0x0,0x0,0x0);
    i++;
   }
 *(line + i) = 0x0;
 return i;
}

int printString(char* string)
{
 int i = 0;
 while (*(string + i) != '\0')
   {
    char al = *(string + i);
    char ah = 0xe;
    int ax = ah * 256 + al;
    interrupt(0x10,ax,0,0,0);
    /* add LF to CR */
    if (al == 13) interrupt(0x10,0xe*0x100+10,0x0,0x0,0x0);
    i++;
   }
 return i;
}

qemu屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM