title Console Library Module

advertisement
80x86 Assembly Subroutines
title Console Library Module
;(console.asm)
public Clrscr, Crlf, Delay_seconds, Get_time, Gotoxy, \
Readchar, Readkey, Scroll, Seconds_today, Set_videoseg, \
Show_time, Waitchar, Writechar, Writestring_direct, \
Writechar_direct
.model small
.386
include console.inc
.code
Page 1
80x86 Assembly Subroutines
; Clrscr ------------------------------------------;
; Clears the entire screen and locates the cursor
; at row 0, column 0. No parameters. Works only on
; video page 0, in text mode.
;
;--------------------------------------------------Clrscr proc
pushad
mov
mov
mov
mov
int
mov
mov
mov
int
popad
ret
Clrscr endp
ax,0600h
cx,0
dx,184Fh
bh,7
10h
ah,2
bh,0
dx,0
10h
;
;
;
;
;
;
;
scroll entire screen up
upper left corner (0,0)
lower right corner (24,79)
normal attribute
call BIOS
locate cursor at 0,0
video page 0
Page 2
80x86 Assembly Subroutines
; Crlf --------------------------------------------;
; Writes a carriage return / linefeed
; sequence (0Dh,0Ah) to standard output.
;
;--------------------------------------------------Crlf proc
pushad
mov
ah,2
mov
dl,0Dh
int
21h
mov
dl,0Ah
int
21h
popad
ret
Crlf endp
; write character
; carriage return
; linefeed
Page 3
80x86 Assembly Subroutines
;Delay_seconds -----------------------------------;
; Causes the program to pause for n seconds. Input
; parameter: EAX contains the number of seconds.
;
;--------------------------------------------------Delay_seconds proc
pushad
mov ecx,eax
call Seconds_today
mov ebx,eax
DLY1:
call
sub
cmp
jb
Seconds_today
eax,ebx
eax,ecx
DLY1
; delay, in seconds
; save start time
;
;
;
;
get the time
subtract from start
delay finished yet?
if not, continue loop
popad
ret
Delay_seconds endp
Page 4
80x86 Assembly Subroutines
;Get_time ---------------------------------------;
; Get the current system time.Input parameter:
; DS:SI points to a TimeRecord structure.
;
;--------------------------------------------------Get_time proc
pushad
mov ah,2Ch
int 21h
mov [si].hours,ch
mov [si].minutes,cl
mov [si].seconds,dh
mov [si].hhss,dl
popad
ret
Get_time endp
Page 5
80x86 Assembly Subroutines
; Gotoxy ------------------------------------------;
; Locates the cursor at row, col on video page
; zero. Input parameters: DH = row, DL = column.
;
;--------------------------------------------------Gotoxy proc
pushad
mov
ah,2
mov
bh,0
int
10h
popad
ret
Gotoxy endp
; function 2: set cursor position
; video page 0
Page 6
80x86 Assembly Subroutines
; Readchar -----------------------------------------;
; Reads a single character from standard input
; without echoing the character or waiting for input.
; Output: If a character was found, ZF = 0 and
; AL contains the character. If no character was
; found, ZF = 1.
;
;--------------------------------------------------Readchar proc
push edx
mov ah,6
mov dl,0FFh
int 21h
pop edx
ret
Readchar endp
Page 7
80x86 Assembly Subroutines
; Readkey -----------------------------------------;
; Waits for a key until one is pressed. Returns the
; scan code in AH and the ASCII code in AL.
; Cannot be redirected.
;
;--------------------------------------------------Readkey proc
mov ah,10h
int 16h
ret
Readkey endp
Page 8
80x86 Assembly Subroutines
; Scroll ------------------------------------------;
; Scrolls all lines in a prescribed window. No range
; checking is performed on the input parameters:
;
;
CH Row of the window's upper left corner
;
CL Column of the window's upper left corner
;
DH Row of the window's lower right corner
;
DL Column of the window's lower right corner
;
BH Attribute/color of the scrolled lines
;
;--------------------------------------------------Scroll proc
pushad
mov
int
mov
call
popad
ret
Scroll endp
ax,0600h
10h
dx,0
Gotoxy
; function: scroll window up
; call BIOS
; locate cursor at 0,0
Page 9
80x86 Assembly Subroutines
; Seconds_today ------------------------------------;
; Returns a count of the number of seconds that
; have elapsed today. Output: EAX contains the
; return value.
;
;--------------------------------------------------.data
timerec TimeRecord <>
saveSec dd ?
.code
Seconds_today proc
push ebx
push si
mov si,offset timerec
call Get_time
; calculate # seconds based on hours.
xor eax,eax
mov al,[si].hours
mov ebx,3600
mul ebx
mov saveSec,eax
; multiply minutes by 60
xor eax,eax
mov al,[si].minutes
mov ebx,60
mul ebx
add saveSec,eax
xor eax,eax
mov al,[si].seconds
add eax,saveSec
pop si
pop ebx
ret
Seconds_today endp
Page 10
80x86 Assembly Subroutines
; Show_time ---------------------------------------;
; Write a TimeRecord structure in 24-hour military
; format to standard output. Input: DS:SI points
; to the structure.
;
;--------------------------------------------------Show_time proc
.data
colonStr db ":",0
.code
extrn Writeint:proc, WriteString:proc
pushad
mov bx,10
; decimal radix
mov ah,0
mov al,[si].hours
call WriteInt
mov dx,offset colonStr
call WriteString
mov al,[si].minutes
call WriteInt
popad
ret
Show_time endp
Page 11
80x86 Assembly Subroutines
; WritestringDirect -----------------------------;
; Write a string directly to video RAM. Input
; parameters: DS:SI points to a null-terminated
; string, AH = attribute, DH/DL = row/column on
; the screen.
;
;------------------------------------------------Writestring_direct proc
pushad
L1:
mov
cmp
je
call
inc
inc
jmp
al,[si]
; get character
al,0
; check for null byte
L2
; quit if found
Writechar_direct
si
; next character
dl
; next column
L1
L2:
popad
ret
Writestring_direct endp
Page 12
80x86 Assembly Subroutines
COLOR_SEGMENT = 0B800h ; color video
.data
videoSegment dw COLOR_SEGMENT
.code
; Set_videoseg --------------------------------;
; Set the current video segment address (the
; default is B800h). Input parameter: AX
; contains the address value.
;
;----------------------------------------------Set_videoseg proc
mov videoSegment,ax
ret
Set_videoseg endp
Page 13
80x86 Assembly Subroutines
; Waitchar -----------------------------------------;
; Reads a single character from standard input
; without echoing the character. Waits for input.
; Output: AL contains the character.
;
;--------------------------------------------------Waitchar proc
mov ah,7
int 21h
ret
Waitchar endp
Page 14
80x86 Assembly Subroutines
;Writechar -----------------------------------;
; Write a character to standard output, using
; INT 21h.
;
;----------------------------------------------Writechar proc
pushad
mov dl,al
mov ah,2
int 21h
popad
ret
Writechar endp
Page 15
80x86 Assembly Subroutines
;Writechar_direct -----------------------------;
; Write a character directly to VRAM. Input
; parameters: AL = char, AH = attribute,
; DH/DL = row, column on screen (0-24, 0-79).
;
;----------------------------------------------Writechar_direct proc
pushad
mov di,videoSegment
mov es,di
; multiply the row by 160
push ax
mov ax,160
mul dh
mov di,ax
; multiply the column by 2, add to DI
shl dl,1
mov dh,0
add di,dx
pop ax
mov es:[di],ax ; store char/attribute
popad
ret
Writechar_direct endp
Page 16
Download