Main - About - Tutorials - Articles - Files - Opcodes - Links  

Untitled Document

Simple Application: Keyboard Handling, Text Printing

Author: InternetNightmare


 One day on IRC I was asked to make little program that read keypress from 1 to 9 and outputs the same number of text lines. It was for the university! So I decided to make tutorial for that. And here it goes! :)

 Interrupt 16h is used for keyboard handling, function 0 (AH=0) waits for keypress and returns it’s scancode in AH and ASCII value in AL. In this tutorial I’ll show you the way to get numbers from ASCII values.

 Here’s the code:


[global start]

org 100h

xor ax, ax
int 16h ; wait for keypress

cmp al, 0x30 ; check if keypress is a number 0x31-0x39 ar numbers from 1 to 9
jbe p_error ; jump to error if number is below or equal to 0x30  

cmp al, 0x39
ja p_error ; jump to error if number is above 0x39

 xor cx, cx ; zero off cx
mov cl, al ; load correct keyboard scancode to cx
sub cx, 0x30 ; substract 0x30 from cx, to get actual number

mov ah, 9 ; DOS (int 21h) printing function
mov dx,txt_ text ; load pointer to the text to print

int 21h
loopnz print ; loops while cx is not equal to 0, after each loop decrases cx by 1, so int 21h is called cx times.

mov ah, 0x4c ; exit
int 21h

p_error: ; prints error
mov dx, txt_error
mov ah, 9
int 21h

mov ah, 0x4c
int 21h

txt_text db "Our super duper cool text here :) $"
txt_error db "Wrong key pressed! $"


 I hope you enjoyed a simple tutorial and learned something. If you have any suggestions for tutorials e-mail me to

Copyright InternetNightmare © - 2004-2009