汇编语言习题:在数据段中自定义一个以符号$结尾的字符串,前两个字符是字符串长度的十六进制表示,之后则是字符串的内容。另外,请在数据段中定义一个字节型变量Result,长度为3个字节,然后统计这个字符串中数字,字母,其他字符(即除数字、字母外的)的数量,并将结果依次存放到Result的三个字节中,然后按顺序以16进制方式打印。注意:字符串末尾的$不算在内。 输入样例1:041AC$ 输出样例1:010200。请写出完整代码
时间: 2024-03-21 07:41:26 浏览: 73
以下是汇编代码,适用于8086微处理器:
```
DATA SEGMENT
STR DB "041AC"
RESULT DB 0, 0, 0
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
; 统计数字
MOV CX, 0
MOV AL, '0'
LEA SI, STR+2
MOV BL, [SI]
SUB BL, '0'
MOV BH, 0
ADD CX, BX
INC SI
DEC BL
JCXZ NEXT
COUNT_DIGITS:
CMP [SI], 0
JE NEXT
CMP [SI], AL
JB SKIP1
CMP [SI], '9'
JA SKIP1
INC CX
SKIP1:
INC SI
DEC BL
JNZ COUNT_DIGITS
; 统计字母
MOV CX, 0
MOV AL, 'A'
LEA SI, STR+2
COUNT_LETTERS:
CMP [SI], 0
JE NEXT
CMP [SI], AL
JB SKIP2
CMP [SI], 'Z'
JA CHECK_LOWER
INC CX
CHECK_LOWER:
CMP [SI], 'a'
JB SKIP2
CMP [SI], 'z'
JA SKIP2
INC CX
SKIP2:
INC SI
JMP COUNT_LETTERS
; 统计其他字符
MOV CX, 0
LEA SI, STR+2
COUNT_OTHERS:
CMP [SI], 0
JE NEXT
CMP [SI], '0'
JB SKIP3
CMP [SI], '9'
JA CHECK_UPPER
JMP SKIP3
CHECK_UPPER:
CMP [SI], 'A'
JB SKIP3
CMP [SI], 'Z'
JA CHECK_LOWER2
JMP SKIP3
CHECK_LOWER2:
CMP [SI], 'a'
JB SKIP3
CMP [SI], 'z'
JA INC_CX
JMP SKIP3
INC_CX:
INC CX
SKIP3:
INC SI
JMP COUNT_OTHERS
; 存储结果
NEXT:
MOV AL, CH
MOV [RESULT], AL
MOV AL, CL
MOV [RESULT+1], AL
MOV AL, DL
MOV [RESULT+2], AL
; 打印结果
MOV AH, 02H
MOV DL, [RESULT]
ADD DL, '0'
INT 21H
MOV DL, [RESULT+1]
ADD DL, '0'
INT 21H
MOV DL, [RESULT+2]
ADD DL, '0'
INT 21H
; 退出
MOV AH, 4CH
INT 21H
CODE ENDS
END START
```
阅读全文