使用汇编语言 从键盘输入一个长度不超过30的字符串,其中包含自己的班级名称+学号+姓名的拼音(班级和姓名的首字母大写),以“$”符号结束,之后完成以下任务(要求使用子程序): 屏幕显示输出该字符串; 将该字符串的奇数位置字符提取出来,组合成一个字符串显示输出。 将该字符串中的所有数字字符剔除,然后显示输出; 将该字符串中的英文大写字母变成小写字母,小写字母变成大写字母,显示输出; 统计该字符串中的大写字母、小写字母和数字的个数,保存在内存变量中,并在屏幕上显示; 将该字符串中的数字进行累加,将累加结果显示输出。
时间: 2023-06-14 17:03:19 浏览: 102
以下是使用汇编语言实现以上任务的代码:
```
.model small
.stack 100h
.data
msg db 0ah, 0dh, 'Please enter a string (no more than 30 characters, end with "$"): $'
str db 31 dup('$')
odd db 15 dup('$')
no_digit db 31 dup('$')
count_upper dw 0
count_lower dw 0
count_digit dw 0
sum dw 0
result db 0ah, 0dh, 'Output:', 0ah, 0dh, '$'
result_odd db 0ah, 0dh, 'Odd characters:', 0ah, 0dh, '$'
result_no_digit db 0ah, 0dh, 'No digit characters:', 0ah, 0dh, '$'
result_count db 0ah, 0dh, 'Count:', 0ah, 0dh, 'Upper:', 0ah, 0dh, 'Lower:', 0ah, 0dh, 'Digit:', 0ah, 0dh, '$'
result_sum db 0ah, 0dh, 'Sum:', 0ah, 0dh, '$'
.code
main proc
mov ax, @data
mov ds, ax
; prompt user to enter a string
mov ah, 09h
lea dx, msg
int 21h
; read string from keyboard
lea si, str
mov cx, 31
mov ah, 01h
read_loop:
int 21h
cmp al, '$'
je input_done
mov [si], al
inc si
loop read_loop
input_done:
mov [si], '$'
; display original string
lea dx, result
mov ah, 09h
int 21h
lea dx, str
mov ah, 09h
int 21h
; extract odd characters
lea si, str
lea di, odd
mov cx, 0
extract_loop:
mov al, [si]
cmp al, '$'
je extract_done
inc cx
cmp cx, 2
jne skip_odd
mov [di], al
inc di
skip_odd:
inc si
loop extract_loop
extract_done:
mov [di], '$'
; display odd characters
lea dx, result_odd
mov ah, 09h
int 21h
lea dx, odd
mov ah, 09h
int 21h
; remove digit characters
lea si, str
lea di, no_digit
remove_digit_loop:
mov al, [si]
cmp al, '$'
je remove_digit_done
cmp al, '0'
jb not_digit
cmp al, '9'
ja not_digit
jmp next_char
not_digit:
mov [di], al
inc di
next_char:
inc si
loop remove_digit_loop
remove_digit_done:
mov [di], '$'
; display string without digits
lea dx, result_no_digit
mov ah, 09h
int 21h
lea dx, no_digit
mov ah, 09h
int 21h
; change case of letters
lea si, str
change_case_loop:
mov al, [si]
cmp al, '$'
je change_case_done
cmp al, 'A'
jb next_char2
cmp al, 'Z'
ja next_char2
add al, 20h
jmp next_char2
next_char2:
cmp al, 'a'
jb next_char3
cmp al, 'z'
ja next_char3
sub al, 20h
jmp next_char3
next_char3:
mov [si], al
inc si
loop change_case_loop
change_case_done:
; count upper, lower, and digit characters
lea si, str
count_loop:
mov al, [si]
cmp al, '$'
je count_done
cmp al, 'A'
jb next_char4
cmp al, 'Z'
ja next_char4
inc count_upper
jmp next_char4
next_char4:
cmp al, 'a'
jb next_char5
cmp al, 'z'
ja next_char5
inc count_lower
jmp next_char5
next_char5:
cmp al, '0'
jb next_char6
cmp al, '9'
ja next_char6
inc count_digit
add ah, al
adc dh, 0
next_char6:
inc si
loop count_loop
count_done:
; display count results
lea dx, result_count
mov ah, 09h
int 21h
mov ax, count_upper
call print_word
mov ax, count_lower
call print_word
mov ax, count_digit
call print_word
; display sum of digits
lea dx, result_sum
mov ah, 09h
int 21h
mov ax, sum
call print_word
mov ah, 4ch
int 21h
main endp
; print a word in decimal format
print_word proc value:word
push ax
push bx
push dx
mov ax, value
mov bx, 10
xor cx, cx
print_digit_loop:
xor dx, dx
div bx
push dx
inc cx
cmp ax, 0
jne print_digit_loop
print_loop:
pop dx
add dl, '0'
mov ah, 02h
int 21h
loop print_loop
pop dx
pop bx
pop ax
ret
print_word endp
end main
```
阅读全文