Saturday, November 22, 2014

Assembly Tutorial 4: Keyboard IO

When writing a program, it is generally necessary to be able to accept user input.  Fortunately in 8086 real mode this is quite simple.  We can use a BIOS interrupt to get a key from the user.

Getting A Key
Accepting a single key is very simple.  We just call interrupt 0x16 after setting ax to 0x01.  This will return a character in register AL, and the key id in register AH.

Getting a String
Once we are able to get a single key we can expand this into a loop that accepts any key press and stores it in a buffer until the enter key is pressed.  At that point we return the buffer in register BX.

This loop can be constructed simply by setting BX to the address of the buffer, after that we set a label as the start of our loop and call to get a key press.

When a key is returned we check if that key was the enter key.  If it is we jump out of the loop.

Otherwise we push the character in register AL to the address in BX and increment the address and jump to the top of the loop.

After the loop we add a 0 to the end of the buffer to indicate that it is the end of the string and push the address of the buffer back to BX and return.

Please check the repository for this tutorial for an example of this code.
https://github.com/musicman89/Assembly-Tutorial

No comments:

Post a Comment