Bubble Sort of Assembly Sorting Knowledge

Hits: 0

Here is a brief introduction to how bubble sorting is performed in assembly. First, review the knowledge and algorithms of bubble sorting in C language, and then explain the process of assembly implementation, which better consolidates and links the idea of ​​programming.

1. C language – bubble sort

The basic concept of bubble sort: compare two adjacent numbers in turn, put the decimal in the front and the large number in the back. Each time the search is completed, a maximum number is found until the sorting ends.

It can be seen from the above table that each round of sorting can determine a maximum number until the sorting ends. The corresponding C language code is as follows: The j parameter is set to Ni-1, which simplifies the sorting complexity, and can also be N-1.

2. Assembly – bubble sort

The corresponding assembly bubble sort is shown in the following figure:

    A          DW 0,15,70,30,32,89,12,12,34,4,38,23,45,13,43,24,54,24,54,1 
    Count       EQU ($-A) /2 ; the number of elements in the array (bytes) 

    MOV  DS,AX 
;----------------------------------------- -------------------------------- 
;Bubble sort core algorithm  A sort    
    MOV  CX,Count-1 ;outer loop Execute n-1 times 
I10 :
    XOR  SI,SI ;XOR clear 
    XOR  DI,DI        

I20 :
    MOV AX,A[SI]
    MOV BX,A[SI+2]

    ;Compare AX and BX size : AX<BX (less than transfer) jump to I30, otherwise swap two numbers 
    JL   I30 
    MOV  A[SI],BX 
    MOV  A[SI+2],AX ;Swap positions

    ADD  SI,2 ;SI plus 2: move one digital position 
    INC  DI ;DI plus 1 
    CMP  DI,CX           

    ;Compare the size of CX and DI : DI<CX transfer, CX is the total number of outer loops n-1 
    JB   I20 
    Loop  I10 ;The loop is adjusted to I10, the Loop loop CX is executed once and minus 1    
;------------ -------------------------------------------------- ------------   
    MOV  AH,4CH 
    INT  21H 

First define an array A in the data segment and store 20 numbers, and Count=($-A)/2 store the number of elements in the array, where $ represents the current position, that is, the last number in array A minus the position The starting position of A, since A is DW (2 bytes), divide it by 2 and convert it to the number of arrays: 20.
Compare the two adjacent numbers in A in turn, and store them to AX=A[SI] and BX=A In [SI+2], compare as immediate exchange, otherwise jump to SI plus 2, and continue to compare the next two adjacent numbers.
At the same time, the number of outer loops Count-1 is recorded in CX. Using the Loop loop, the default CX register of the Loop will decrease by 1 in each loop until it is 0, and the loop stops. The number of sorted records is recorded in DI, and 1 is added each time to compare with CX until the sorting ends.
The output is as shown in the figure below, and the output function is omitted here. I just want to talk about the process of bubble sort, sorry! ! !

I hope it can help you better understand the connection and idea of ​​bubble sort in C language and assembly, thank you! I hope readers understand the shortcomings. By: Eastmount

Leave a Reply

Your email address will not be published.