.model small .stack 100h .data buffer db 10, ?, 10 dup(?) output db 12, ?, 12 dup(?) prompt db "Please enter a string (max 10 characters):", "$" .code Main proc mov ax, @data mov ds, ax ; 显示提示信息 lea dx, prompt mov ah, 09h int 21h ; 从键盘输入字符串 lea si, buffer + 1 ; 预留第一个字节用于存储长度 input_string: mov ah, 01h int 21h cmp al, 0Dh ; 检查回车键 je end_input stosb inc byte ptr [si-1] cmp byte ptr [si-1], 10 jb input_string end_input: xor al, al stosb ; 子程序1: 检测并清除数字字符 call RemoveDigits ; 子程序2: 字符串中小写字母改为大写字母 call LowerToUpper ; 输出处理后的字符串 lea dx, output mov ah, 09h int 21h ; 退出程序 mov ax, 4C00h int 21h Main endp RemoveDigits proc near lea si, buffer + 1 lea di, output + 1 xor cx, cx mov cl, [si-1] remove_loop: lodsb cmp al, 0 je remove_done ; 判断是否为数字字符 cmp al, '0' jb next_char cmp al, '9' ja next_char ; 删除数字字符,不写入新串 loop remove_loop next_char: ; 写入新串 stosb inc byte ptr [di-2] loop remove_loop remove_done: stosb ret RemoveDigits endp LowerToUpper proc near lea si, output + 1 xor cx, cx mov cl, [si-1] upper_loop: lodsb cmp al, 0 je upper_done cmp al, 'a' jb upper_next_char cmp al, 'z' ja upper_next_char sub al, 20h upper_next_char: dec si mov [si], al inc si loop upper_loop upper_done: ret LowerToUpper endp end Main这个程序输入一个字符后就无法输入,帮我修改
时间: 2024-02-10 18:10:56 浏览: 170
这个程序的问题在于在输入字符串时,它只检查了回车键,但没有检查是否已经输入了最大长度的字符串。因此,当输入了10个字符后,程序将无法继续输入字符。为了解决这个问题,我们需要在输入字符串时检查输入的字符数是否已经达到最大值。下面是修改后的代码:
```
.model small
.stack 100h
.data
buffer db 10, ?, 10 dup(?)
output db 12, ?, 12 dup(?)
prompt db "Please enter a string (max 10 characters):", "$"
.code
Main proc
mov ax, @data
mov ds, ax
; 显示提示信息
lea dx, prompt
mov ah, 09h
int 21h
; 从键盘输入字符串
lea si, buffer + 1 ; 预留第一个字节用于存储长度
mov cx, 0 ; 初始化字符计数器
input_string:
mov ah, 01h
int 21h
cmp al, 0Dh ; 检查回车键
je end_input
cmp cx, 10 ; 检查字符计数器是否达到最大值
jae end_input
stosb
inc cx
jmp input_string
end_input:
mov [si-1], cl ; 存储字符串长度
xor al, al
stosb
; 子程序1: 检测并清除数字字符
call RemoveDigits
; 子程序2: 字符串中小写字母改为大写字母
call LowerToUpper
; 输出处理后的字符串
lea dx, output
mov ah, 09h
int 21h
; 退出程序
mov ax, 4C00h
int 21h
Main endp
RemoveDigits proc near
lea si, buffer + 1
lea di, output + 1
xor cx, cx
mov cl, [si-1]
remove_loop:
lodsb
cmp al, 0
je remove_done
; 判断是否为数字字符
cmp al, '0'
jb next_char
cmp al, '9'
ja next_char
; 删除数字字符,不写入新串
loop remove_loop
next_char:
; 写入新串
stosb
inc byte ptr [di-2]
loop remove_loop
remove_done:
stosb
ret
RemoveDigits endp
LowerToUpper proc near
lea si, output + 1
xor cx, cx
mov cl, [si-1]
upper_loop:
lodsb
cmp al, 0
je upper_done
cmp al, 'a'
jb upper_next_char
cmp al, 'z'
ja upper_next_char
sub al, 20h
upper_next_char:
dec si
mov [si], al
inc si
loop upper_loop
upper_done:
ret
LowerToUpper endp
end Main
```
这个修改后的程序会在输入字符数达到最大值时停止输入,因此可以正常工作。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20210720083447.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)