How do I read a key and store the value of that key into a variable in ASM?

701 views Asked by At

I am working on making a C#-like language that compiles directly to x86 NASM code. I have written a function similar to Console.ReadKey(). It is supposed to wait for a key and then store the value of it in a string. Here is the code for the ASM function:

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

I then call that function in my code. Here it is:

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

and it just keeps printing 'a'. Now I am sure that I am calling the function correctly. My compiler is made in C#, so its not that hard to follow. The 'append' instruction just adds on ASM code to the file. Method for calling:

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

So... How do I actually get that key into the string and then print that string?

1

There are 1 answers

0
Sep Roland On

Some ideas :
You're using stosb but you don't setup ES. Are you sure it's already OK?
Does line.Substring use 0-based or 1-based indexing?