Search code examples
assemblyx86qemu16-bit

How to save keyboard input to a register in assembly?


So i am basically making a basic commandline os and i have made it so that it can take keyboard presses and a procedure to go to a new line when i press enter but i want it to store the command as a string before moving to a new line so that i can compare a command with the user input so that I can make it print a string or do something so how would we join all the chars and make a string here is my code till now

;bg
MOV AH, 06h   
XOR AL, AL    
XOR CX, CX    
MOV DX, 184FH  
MOV BH, 1Eh  
INT 10H
;cursor
mov dh, 1
mov dl, 30
mov bh, 0
mov ah, 2
int 10h
;text
mov ah, 0x0E
mov al, 'W'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'E'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'L'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'C'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'O'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'M'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'E'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, ' '
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'T'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'O'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, ' '
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'D'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'A'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'R'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'S'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'H'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'O'
mov bh, 0x00
int 10h
mov ah, 0x0E
mov al, 'S'
mov bh, 0x00
int 10h
;cursor
mov dh, 3
mov dl, 2
mov bh, 0
mov ah, 2
int 10h
;commandline
Key:
    MOV AH,0 
    INT 16H
    cmp ah, 28
    je Newline
    mov ah, 0eh
    int 10h
    jmp Key

Newline:

    add dh, 1
    mov dl, 2
    mov bh, 0
    mov ah, 2
    int 10h
    jmp Key
times 510-($-$$) db 0
dw 0xaa55

Screenshot: img of the program


Solution

  • As a first observation, you are writing all of this in a bootsector. That's probably fine for learning purposes, but then at least be conscious about a bootsector's 512 bytes size limitation. I mean, you should not output all of those characters separately but rather as a string, which is of course what your question is about, isn't it.

    First solve the output, the input storage problem is similar.

    Start your bootsector program with this code:

    ORG  7C00h
    xor  ax, ax
    mov  ds, ax
    mov  es, ax
    cld
    

    Near the bottom of the program you insert this text:

    msg  db 'WELCOME TO DARSHOS', 0
    times 510-($-$$) db 0
    dw 0xAA55
    

    And where you have that very long chain of single character outputs, you replace the whole chain by the following:

    ;text
      mov  si, msg     ; Load address of the message in SI 
      mov  bh, 0       ; DisplayPage
      lodsb
    More:
      mov  ah, 0Eh     ; BIOS.Teletype
      int  10h
      lodsb            ; Retrieve a character, auto-increments the address SI
      cmp  al, 0
      jnz  More
    

    If your assembler follows NASM style, mov si, msg loads the address of the message. But if your assembler follows MASM style, you will have to write mov si, offset msg instead.


    In order to save the inputted characters, you need to define a suitable buffer.
    The buf line below will allow for a textual command of at most 7 characters followed by the delimiting carriage return. You need the delimiter to later know how long the full command is. Insert the following line near the bottom of the program:

    buf  db '........'
    msg  db 'WELCOME TO DARSHOS', 0
    times 510-($-$$) db 0
    dw 0xAA55
    

    And this is how you fill it:

    ;commandline
    Key:
        mov  di, buf   ; Load address of the buffer in DI
    Next:
        mov  ah, 00h   ; BIOS.GetKeyboardKey
        int  16h       ; -> AX
        stosb          ; Store in buffer, auto-increments the address DI
        mov  bh, 0     ; DisplayPage
        mov  ah, 0Eh   ; BIOS.Teletype
        int  10h
        cmp  al, 13
        jne  Next      ; Not yet carriage return
        mov  al, 10    ; Let follow a linefeed to complete the 'newline' sequence
        int  10h
        ...
    

    And at the ... you can start processing the string of characters that form a command like CLS, TIME, or anything that suits you.