[BootBoot] Part 3: Wait for keypress

This 3rd post on BootBoot my x86 Assembly helloworld will be shorter than Part 1 and Part 2. We are going to be waiting for a key to be pressed to switch from the cyan screen of Part 1 to the purple screen of Part 2.

Code additions:

x86 assembly has two special instructions to read from and write to I/O devices. This is because some processors might use different address spaces for the memory and the devices (ARM for example uses one address space for all).

We are going to implement .key_wait using these instructions to wait until a key is pressed before we load the 2nd sector we’ve seen in Part 2.

Let’s see what’s going on in .key_wait:
According to this documentation on the PS/2 controller thee PS/2 Controller itself uses 2 IO ports (IO ports 0x60 and 0x64). Like many IO ports, reads and writes may access different internal registers.

To read the status register we must use the 0x64 port. The Status Register contains various flags that show the state of the PS/2 controller. The meanings for each bit are demonstrated in section “Status Register” of [7].

We first set the 64h instruction in al to check the keyboard status.

Then we use and al, 1 to check if the keyboard controller output buffer is full. If it is, and between the 1st bit of al and 1 will be 1. So can then use jz to loop when the result is zero and the buffer is not full.

When we exit the loop we call in al, 60h to read the buffer. In this program we don’t care about the buffer content (any key pressed is fine) but we still need to read the key event to change the buffer flag and to prevent it from eternally showing pending data.


Running the program with make run (explained in Part 1) and pressing a random key (space in the gif below) changes the color:



the Makefile and .gdbinit are the same we’ve used in previous posts.


Previous posts on BootBoot:
[BootBoot] Part 1: Boot from a floppy drive and clear the screen
[BootBoot] Part 2: Boot from a floppy drive, load the 2nd sector, jump to it

[1]: Ralf Brown’s Interrupt List
[2]: Mode 13h in Wikipedia
[3]: X86 memory segmentation
[4]: NASM – The Netwide Assembler
[5]: The Netwide Disassembler, NDISASM
[6]: Int 13/AH=02h: Read disk sectors into memory
[7]: “8042” PS/2 Controller

Leave a Reply

Your email address will not be published. Required fields are marked *