Previously, you learned how to capture a single key press. That was an important first step, but most real programs need to read more than one character at a time. They need to accept full words, commands, or even sentences typed by the user.
In this lesson, you’ll learn how to read a complete line of text from the keyboard, store it in memory, and process it. This forms the foundation for building menus, command interfaces, text parsers, and text-driven games.
When the user types on the keyboard, each character is converted into an ASCII code by the system. We could read these one at a time, but that would quickly become complicated. Instead, we can use a built-in system routine that does all the work for us:
ZGETLINENTER.DE.0 byte to mark the end of the text.With this routine, your program doesn’t need to worry about individual key presses — it simply waits until the line is complete and then works with the result.
The program below shows the simplest possible example of reading a line of input and printing it back to the screen. Every part of it is essential, but nothing more.
ORG 256
start:
; --- Read input line into buffer ---
LD DE, input_buffer
RST 8
DEFB 157 ; Call ZGETLIN - read a line of text into buffer
; --- Print the line back ---
LD HL, input_buffer ; HL points to start of text
echo_loop:
LD A, (HL) ; Load next character
CP 0 ; End of string?
JP Z, done ; If null terminator, stop
RST 8
DEFB 158 ; Print the character
INC HL ; Move to next character
JP echo_loop ; Repeat
done:
JP start ; Go back and do it again
; --- Reserve input buffer ---
input_buffer:
DEFS 64 ; 64-byte buffer for user input
DEFS 64, which reserves 64 bytes.DE.ZGETLIN using RST 8 / DEFB 157.ENTER and stores their text in the buffer.HL is set to point at the start of the buffer.0, that marks the end of the string and the loop stops.start and waits for new input.0 byte at the end of the text tells your program where the string stops.ZGETLIN automatically displays characters as the user types them. You don’t need to write separate code for this.Modify the program so that it prints a message like You typed: before echoing the text.
Change the program so that instead of echoing the text, it counts the number of characters typed and prints the total.
Write a version of the program that prints HELLO only if the user types the word HELLO, and prints UNKNOWN COMMAND otherwise.
ZGETLIN (MCAL 157) reads a complete line of text and stores it in memory.0 byte marks the end.With this knowledge, your programs can now handle full words and phrases — a crucial capability for building command-driven systems or text-based adventures.