Jakash3’s Basic I/O Library

The following applies to 16-bit dos assembly language, code examples are for fasm.
I’ve made a basic console i/o library in asm which is a collection of these 6 functions that each use the BIOS interrupts for their operations:

  • printc – Prints a character
  • prints – Print string
  • inputc – Character input
  • inputs – String input
  • inccur – Increases cursor position
  • deccur – Decreases cursor position

Here is a working example of using it:

org 100h
use16
push 20       ;Length of buffer (excluding room for terminating null char)
push buffer   ;Address of buffer
call inputs   ;Get user string input (ending with cr)
              ;address of buffer still on stack from last call
call prints   ;print buffer
call inputc   ;pause by calling character input
int 20h       ;terminate
buffer db 21 dup (0)   ;declare 21 bytes of

and here’s the library:

;Jakash3's basic i/o library. Unless otherwise commented,
;each function returns the current cursor position.
;return values placed in ax after function call.
;arguments for functions pushed to stack.

printc:                     ;printc(byte c)
push bp
mov bp,sp
push bx
push cx
push dx
push si
push di
mov ah,0fh
int 10h
mov ah,0ah
mov al,byte [bp+4]
cmp al,0dh
je printc_cr
cmp al,0ah
je printc_lf
jmp printc_continue
printc_cr:
mov ah,03
int 10h
mov dl,0
mov ah,02
int 10h
jmp printc_done
printc_lf:
mov ah,03
int 10h
inc dh
mov ah,02
int 10h
jmp printc_done
printc_continue:
mov cx,1
int 10h
call inccur
printc_done:
mov ah,03
int 10h
mov ah,dh
mov al,dl
pop di
pop si
pop dx
pop cx
pop bx
mov sp,bp
pop bp
ret

prints:                     ;prints(word str_ptr)
push bp
mov bp,sp
push bx
push cx
push dx
push si
push di
mov bx,word [bp+4]
sub sp,1
prints_loop:
mov dl,byte [bx]
cmp dl,0
jz prints_done
mov byte [bp-11],dl
call printc
inc bx
jmp prints_loop
prints_done:
mov ah,03
int 10h
mov ah,dh
mov al,dl
pop di
pop si
pop dx
pop cx
pop bx
mov sp,bp
pop bp
ret

inputc:                     ;byte inputc()
push bp                     ;returns byte value of single character input
mov bp,sp
push bx
push cx
push dx
push si
push di
mov ah,00
int 16h
sub sp,2
mov byte [bp-11],al
mov byte [bp-12],al
call printc
mov ah,0
mov al,byte [bp-11]
pop di
pop si
pop dx
pop cx
pop bx
mov sp,bp
pop bp
ret

inputs:                     ;byte inputs(byte len,word buff_ptr)
push bp                     ;returns number of bytes read from input
mov bp,sp
push bx
push cx
push dx
push si
push di
sub sp,1
mov bx,word [bp+4]
mov ch,byte [bp+6]
xor cl,cl
input_L1:
mov ah,0
int 16h
cmp al,08
je input_bksp
cmp al,0dh
je input_br1
cmp cl,ch
jge input_L1
mov byte [bx],al
inc bx
mov byte [bp-11],al
call printc
inc cl
jmp input_L1
input_bksp:
cmp cl,0
je input_L1
mov byte [bx],0
dec bx
mov byte [bx],0
mov byte [bp-11],0
call printc
call deccur
call printc
dec cl
jmp input_L1
input_br1:
mov byte [bx],0
mov ah,0
mov al,cl
pop di
pop si
pop dx
pop cx
pop bx
mov sp,bp
pop bp
ret

deccur:                     ;increment cursor position
push bp
mov bp,sp
push bx
push cx
push dx
push si
push di
sub sp,1
mov ah,0fh
int 10h
mov byte [bp-11],ah
mov ah,03
int 10h
cmp dl,0
jne deccur_continue
mov dl,byte [bp-11]
dec dh
jmp deccur_done
deccur_continue:
dec dl
deccur_done:
mov ah,02
int 10h
add sp,1
mov ah,03
int 10h
mov ah,dh
mov dl,dl
pop di
pop si
pop dx
pop cx
pop bx
mov sp,bp
pop bp
ret

inccur:                     ;decrement cursor position
push bp
mov bp,sp
push bx
push cx
push dx
push si
push di
sub sp,1
mov ah,0fh
int 10h
mov byte [bp-11],ah
mov ah,03
int 10h
cmp dl,byte [bp-11]
jne inccur_continue
mov dl,0
inc dh
jmp inccur_done
inccur_continue:
inc dl
inccur_done:
mov ah,02
int 10h
add sp,1
mov ah,03
int 10h
mov ah,dh
mov al,dl
pop di
pop si
pop dx
pop cx
pop bx
mov sp,bp
pop bp
ret

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: