在8086masm环境下,写一个汇编程序,要求以下两个功能分别使用两个子程序实现。 检测并清除数字字符; 字符串中小写字母改为大写字母。从键盘输入(带回显)一个长度在10字符以内的字符串,并保存在缓冲区; 检测并删除字符串中的数字字符,将字符串中所有小写字母改为大写字母; 将处理后的字符串在新的一行显示,若字符串为空串则显示 ‘?’。
时间: 2024-03-10 07:45:48 浏览: 70
好的,我会根据你的要求为你编写汇编程序,以下是完整的代码,你可以自己测试运行:
```
.model small
.stack 100h
.data
buffer db 11,?,10 dup('$')
prompt db 'Enter a string (up to 10 characters): $'
newline db 13,10,'$'
empty db '?$'
.code
extrn read_char_proc:near
extrn write_char_proc:near
main proc
mov ax, @data
mov ds, ax
; prompt user to enter a string
lea dx, prompt
call write_string
; read user input and store it in buffer
lea si, buffer + 2 ; start from the third byte to skip the length byte and the first $
mov cx, 10 ; maximum length of string
call read_string
; check if string is empty
cmp byte ptr [buffer + 1], 0
je display_empty
; remove digits from string
call remove_digits
; convert lowercase letters to uppercase
call to_uppercase
; display processed string
lea dx, newline
call write_string
lea si, buffer + 2
call write_string
mov ah, 4ch
int 21h
main endp
; read a string from keyboard and store it in buffer
read_string proc
mov ah, 0 ; initialize counter
read_char:
call read_char_proc
cmp al, 13 ; check for Enter key
je end_read
cmp al, 8 ; check for Backspace key
je handle_backspace
cmp ah, cl ; check if buffer is full
je end_read
mov byte ptr [si], al ; store character in buffer
inc si ; increment buffer pointer
inc ah ; increment counter
jmp read_char
handle_backspace:
cmp ah, 0 ; check if buffer is empty
je read_char
dec si ; move buffer pointer back
mov al, ' ' ; overwrite previous character with space
call write_char_proc
mov al, 8 ; move cursor back one position
call write_char_proc
dec ah ; decrement counter
jmp read_char
end_read:
mov byte ptr [buffer + 1], ah ; store string length
ret
read_string endp
; remove digits from string
remove_digits proc
lea si, buffer + 2 ; start from the first character
mov cl, byte ptr [buffer + 1] ; get string length
remove_digit:
cmp cl, 0 ; check if end of string is reached
je end_remove_digit
cmp byte ptr [si], '0' ; check if character is a digit
jb skip_remove_digit
cmp byte ptr [si], '9'
ja skip_remove_digit
; character is a digit, remove it
mov al, ' ' ; overwrite digit with space
mov byte ptr [si], al
skip_remove_digit:
inc si ; move to next character
dec cl ; decrement counter
jmp remove_digit
end_remove_digit:
ret
remove_digits endp
; convert lowercase letters to uppercase
to_uppercase proc
lea si, buffer + 2 ; start from the first character
mov cl, byte ptr [buffer + 1] ; get string length
convert_case:
cmp cl, 0 ; check if end of string is reached
je end_convert_case
cmp byte ptr [si], 'a' ; check if character is lowercase letter
jb skip_convert_case
cmp byte ptr [si], 'z'
ja skip_convert_case
; character is a lowercase letter, convert it to uppercase
sub byte ptr [si], 32
skip_convert_case:
inc si ; move to next character
dec cl ; decrement counter
jmp convert_case
end_convert_case:
ret
to_uppercase endp
; write a string to console
write_string proc
push ax ; save registers
push bx
push cx
push dx
mov ah, 09h ; write string function
int 21h ; write string to console
pop dx ; restore registers
pop cx
pop bx
pop ax
ret
write_string endp
; read a character from keyboard with echo
read_char_proc proc
push ax ; save registers
push bx
push dx
mov ah, 01h ; read character with echo
int 21h ; read character from keyboard
mov bl, al ; save character in bl
mov ah, 02h ; write character to console
int 21h ; echo character to console
pop dx ; restore registers
pop bx
pop ax
ret
read_char_proc endp
; write a character to console
write_char_proc proc
push ax ; save registers
push dx
mov ah, 02h ; write character to console
int 21h ; write character to console
pop dx ; restore registers
pop ax
ret
write_char_proc endp
; display '?' if string is empty
display_empty:
lea dx, empty
call write_string
jmp main
end main
```
说明:
1. 程序中使用的缓冲区 `buffer` 长度为 11 字节,前两个字节用于存储字符串长度和起始标志 `$`,后面 10 个字节用于存储用户输入的字符串。
2. 程序中使用的输入函数 `read_string` 从键盘读取用户输入的字符串。输入过程中支持 Backspace 键,可以删除输入的某个字符。如果输入的字符串长度超过 10 个字符,程序会自动截断。
3. 程序中使用的两个子程序 `remove_digits` 和 `to_uppercase` 分别用于删除字符串中的数字字符和将小写字母转换为大写字母。这两个子程序都是基于字符串指针和字符串长度进行处理的。
4. 程序中使用的输出函数 `write_string` 用于将字符串输出到控制台。
5. 程序中使用的输入函数 `read_char_proc` 用于从键盘读取单个字符,并在控制台上显示出来。
6. 程序中使用的输出函数 `write_char_proc` 用于将单个字符输出到控制台。
7. 如果用户输入的字符串为空串,则程序会输出一个问号。
希望这个程序能够帮助到你!
阅读全文