用8086汇编语言编写程序键盘输入一段字符以#结束,统计其中数字、字母、空格的个数是多少?

如题所述

第1个回答  推荐于2019-09-14
stack segment stack
byte 256 dup(0)
stack ends

data segment
C1 dw 0 ;空格数
C2 dw 0 ;数字数
C3 dw 0 ;字母数
C4 dw 0 ;其它字符数
BUFF db 64, 0, 64 dup(?) ;接收输入缓冲区,BUFF[0]存储缓冲区大小,BUFF[1]存储实际输入数,BUFF[2]开始为输入的数据
SHEX DB '0123456789ABCDEF$'
MSG1 db 13,10,'space: $'
MSG2 db 13,10,'number: $'
MSG3 db 13,10,'alpha: $'
MSG4 db 13,10,'other: $'
MSG5 db 'input your data, end to input #', 13, 10, '$'
data ends

code segment
assume cs:code,ss:stack,ds:data
START:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax

call GetInputData;

call CountInputData;

;输出空格数
mov dx, offset MSG1
mov ax, C1
call PrintLen

;输出数字数
mov dx, offset MSG2
mov ax, C2
call PrintLen

;输出字母数
mov dx, offset MSG3
mov ax, C3
call PrintLen

;输出其他数
mov dx, offset MSG4
mov ax, C4
call PrintLen
PROCEXIT:
; 程序退出
mov ax,4c00h
int 21h

;打印出长度,长度保存在AX里, DX里存储提示信息地址
PrintLen proc near
push ax
mov ah,9h
int 21h
pop ax

mov cx,4
PRINTLOOP:
rol ax,1
rol ax,1
rol ax,1
rol ax,1
push ax
mov dl,al
and dl,0FH
mov bx, offset SHEX
add bl,dl
mov dl, byte ptr [bx]
mov ah,2
int 21h
pop ax
loop PRINTLOOP
ret
PrintLen endp

CountInputData proc near
mov ch,0
mov cl, byte ptr[BUFF+1]
mov si, offset BUFF+2
MYLOOP:
mov al, [si]
inc si

cmp al,20h ;' '空格
je CCOUNT1
cmp al, '0' ;'0'
jl CCOUNT4
cmp al, '9' ;'9'
jle CCOUNT2
cmp al, 'A' ;'A'
jl CCOUNT4
cmp al, 'Z' ;'Z'
jle CCOUNT3
cmp al, 'a' ;'a'
jl CCOUNT4
cmp al, 'z' ;'z'
jle CCOUNT3
CCOUNT4:
inc C4
jmp NEXTLOOP
; 空格
CCOUNT1:
inc C1
jmp NEXTLOOP
; 数字
CCOUNT2:
inc C2
jmp NEXTLOOP
; 字母
CCOUNT3:
inc C3
jmp NEXTLOOP
NEXTLOOP:
loop MYLOOP
ret
CountInputData endp

;获取输出数据,输入数据保存在BUFF里,个数
GetInputData proc near
mov dx, offset MSG5
mov ah, 9h
int 21h

mov ch,0
mov cl,BUFF
lea bx,BUFF+2;
INPUTLOOP:
mov ah,1
int 21h
cmp al, '#'
jz EXITINPUT
cmp al, 13 ;跳过回车符
jz INPUTLOOP
cmp al, 10 ;跳过换行符
jz INPUTLOOP
inc byte ptr[BUFF+1]
mov byte ptr[bx], al
inc bx
loop INPUTLOOP
EXITINPUT:
ret
GetInputData endp

code ends

end START本回答被网友采纳
相似回答