;---------------------------------------------------------
;统计数组中的正数,负数和 0 的个数
;---------------------------------------------------------
;--------------------------------------------------------
; 文件 macro.mac
;---------------------------------------------------------
;---------------------------------------------------------
;屏幕打印回车换行符,用到寄存器 ax,dx
;---------------------------------------------------------
macPutEnt macro
mov dl,0dh
mov ah,02h
int 21h
mov dl,0ah
mov ah,02h
int 21h
endm
;---------------------------------------------------------
;屏幕输出一个字符 OPRCH,用到寄存器 dx
;---------------------------------------------------------
macPutChar macro OPRCH
mov dl,OPRCH
mov ah,02h
int 21h
endm
;---------------------------------------------------------
;屏幕输出以 '$' 结尾的字符串,OPR为字符串变量
;用到寄存器 ax,dx
;---------------------------------------------------------
macPutTxt macro OPR
mov dx,offset OPR
mov ah,09h
int 21h
endm
;---------------------------------------------------------
;结束程序,回到 DOS,用到寄存器 ax,dx
;---------------------------------------------------------
macExit macro
mov ah,4ch
int 21h
endm
;--------------------------------------------------------
; 主文件
;---------------------------------------------------------
;---------------------------------------------------------
if1
include macro.mac
endif
;---------------------------------------------------------
.model small
data segment
array dw 12,123,0,-45,0,89,-6,45,0,5,-65,2,0,-59,12,'$'
msg_pos db 'Positive:','$'
msg_neg db 'Negative:','$'
msg_zr db 'Zero:','$'
rlt_asc db 10 dup(?)
temp_asc db 10 dup(?)
data ends
code segment
assume cs:code,ds:data
;---------------------------------------------------------
;将 ax 中的二进制转为十进制ASCALL 码(带符号数)
;结果存于 rlt_asc 中
;---------------------------------------------------------
bin_to_hec_asc proc far
push bx
push di
mov bx,0
test ax,8000h
jz positive
mov byte ptr rlt_asc[bx],'-' ;负数
inc bx
neg ax
positive: mov di,0
mov dl,10
nextbh: div dl ;字节操作
add ah,30h
mov byte ptr temp_asc[di],ah
inc di
mov ah,0
cmp al,0
jnz nextbh
nextinbh: dec di
js overbth
mov al,byte ptr temp_asc[di]
mov byte ptr rlt_asc[bx],al
inc bx
jmp nextinbh
overbth: mov byte ptr rlt_asc[bx],' '
inc bx
mov byte ptr rlt_asc[bx],'$'
pop di
pop bx
ret
bin_to_hec_asc endp
;---------------------------------------------------------
;主过程
;---------------------------------------------------------
main proc far
mov ax,data
mov ds,ax
mov si,0 ;统计 0
mov di,0 ;统计 正数
mov cx,0 ;统计 负数
mov bx,0
next: mov ax,array[bx]
add bx,2
cmp ax,'$'
jz overtest
cmp ax,0
je zradd
jg posadd
inc cx
jmp next
zradd: inc si
jmp next
posadd: inc di
jmp next
overtest: mov ax,di
call bin_to_hec_asc
macPutTxt msg_pos
macPutTxt rlt_asc
macPutEnt
mov ax,si
call bin_to_hec_asc
macPutTxt msg_zr
macPutTxt rlt_asc
macPutEnt
mov ax,cx
call bin_to_hec_asc
macPutTxt msg_neg
macPutTxt rlt_asc
macPutEnt
macExit
main endp
code ends
end main
评论