使用8086汇编写一个成绩系统,包含排序功能
时间: 2024-01-25 09:02:34 浏览: 26
好的,以下是一个使用8086汇编语言编写的成绩系统,包含排序功能的示例代码:
```
; 8086 Assembly Language Code - Grade System with Sorting Functionality
data segment
grades db 85, 72, 93, 67, 88, 75, 98, 62, 77, 84
size equ 10
data ends
code segment
start:
mov ax, data
mov ds, ax
; display unsorted grades
call display
; sort the grades in ascending order
call sort
; display sorted grades
call display
mov ah, 4ch
int 21h
display proc
; display grades
mov cx, size
mov si, offset grades
display_loop:
mov dl, [si]
add dl, 30h ; convert grade to ASCII
mov ah, 02h
int 21h
inc si
loop display_loop
; add a newline character
mov dl, 0Ah
mov ah, 02h
int 21h
ret
display endp
sort proc
; sort grades in ascending order using bubble sort
mov cx, size
outer_loop:
dec cx
mov bx, cx
mov si, offset grades
inner_loop:
cmp bx, 0
jle exit_inner_loop
mov al, [si]
cmp al, [si-1]
jge exit_inner_loop
xchg al, [si-1]
mov [si], al
dec si
dec bx
jmp inner_loop
exit_inner_loop:
loop outer_loop
ret
sort endp
code ends
end start
```
在这个程序中,我们使用了一个名为 "grades" 的数据段,其中包含了10个学生的成绩。我们使用 "size" 变量来存储成绩的总数。在 "start" 标签处,我们将数据段的地址存储在AX中,然后将DS寄存器设置为AX,以便程序可以访问数据段中的内容。
接下来,我们调用了 "display" 过程来显示未排序的成绩。这个过程使用 "MOV" 和 "INT" 指令来显示每个成绩,并在最后添加一个换行符。
然后,我们调用了 "sort" 过程来对成绩进行排序。这个过程使用冒泡排序算法来将成绩按升序排序。
最后,我们再次调用 "display" 过程来显示排序后的成绩。程序结束时,我们使用 "INT 21h" 中断指令来终止程序。
希望这个示例代码能够帮助你理解如何使用8086汇编语言编写成绩系统,并包含排序功能。