【问题标题】:How do I read a key and store the value of that key into a variable in ASM?如何读取密钥并将该密钥的值存储到 ASM 中的变量中?
【发布时间】:2014-11-26 19:55:21
【问题描述】:

我正在制作一种类似 C# 的语言,可以直接编译为 x86 NASM 代码。我写了一个类似于 Console.ReadKey() 的函数。它应该等待一个键,然后将它的值存储在一个字符串中。下面是 ASM 函数的代码:

os_input_char:
    pusha
    mov di, ax              ; DI is where we'll store input (buffer)
    call os_wait_for_key    ; Moves the next char into al
    stosb                   ; Store character in designated buffer
    mov al, 0               ; 0-terminate it
    stosb                   ; ^^
    popa
    ret

然后我在我的代码中调用该函数。这里是:

type [kernel]

:start
string key = "a";
graphicsmode;

nasm{
    mov bl, 0001b           ;blue
    call os_clear_graphics  ;clear
}nasm

movecursor(#0#, #0#);
print("                                        ");
print("        Welcome to LightningOS!         ");
print("                                        ");

:main1
    readkey -> key;
    //I put the key code into the variable "key"

    println("you pressed: "+%key%);
    //Now print the variable key

    goto(main1);
    //now loop back to main

它只是不断打印'a'。现在我确定我正确调用了该函数。我的编译器是用 C# 制作的,所以并不难理解。 'append' 指令只是将 ASM 代码添加到文件中。调用方法:

            else if (line.StartsWith("readkey -> ") && line.EndsWith(";"))
            {
                append("mov ax, " + line.Substring(11, line.Substring(11).Length - 1));
                append("call os_input_char");
            }

那么...我如何真正将那个键放入字符串中然后打印那个字符串?

【问题讨论】:

标签: assembly nasm cosmos


【解决方案1】:

一些想法:
您正在使用stosb,但您没有设置 ES。你确定它已经OK了吗?
line.Substring 使用从 0 开始还是从 1 开始的索引?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多