So far, your programs have been one-way — they display output, perform calculations, and follow instructions, but they don’t yet respond to anything the user does. In this lesson, we’ll change that by introducing keyboard input.
Learning how to read keys from the keyboard is a vital step towards creating interactive programs. Whether it’s pressing a key to continue, choosing a menu option, or controlling a character in a game, user input is at the heart of interactive software.
In this module, you’ll learn how to capture a single key press from the Tatung Einstein keyboard, understand how input is handled internally, and use that value in your program.
The Tatung Einstein’s operating system provides built-in routines for handling the keyboard. These routines are part of the MCAL (Machine Call Abstraction Layer) — a set of standard system functions that you can access from your Z80 assembly programs using the RST 8 instruction followed by a specific function code.
For reading a single key press, the most useful routine is:
KEYINA.When your program calls this routine, it pauses execution until a key is pressed. Once a key is detected, the system places its ASCII code into register A, ready for you to use or display.
When you press a key, the value returned is its ASCII code — a numerical representation of that character. For digits '0' to '9', the codes range from 48 to 57.
For example:
'0' → 48'1' → 49'A' → 65If you press a key like 3, the CPU will receive the number 51. To display the character rather than the raw number, we need to convert the numeric value back into its ASCII form. This is easily done by adding 48 before printing.
The following program waits for a single key press and then displays it on the screen.
ORG 256
RST 8
DEFB 156 ; Call MCAL KEYIN - wait for key press, ASCII in A
RST 8
DEFB 158 ; Call ZOUTC - print the character in A
RET ; Return to system
RST 8 / DEFB 9C calls the KEYIN MCAL routine. The program halts here until the user presses a key. Once pressed, the ASCII code for that key is stored in register A.RST 8 / DEFB 158 calls the ZOUTC routine, which prints the character stored in register A to the screen.RET cleanly ends the program and returns control to the operating system.When you press a key on the Tatung Einstein, the keyboard controller sends a hardware signal to the system firmware, which translates it into an ASCII code. The KEYIN MCAL routine waits for this process to complete and delivers the result directly to your program.
A.This is the simplest form of input — perfect for menus, simple commands, or learning how programs respond to the user.
Write a program that continuously waits for a key press and prints the corresponding character until the ESC key is pressed. Hint: The ASCII code for ESC is 27.
Create a program that:
YNUse conditional jumps to check the value in register A.
Write a program that:
0–9)This will reinforce the concept of ASCII conversion and output.
MCAL 9C (KEYIN) waits for a key press and returns the ASCII code in register A.ZOUTC (DEFB 158) displays the character on screen.With this knowledge, you now have the tools to create your first truly interactive programs. In the next lesson, we’ll build on this by reading whole lines of input and processing them as strings — a key step towards building command interfaces and text-driven games.