assume cs:code,ds:data
data segment
count DW 256 dup(0)
count4Number DW 0
count4Uletter DW 0
count4Lletter DW 0
count4Other DW 0
msg1 db "number count:",36
msg2 db "upcase letter count:",36
msg3 db "lowcase letter count:",36
msg4 db "other character count:",36
msg5 db " times appeared character:",36
data ends
code segment
_Break MACRO
NOP
INT 1H
NOP
ENDM
_EnterLine MACRO
MOV AH,02
MOV DL,0AH
INT 21H
MOV DL,0DH
INT 21H
ENDM
_PushAll MACRO
PUSH SI
PUSH DI
PUSH BX
PUSH CX
PUSH DX
ENDM
_PopAll MACRO
POP DX
POP CX
POP BX
POP DI
POP SI
ENDM
_start:
MOV AX,DATA
MOV DS,AX
MOV ES,AX
XOR AX,AX
LEA DI,count
MOV CX,256
_@1:
INC DI
STOSB
INC AX
LOOP _@1
CALL _InputChar
CALL _Sort
CALL _Output
MOV AX,4C00H
INT 21H
_Output PROC
_EnterLine
MOV AH,9
LEA DX,msg1
INT 21H
PUSH count4Number
CALL _OutputByte
_EnterLine
MOV AH,9
LEA DX,msg2
INT 21H
PUSH count4Uletter
CALL _OutputByte
_EnterLine
MOV AH,9
LEA DX,msg3
INT 21H
PUSH count4Lletter
CALL _OutputByte
_EnterLine
MOV AH,9
LEA DX,msg4
INT 21H
PUSH count4Other
CALL _OutputByte
_EnterLine
MOV CX,256
LEA SI,count
XOR BX,BX
_OutputLoop:
LODSW
TEST AL,AL
JE _OutEnd
CMP AL,BL
JE _SameTimes
MOV BL,AL
PUSH AX
_EnterLine
PUSH BX
CALL _OutputByte
MOV AH,9
LEA DX,msg5
INT 21H
POP AX
MOV DL,AH
MOV AH,2
INT 21H
JMP _@@1
_SameTimes:
PUSH AX
MOV AH,02
MOV DL,','
INT 21H
POP AX
MOV DL,AH
MOV AH,2
INT 21H
_@@1:
LOOP _OutputLoop
_OutEnd:
RET
_Output ENDP
_InputChar PROC
LEA DI,count
LEA SI,count4number
XOR CX,CX
_@2:
MOV AH,1
INT 21H
CMP AL,0DH
JE _@3
;小于32大于等于127的字符放弃(32是空格,126是~)
CMP AL,32
JL _@2
CMP AL,127
JAE _@2
;判断数字
CMP AL,'0'
JL _@4
CMP AL,'9'
JG _@5
;是数字
INC BYTE PTR [SI]
JMP _@7
_@5:
;判断大写字母
CMP AL,'A'
JL _@4
CMP AL,'Z'
JG _@6
;是大写字母
INC BYTE PTR [SI+2]
JMP _@7
_@6:
;判断小写字母
CMP AL,'a'
JL _@4
CMP AL,'z'
JG _@4
;是小写字母
INC BYTE PTR [SI+4]
JMP _@7
_@4:
;是其他字符
INC BYTE PTR [SI+6]
_@7:
XOR AH,AH
SHL AX,1
MOV BX,AX
;统计每个字符的出现次数
INC BYTE PTR [DI+BX]
;控制输入字符不超过255个
INC CX
CMP CX,255
JGE _@3
JMP _@2
_@3:
RET
_InputChar ENDP
;字符出现次数排序
_Sort PROC
LEA DI,count
MOV CX,256
_outLoop:
PUSH CX
DEC CX
CMP CX,1
JE _SortOver
MOV BX,CX
_innerLoop:
PUSH BX
SUB BX,CX
SHL BX,1
MOV AX,[DI+BX]
CMP AL,[DI+BX+2]
JAE _BigThan
PUSH [DI+BX+2]
POP [DI+BX]
PUSH AX
POP [DI+BX+2]
_BigThan:
POP BX
LOOP _innerLoop
POP CX
LOOP _outLoop
_SortOver:
POP CX
RET
_Sort ENDP
_OutputByte PROC
PUSH BP
MOV BP,SP
SUB SP,4
_PushAll
MOV WORD PTR [BP-2],0
MOV WORD PTR [BP-4],100
MOV CX,3
MOV AX,[BP+4]
_OBLoop:
XOR DX,DX
DIV WORD PTR [BP-4]
XCHG AX,DX
TEST DX,DX
JNE _OutputIt
MOV BX,[BP-2]
TEST BX,BX
JE _NoOutput
_OutputIt:
PUSH AX
MOV AH,02
ADD DL,030H
INT 21H
INC WORD PTR [BP-2]
POP AX
_NoOutput:
PUSH AX
XOR DX,DX
MOV AX,[BP-4]
MOV BX,10
DIV BX
MOV [BP-4],AX
POP AX
LOOP _OBLoop
MOV AX,[BP-2]
TEST AX,AX
JNE _OBEnd
MOV AH,2
MOV DL,'0'
INT 21H
_OBEnd:
_PopAll
ADD SP,4
MOV SP,BP
POP BP
RET 2
_OutputByte ENDP
CODE ENDS
END _start