题目:计算字符串中字母、数字、其他字符的个数。
代码:
DATAS SEGMENT
BUF DB 80
LEN DB ?
STRING DB 80 DUP(32)
crlf db 13,10,'$'
displ DB 'The amount of letters is:',13,10,'$'
dispd db 'The amount of digits is:',13,10,'$'
dispo db 'The amount of others is:',13,10,'$'
LETTER DB 0
DIGIT DB 0
OTHER DB 0
DATAS ENDS
STACKS SEGMENT
DB 200 DUP (?)
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX
call input
call dispcrlf
mov dx,offset string
mov ah,09
int 21h
CALL TONGJI
CALL RESULT
MOV AH,4CH
INT 21H
input proc near;输入
mov dx,offset buf
mov ah,10
int 21h
ret
input endp
TONGJI PROC NEAR;统计
LEA BX,STRING
MOV CL,LEN
LOOP1:
MOV AL,[BX]
CALL ISLETTER
JNC NEXT
INC LETTER
JMP THE_END
NEXT:
CALL ISDIGIT
JNC THE_END
INC DIGIT
THE_END:
INC BX
LOOP LOOP1
ret
TONGJI ENDP
ISLETTER PROC NEAR
CMP AL,'A'
JB EXIT1
CMP AL,'Z'
JBE EXIT2
CMP AL,'a'
JB EXIT1
CMP AL,'z'
JBE EXIT2
EXIT1:
CLC
RET
EXIT2:
STC
RET
ISLETTER ENDP
ISDIGIT PROC NEAR
CMP AL,'0'
JB NOTD
CMP AL,'9'
JA NOTD
STD
RET
NOTD:
CLC
RET
ISDIGIT ENDP
RESULT PROC NEAR
mov dx,offset crlf
mov ah,09h
int 21h
mov dx,offset displ;输出字母个数
mov ah,09
int 21h
mov bl,letter
call out_amount
call dispcrlf
mov dx,offset dispd;输出数字个数
mov ah,09
int 21h
mov bl,digit
call out_amount
call dispcrlf
mov dx,offset dispo;输出其他字符个数
mov ah,09
int 21h
mov bl,len
sub bl,letter
sub bl,digit
call out_amount
call dispcrlf
ret
RESULT ENDP
dispcrlf proc near
mov dx,offset crlf
mov ah,09
int 21h
ret
dispcrlf endp
out_amount proc near
;要输出的数存储在bl中
push ax
push dx
push cx
mov cl,4
mov al,bl
shr al,cl
mov dl,al
and dl,0fh
add dl,30h
mov ah,02h
int 21h
mov dl,bl
and dl,0fh
add dl,30h
mov ah,02h
int 21h
pop cx
pop dx
pop ax
ret
out_amount endp
CODES ENDS
END START