Assembly Language Fundamentals

Beginner
Version:
1.0

Reading Key Input

Introduction

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.

How Keyboard Input Works

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:

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.

ASCII Refresher: Why Add 48?

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:

If 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.

Step-by-Step: Reading and Echoing a Key Press

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

Walkthrough

  1. Wait for Input
    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.
  2. Output the Character
    RST 8 / DEFB 158 calls the ZOUTC routine, which prints the character stored in register A to the screen.
  3. Return to System
    RET cleanly ends the program and returns control to the operating system.

How It Works Internally

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.

This is the simplest form of input — perfect for menus, simple commands, or learning how programs respond to the user.

Exercises

Exercise 8.1 — Echo Characters

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.

Exercise 8.2 — Key Reaction

Create a program that:

Use conditional jumps to check the value in register A.

Exercise 8.3 — Numeric Keypad

Write a program that:

This will reinforce the concept of ASCII conversion and output.

Summary

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.

Previous Module
Next Module