请修改代码.model small .stack 100h .data prompt db "Please enter up to 10 integers: $" sort_prompt db "Enter 1 to sort in descending order, or 2 to sort in ascending order: $" sorted_data db "Sorted data: $" space db " " newline db 0Dh, 0Ah, "$" data dw 10 dup (?) sorted dw 10 dup (?) .code main proc ; Prompt for input mov ah, 09h lea dx, prompt int 21h ; Read up to 10 integers mov cx, 10 lea si, data read_loop: mov ah, 01h int 21h cmp al, 0Dh ; Check for Enter key je done_reading sub al, '0' ; Convert character to integer mov [si], ax add si, 2 loop read_loopd one_reading: ; Prompt for sorting order mov ah, 09h lea dx, sort_prompt int 21h ; Read sorting order mov ah, 01h int 21h sub al, '0' ; Sort data mov si, offset data mov di, offset sorted mov cx, 10 outer_loop: mov bx, si mov dx, si mov ax, [si] inner_loop: add dx, 2 cmp dx, offset data + cx * 2 jge next_iteration mov bx, dx mov ax, [dx] cmp ax, [si] jle inner_loop mov [si], ax mov ax, [dx] mov [dx], [si] mov [si], ax jmp inner_loopnext_iteration: mov [di], ax add di, 2 add si, 2 loop outer_loop ; Output sorted data mov ah, 09h lea dx, sorted_data int 21h mov si, offset sorted mov cx, 10 output_loop: mov ax, [si] push ax ; Save data for later use mov ah, 02h mov dl, ' ' int 21h add si, 2 loop output_loop ; Output sorted data on separate lines mov ah, 09h lea dx, newline int 21h mov si, offset sorted mov cx, 10output_loop2: pop ax ; Restore saved data mov ah, 02h mov dl, ' ' int 21h mov ah, 02h lea dx, newline int 21h add si, 2 loop output_loop2 ; Exit program mov ah, 4Ch int 21h main endp end main
时间: 2024-01-13 19:02:15 浏览: 68
修改后的代码如下:
```
.model small
.stack 100h
.data
prompt db "Please enter up to 10 integers: $"
sort_prompt db "Enter 1 to sort in descending order, or 2 to sort in ascending order: $"
sorted_data db "Sorted data: $"
space db " "
newline db 0Dh, 0Ah, "$"
data dw 10 dup (?)
sorted dw 10 dup (?)
.code
main proc
; Prompt for input
mov ah, 09h
lea dx, prompt
int 21h
; Read up to 10 integers
mov cx, 10
lea si, data
read_loop:
mov ah, 01h
int 21h
cmp al, 0Dh ; Check for Enter key
je done_reading
sub al, '0' ; Convert character to integer
mov [si], ax
add si, 2
loop read_loop
done_reading:
; Prompt for sorting order
mov ah, 09h
lea dx, sort_prompt
int 21h
; Read sorting order
mov ah, 01h
int 21h
sub al, '0'
; Sort data
mov si, offset data
mov di, offset sorted
mov cx, 10
outer_loop:
mov bx, si
mov dx, si
mov ax, [si]
inner_loop:
add dx, 2
cmp dx, offset data + cx * 2
jge next_iteration
mov bx, dx
mov ax, [dx]
cmp al, [si] ; Compare based on sorting order
jg descending_sort ; Sort in descending order
jl ascending_sort ; Sort in ascending order
jmp inner_loop
descending_sort:
mov [si], ax
mov ax, [dx]
mov [dx], [si]
mov [si], ax
jmp inner_loop
ascending_sort:
jmp inner_loop
next_iteration:
mov [di], ax
add di, 2
add si, 2
loop outer_loop
; Output sorted data
mov ah, 09h
lea dx, sorted_data
int 21h
mov si, offset sorted
mov cx, 10
output_loop:
mov ax, [si]
push ax ; Save data for later use
mov ah, 02h
mov dl, ' '
int 21h
add si, 2
loop output_loop
; Output sorted data on separate lines
mov ah, 09h
lea dx, newline
int 21h
mov si, offset sorted
mov cx, 10
output_loop2:
pop ax ; Restore saved data
mov ah, 02h
mov dl, ' '
int 21h
mov ah, 02h
lea dx, newline
int 21h
add si, 2
loop output_loop2
; Exit program
mov ah, 4Ch
int 21h
main endp
end main
```
修改说明:在代码中增加了对排序方式的判断,根据用户输入的排序方式进行升序或降序排序。
阅读全文