利用汇编语言编写程序实现1、从键盘输入(带回显)一个长度在10字符以内的字符串,并保存在缓冲区2、检测并删除字符串中的数字字符,将字符串中所有小写字母改为大写字母3、将处理后的字符串在新的一行显示,若字符串为空串则显示 ‘?’。
时间: 2024-01-22 13:20:45 浏览: 95
以下是利用 x86 汇编语言编写程序实现上述功能的示例代码:
```
.MODEL SMALL
.STACK 100H
.DATA
prompt1 db "Enter a string (max 10 characters): $"
prompt2 db "Processed string: $"
buffer db 10 dup("$")
newline db 0AH, 0DH, "$"
question db "?$"
digits db "0123456789$"
lowercase db "abcdefghijklmnopqrstuvwxyz$"
uppercase db "ABCDEFGHIJKLMNOPQRSTUVWXYZ$"
.CODE
MAIN PROC
; Display prompt1 and read input string into buffer
mov ax, @data
mov ds, ax
lea dx, prompt1
mov ah, 09h
int 21h
lea dx, buffer
mov ah, 0Ah
int 21h
; Check if input string is empty
mov al, [buffer+1]
cmp al, 0
jz EMPTY_STRING
; Process input string
lea si, buffer+2
lea di, buffer+2
mov cx, [buffer+1]
mov bx, 0
next_char:
; Check if character is a digit
mov al, [si]
mov dl, al
mov ah, 0
mov si, offset digits
mov bl, 0
check_digit:
cmp dl, [si]
jz is_digit
inc si
inc bl
cmp bl, 11
jl check_digit
jmp not_digit
is_digit:
; Skip digit character
inc si
inc si
dec cx
jmp next_char
not_digit:
; Check if character is lowercase letter
mov al, [di]
mov dl, al
mov ah, 0
mov di, offset lowercase
mov bl, 0
check_lowercase:
cmp dl, [di]
jz is_lowercase
inc di
inc bl
cmp bl, 27
jl check_lowercase
jmp not_lowercase
is_lowercase:
; Convert lowercase to uppercase
mov al, [di+26]
mov [di-26], al
not_lowercase:
; Move to next character
inc di
inc si
loop next_char
; Display processed string
lea dx, prompt2
mov ah, 09h
int 21h
mov al, [buffer+1]
cmp al, 0
jz PRINT_QUESTION
lea dx, buffer+2
mov ah, 09h
int 21h
jmp EXIT_PROGRAM
; Display '?' for empty string
EMPTY_STRING:
lea dx, question
mov ah, 09h
int 21h
jmp EXIT_PROGRAM
; Display '?' for input error
PRINT_QUESTION:
lea dx, newline
mov ah, 09h
int 21h
lea dx, question
mov ah, 09h
int 21h
; Exit program
EXIT_PROGRAM:
mov ah, 4ch
int 21h
MAIN ENDP
END MAIN
```
上述程序使用了 DOS 中断来进行键盘输入和屏幕输出。程序首先提示用户输入一个字符串,并将其保存到缓冲区中。然后程序检查字符串是否为空,如果为空则显示 '?'。否则,程序逐个检查字符串中的字符,将数字字符跳过并将小写字母转换为大写字母。最后,程序显示处理后的字符串,如果为空则显示 '?'。
阅读全文