;输出字符串函数
prompt proc
mov ah, 09h
int 21h
ret
prompt endp
;输出回车换行符函数
crlf proc
mov dl, 0dh
mov ah, 2
int 21h
mov dl, 0ah
mov ah, 2
int 21h
ret
crlf endp
;输出空格符函数
blank proc
mov dl, 20h
mov ah, 2
int 21h
ret
blank endp
;二进制的输入函数
;输入范围0~1111111111111111
inb proc
push cx
inbstart:
mov bx, 0
accept:
mov ah, 1
int 21h
cmp al, 0dh
je inbexit
cmp al, 30h
jl inberr
cmp al, 31h
ja inberr
sub al, 30h
mov ah, 0
mov ax, bx
mov cx, 2
mul cx
add bx, ax
jmp accept
inbexit:
pop cx
ret
inberr:
lea dx, 'bad format...$'
call prompt
jmp inbstart
inb endp
;八进制的输入函数
;范围:0-177777
inq proc
push cx
pop cx
ret
inq endp
;十进制的输入函数
;范围:0-65535
ind proc
push cx
indstart:
mov bx, 0
accept:
mov ah, 1
int 21h
cmp al, 0dh
je indexit
cmp al, 30h
jl inderr
cmp al, 39h
ja inderr
sub al, 30h
mov ah, 0
xchg ax, bx
mov cx, 10
mul cx
add bx, ax
jmp accept
indexit:
pop cx
ret
inderr:
mov dx, offset 'bad format...$'
call prompt
jmp indstart
ind endp
;十六进制的输入函数
;范围:0-0FFFFH
inh proc
push cx
inhstart:
mov bx, 0
accept:
mov ah, 1
int 21h
cmp al, 0dh
je inhexit
cmp al, 30h
jl inherr
cmp al, 66h ;'f'
ja inherr
cmp al, 39h ;'9'
jl num
cmp al, 41h ;'A'
jl inherr
cmp al, 46h ;'F'
ja lowercase
sub al, 7h ;'A'-':'=7h
jmp num
lowercase:
cmp al, 61h ;'a'
jl inherr
cmp al, 66h ;'f'
ja inherr
sub al, 27h ;'a'-':'=27h
num:
and ax, 0fh
xchg ax, bx
mov cx, 16 ;mov cx, 4
mul cx ;shl ax
add bx, ax
jmp accept
inhexit:
pop cx
ret
inherr:
lea dx, 'bad format...$'
call prompt
jmp inhstart
inh endp
;二进制的输出函数
;范围:0-1111111111111111
;call: 要输出的数字放在bx中传入
outb proc
push cx
mov nozero, 0 ;like a boolean variable
mov cl, 16
nextbit:
mov dl, 0
test bx, 8000h
je disp
mov nozero, 1
mov dl, 1
disp:
cmp nozero, 0
je continue
add dl, 30h
mov ah, 2
int 21h
continue:
shl bx, 1
loop nextbit
cmp nozero, 0
jne outbexit
mov dl, 30h ;output '0' because the number in bx is zero
mov ah, 2
int 21h
outbexit:
pop cx
ret
outb endp
;八进制的输出函数
;范围:0-177777
;call: 要输出的数字放在bx中传入
;十进制的输出函数
;范围:0-65535
;call: 要输出的数字放在bx中传入
outd proc
push cx
mov nozero, 0 ;like a boolean variable
mov cx, 10000
call decdiv
mov cx, 1000
call decdiv
mov cx, 100
call decdiv
mov cx, 10
call decdiv
mov cx, 1
call decdiv
cmp nozero, 0
jne outdexit
mov dl, 30h
mov ah, 2
int 21h
outdexit:
pop cx
ret
;---sub proc---
;call: 被除数在bx传入,除数在cx传入
;ret: 打印商,余数送bx
decdiv proc
mov ax, bx
mov dx, 0
div cx
mov bx, dx
mov dl, al
cmp dl, 0
jne dispdigit
cmp nozero, 0
jne dispdigit
jmp decdivexit
dispdigit:
mov nozero, 1
add dl, 30h
mov ah, 2
int 21h
decdivexit:
ret
decdiv endp
;---sub proc---
outd endp
;十六进制的输出函数
;范围:0-0FFFFH(或0-0ffffh)
;输出十六进制用大写字母(A-F表示)
;call: 要输出的数字放在bx中传入
outh proc
push cx
mov ch, 4
mov nozero, 0 ;like a boolean variable
next4bits:
mov cl, 4
rol bx, cl
mov al, bl
and al, 0fh
cmp al, 10
jl dispdigit
add al, 7h ;if disp 'a-f' ,should add 27h
dispdigit:
cmp al, 0
jne dispnozero
cmp nozero, 0
je continue
dispnozero:
mov nozero, 1
add al, 30h
mov dl, al
mov ah, 2
int 21h
continue:
dec ch
jnz next4bits
cmp nozero, 0
jne outhexit
mov dl, 30h
mov ah, 2
int 21h
outhexit:
pop cx
ret
outh endp
;冒泡排序函数(降序)(sort in DW array<2 bytes>)
bubble proc
push cx
mov cx, count ;NOTICE: count = nums.length-1
outloop:
mov di, cx
mov si, 0
inloop:
mov ax, nums[si]
cmp ax, nums[si+2] ;nums[si+1], if sort in 8 byte
jae incontinue
xchg ax, nums[si+2] ;nums[si+1], if sort in 8 byte
xchg ax, nums[si]
incontinue:
add si, 2 ;add si, 1, if sort in 8 byte
loop inloop
mov cx, di
loop outloop
pop cx
ret
bubble endp
;查找最小ASCII码
;call: 被查找ASCII串码放在buff中
;ret: 找到的最小ASCII码将被放在minascii中返回
getmin proc
push cx
lea si, buff+2
mov cl, num+1
and cx, 0fh
mov bl, [si]
next:
inc si
cmp bl, [si]
jle continue
mov bl, [si]
continue:
loop next
mov minascii, bl
pop cx
ret
getmin endp
;统计非字符和非数字的个数
;call: 被统计ASCII串码放在buff中
;ret: 统计的数字将被放在sum中返回
count proc
push cx
mov cl, buff+1
lea si, buff+2
mov sum, 0
next:
mov al, [si]
cmp al, '0' ;30h
jb yes
cmp al, '9' ;39h
jb no
cmp al, 'A' ;41h
jb yes
cmp al, 'Z' ;5ah
jb no
cmp al, 'a' ;61h
jb yes
cmp al, 'z' ;7ah
jb no
jmp yes
yes:
inc byte ptr sum
no:
inc si
loop next
pop cx
ret
count endp