1.在数据段定义缓冲区,从键盘接收两串字符到缓冲区,将第二串与第一串字符不一致的字符显示在屏幕。 2.再从键盘输入一位字符到BX,查找第一串字符中有几个相同的字符,并将次数显示在屏幕上。汇编语言实现
时间: 2024-05-12 20:12:47 浏览: 122
1. 汇编代码实现:
```
DATA SEGMENT
BUFFER DB 20 DUP('$')
MESSAGE1 DB 'Enter first string: $'
MESSAGE2 DB 'Enter second string: $'
RESULT DB 20 DUP('$')
NL DB 0DH,0AH,'$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
; Display message to enter first string
LEA DX, MESSAGE1
MOV AH, 09H
INT 21H
; Read first string from keyboard to buffer
LEA DX, BUFFER
MOV AH, 0AH
INT 21H
; Display message to enter second string
LEA DX, MESSAGE2
MOV AH, 09H
INT 21H
; Read second string from keyboard to buffer
LEA DX, BUFFER+2
MOV AH, 0AH
INT 21H
; Find non-matching characters and store in RESULT
LEA SI, BUFFER
LEA DI, BUFFER+2
LEA DX, RESULT
MOV CX, 20
MOV BX, 0
COMPARE:
MOV AL, [SI+2][BX]
CMP AL, '$'
JE DONE
CMP AL, [DI+2][BX]
JE CONTINUE
MOV [DX+BX], AL
INC BX
CONTINUE:
INC SI
INC DI
LOOP COMPARE
DONE:
; Display non-matching characters
LEA DX, NL
MOV AH, 09H
INT 21H
LEA DX, RESULT
MOV AH, 09H
INT 21H
; Read a character to BX
MOV AH, 01H
INT 21H
MOV BL, AL
; Find number of occurrences of character in first string
LEA SI, BUFFER
MOV CX, 20
MOV BX, 0
COUNT:
MOV AL, [SI+2][BX]
CMP AL, '$'
JE DISPLAY_COUNT
CMP AL, BL
JNE CONTINUE_COUNT
INC CX
CONTINUE_COUNT:
INC SI
LOOP COUNT
DISPLAY_COUNT:
; Display number of occurrences of character
LEA DX, NL
MOV AH, 09H
INT 21H
MOV AH, 02H
MOV DL, ' '
INT 21H
MOV DL, BL
INT 21H
MOV DL, ':'
INT 21H
MOV AX, CX
CALL DISPLAY_NUM
JMP END_PROGRAM
DISPLAY_NUM PROC
; Display a number in decimal format
PUSH AX
MOV BX, 10
XOR CX, CX
DIV BX
OR AH, '0'
MOV DL, AH
MOV AH, 02H
INT 21H
POP AX
CMP AX, 0
JE END_DISPLAY
JMP DISPLAY_NUM
END_DISPLAY:
RET
DISPLAY_NUM ENDP
END_PROGRAM:
MOV AH, 4CH
INT 21H
CODE ENDS
END START
```
2. 上述代码中已经包含了查找第一串字符中相同字符的功能,并将次数显示在屏幕上,实现了整个问题的要求。
阅读全文