Assembly Language: Simple IO Program

The following example illustrates a simple program that gets keyboard input from a user, adds two numbers together, and displays output to the standard video output. This routine uses three basic DOS calls: the DOS function 1 (StdConInput) for input, the DOS function 2 (StdConOutput) for output, and the DOS function 4CH (Exit) to terminate the routine. This routine gets two single-digit numbers from a user, then adds the numbers together and displays the results. The routine continues to operate until the user enters a keyboard character that is not a number.


;Code routine to add numbers
.MODEL small
;######################################
.STACK 500
;######################################
.DATA
data_1 dw 0
;######################################
.CODE
start proc near
mov dx,@data ;get data segment index
mov ds,dx ;set data segment
add_loop:
call get_number
jc number_error
mov data_1,ax
call get_number
jc number_error
add ax,data_1
call display_number
jmp add_loop
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

number_error:
mov ah,4CH ;set for DOS terminate function
mov al,0 ;set terminating variable code
int 21H ;call DOS to terminate

start endp
;************************************

get_number proc near
;on exit if carry clear then number in AX is OK else ERROR
call display_new_line
mov ah,1 ;set for read keyboard function
int 21H ;call DOS to get keyboard data
cmp al,’0’ ;test for valid number
jb get_number_bad ;jump if bad number
cmp al,’9’
ja get_number_bad ;jump if bad number
an ax,0FH
clc ;set OK exit code
ret ;exit
get_number_bad:
stc ;set ERROR exit code
ret ;exit subroutine
get_number endp
;************************************
display_number proc near
;on exit binary number in AX is displayed (between 0 to 19)
call display_new_line
display_number_1:
cmp al,9
ja display_number_2
or al,30H
mov dl,al
mov ah,2 ;set for DOS display character
int 21H ;call DOS function
ret ;exit subroutine
display_number_2:
sub al,10 ;adjust number
push ax ;save adjusted number
mov dl,’1’
mov ah,2 ;set for DOS display character
int 21H ;call DOS function
pop ax ;restore adjusted number
jmp display_number_1

display_number endp
;************************************
display_new_line proc near
;on exit cursor is at start of next line down
push ax ;save AX register
push dx ;save DX register
mov dl,0DH ;start of line character
mov ah,2 ;set for DOS display character
int 21H ;call DOS function
mov dl,0AH ;new line character
mov ah,2 ;set for DOS display character
int 21H ;call DOS function
pop dx ;restore DX register
pop ax ;restore AX register
ret ;exit subroutine
display_new_line endp
;************************************
end start

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.