SoFunction
Updated on 2025-03-04

Implementation of assembler score sorting

Assembler Implementation of Sorting

【Task】
In the data area, the number of students and the grades of the assembly language course are given. Please sort the grades and save them in the original data area.
Some codes are given below, requiring the sorting subroutine to be completed.
The data segments given in the program include the grades of 20 students, and the data of each student accounts for 1 byte.

Reference program block:

assume cs:cseg, ds:dseg, ss:sseg
sseg segment stack
  dw 100H dup (?)
sseg ends
dseg segment
  db 20
  db 98,61,57,82,89,73,61,58,53,54
  db 84,78,70,64,84,63,76,84,83,86
dseg ends
cseg segment
 start: mov ax, dseg
     mov ds, ax
     mov cl, ds:[0]
     mov ch, 0   ;cxStore the number of numbers to be sorted
     mov bx, 1   ;The starting offset address of the number to be sorted
     call sort

     mov ax, 4c00h
     int  21h
;Subprogram name:sort
;achievement  able:Right from(DS):(bx)Beginning(cx)Byte sort
;Entry parameters:(DS):(BX)The starting address for saving data
;    (cx)The number of data to be sorted
;Export parameters:none
sort proc
   ;Write a subroutine here
sort endp

cseg ends
end start

【Reference answer】

assume cs:cseg, ds:dseg, ss:sseg
sseg segment stack
  dw 100H dup (?)
sseg ends
dseg segment
  db 20
  db 98,61,57,82,89,73,61,58,53,54
  db 84,78,70,64,84,63,76,84,83,86
dseg ends
cseg segment
 start: mov ax, dseg
     mov ds, ax
     mov cl, ds:[0]
   mov ch, 0   ;cxStore the number of numbers to be sorted
   mov bx, 1   ;The starting offset address of the number to be sorted
   call sort

     mov ax, 4c00h
     int  21h

sort proc
     push si
     push ax
     dec cx   ;The number of outer loops is the number of data1
 c1:        ;Sort outer loop
     push cx   
     mov si, bx
 c2:        ;Sort inner loop
     mov al, [si]
     cmp al, [si+1]
     jbe noswap
     mov ah, [si+1]
   mov [si+1], al 
     mov [si], ah 
 noswap: inc si 
     loop c2 
     pop cx
     loop c1

   pop ax
   pop si
   ret
sort endp

cseg ends
end start

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.