Assembly language can implement bubble sorting and other sorting algorithms ? Certainly. , Sorting is not a complicated thing . It's in No 7 week , I'd like to add to that cmp,jxxx,xchg instructions , The next bubble comes out .
It can be compared with the attached C Language sequencing for understanding .
assume cs:cseg, ds:dseg, ss:sseg sseg segment stack dw 10H dup (?) sseg ends
dseg segment dw123,5,9,11,33,6,9,76,100,3 dseg ends cseg segment start: mov ax,
dseg mov ds, ax mov cx,9 ; Outer circulation 9 Trip sort: push cx ; Save outer loop times ; Note that the outer layer 1 Subcycle , Internal circulation execution 9 second , Outer layer
2 Subcycle , Internal circulation execution 8 second ,... Control of external circulation cx The value is exactly the number of inner layer cycles mov si, 0 ; Equivalent to inner circulation j=0 inner: mov ax, [si]
;(ax) Namely a[j] cmp ax, [si+2] ;a[j] And a[j+1] compare jbe noswap ;a[j]<=a[j+1] Time does not exchange , Equivalent to a[j]>a
[j+1] Time exchange xchg ax, [si+2] ; exchange mov [si], ax ; The end result is a[j] And a[j+1] In exchange noswap: add si, 2
; It's going to the next number , amount to j++ loop inner ; inner loop pop cx ; Restore the cx loopsort ; Outer circulation mov ax, 4
c00h int21h cseg ends end start
The assembler above , Corresponding to the following C Language program . Can read in contrast ：
#include <stdio.h> #define n 10 int main( ) { int i,j,t,a[n]= {123,5,9,11,33,6,
9,76,100,3}; for (i=0; i<n-1; i++) for(j=0; j<n-i-1; j++) if (a[j]<a[j+1]) {
t=a[j]; a[j]=a[j+1]; a[j+1]=t; } for(i=0;i<n;i++) printf("%d ",a[i]); return 0;
}

Technology
Daily Recommendation
views 5
views 2