;从键盘上接收一个以$为结束符的字符串,统计并输出其中非数字字符的个数
DATA SEGMENT
BUFFER DB 128,?,128 DUP('$')
COUNT DW 0
CRLF DB 0AH,0DH,'$'
DATA ENDS
STACK SEGMENT PARA STACK
DW 128 DUP(?)
TOP LABEL WORD
STACK ENDS
CODE SEGMENT
ASSUME CS:CODE,SS:STACK,DS:DATA
MAIN PROC FAR
START:
MOV AX,DATA
MOV DS,AX
MOV AX,STACK
MOV SS,AX
LEA SP,TOP
LEA DX,BUFFER ;INT 21H的0AH号功能:DS:DX->缓冲区首地址
MOV AH,0AH ;(DS:DX)=预计输入缓冲区字符数,(DS:DX+1)=实际输入字符数
INT 21H ;(DS:DX+2)->缓冲区开始,分别对应上面的BUFFER DB 128,?,128 DUP('$')
LEA SI,BUFFER+1 ;(DS:DX+1)=实际输入字符数
MOV CL,[SI]
XOR CH,CH
LEA DI,BUFFER+2 ;(DS:DX+2)->缓冲区开始
L0: MOV AL,[DI]
CMP AL,30H
JL L1
CMP AL,39H
JG L1
DEC COUNT ;是数字的话先加1再减1相互抵消
L1: INC COUNT
INC DI
LOOP L0
XOR CX,CX ;CX清零,用于记录余数的个数
MOV AX,COUNT
MOV BL,10
L2: DIV BL
PUSH AX
INC CX
XOR AH,AH
CMP AL,0
JNZ L2
MOV AH,9
LEA DX,CRLF
INT 21H
L3: POP DX
MOV DL,DH
ADD DL,30H
MOV AH,2
INT 21H
LOOP L3
MOV AH,4CH
INT 21H
CODE ENDS
MAIN ENDP
END START