Kevin Wojoda
Kevin Wojoda

Reputation: 29

How can I print the pressed keyboard key in Assembly bootloader?

I have created a small boot-able OS in Assembly, with Flat Assembler. I call it PulsarOS. However, I want to create a typing program for it. Like I said, it's all in x86 Assembly. I want it simply to where the user can type, and the typed text is shown on the screen. Here's the code. It boots just fine in VirtualBox and on my physical PC:

mov ax, 9ch
mov ss, ax
mov sp, 4096d
mov ax, 7c0h
mov ds, ax
;Pulsar Micro-Kernel With Text Editor v1.0.1, running Pulsar OS v1.0.4
;_______BOOTED CODE PAST THIS POINT______
mov ah, 09h
mov cx, 80d
mov al, 20h
MOV SI, HelloString
CALL PrintString
mov bl, 80h
int 10h

mov ah, 09h
mov cx, 1000h
mov al, 20h
mov bl, 17h
int 10h

JMP $


PrintCharacter:

mov ah, 0x0E
mov bh, 0x00

INT 0x10
RET

PrintString:

next_character:
MOV AL, [SI]
INC SI
OR AL, AL
JZ exit_function
CALL PrintCharacter
JMP next_character
exit_function:
RET



;In the quotes is the text shown, no ASCII codes here! :)
HelloString db 'PulsarOS Basic Text Editor v1.0.4 ', 0



;When building with cmd prompt, type: copy /b ytut.bin ytut.img
;ytut is the name of the file saved.
;----------------------------------------
times 510-($-$$) db 0
dw 0xAA55

So, it relatively simple, but I want a just-as-simple code I can add into the booted code. I am also pretty new to Assembly, fresh out of my classes, so explain it like I'm five. Thank you!

Upvotes: 2

Views: 3635

Answers (1)

SeeSoftware
SeeSoftware

Reputation: 571

You can find keyboard services on the BIOS Interrupt 16h: INT 16H

WARNING: This method only works in "Real Mode".
Currently I'm not sure about "Protected Mode," but this will work for your bootloader or 16bit OS:

mov ah,0h  ;service 0h Read key press
int 16h    ;Puts the pressed key into al 

if you want to have it, wait for the key:
here is a function that waits for a keypress and then puts the keypress into al:

wait_for_keypress: ;al <= Pressed key | ah <= 0h
push bx ;push registers
push cx
push dx

wait_for_keypress_loop:
mov ah,0h
int 16h

cmp al,0h ;if you pressed a key exit
jg wait_for_keypress_end

jmp wait_for_keypress_loop ;if not loop infinitely until you press a key

wait_for_keypress_end:
pop dx ;restore registers
pop cx
pop bx
ret ;return  

Upvotes: 2

Related Questions