assembly language implementation of small-to-large bubble sort

Source: Internet
Author: User

Compilation implements a small-to-large bubble sort

The main function is implemented by C, and the sort function is written in assembly language.

#include <stdio.h>


int buffer[256]; Data buffers
int bufferlen=0; Data buffer Data count


extern sort (int *buffer,int bufferlen); Assembler function Sort Interface


Input data from the keyboard ********
int inputdatafromkeyboard (int *pbuffer)
{
int ncnt=0;
int x;

printf ("Data entry start: \ n");
while (1)
{
scanf ("%d", &x); Keyboard input integer number ==〉x
Pbuffer[ncnt++]=x; X buffer
if ((x== (int)-1) | |  (ncnt>=256)) Input data is-1 end input
{
ncnt--;
Break
}
}
printf ("Data entry End!\n");
return ncnt;
}


Output buffer data to the console ********
void Outputconsole ()
{
int i;
printf ("Output buffer data: \ n"); outputting data to the console
for (i=0;i<bufferlen;i++)
{printf ("%4d", Buffer[i]);}
printf ("\ n");


}






Main function ********
int main ()
{


Bufferlen=inputdatafromkeyboard ((int *) buffer); Data entry from the keyboard
Outputconsole (); Output input data to the console
Sort ((int *) buffer,bufferlen); Sort the input data
Outputconsole (); Output Sort Data
return 0;
}

The assembly functions are as follows:

Export sort; derive function sort
Area Exp103,code,readonly

CODE32




Sort; function name
; entry Parameters
; R0<==buffer Data Buffer Header address
; R1<==bufferlen Data Buffer Data count


SUBS R1,r1, #1; first loop number R1

L0 MOV R2, #0
MOV r3,r1; second-level cycle times R3
CMP R1, #4
Addne r0,r0, #4; except for the first time, to make the R0 shift, multiplication, especially the one with the left shift.


L1 LDR R6,[R0,R2]; R6&LT;=[R0+R2]
ADD R4,R2, #4
LDR R7,[R0,R4]; R7&LT;=[R0+R2+1]
CMP R6,R7
BLT L2


STR R7,[R0,R2]; R6&LT;R7 Exchange
STR R6,[R0,R4]


L2 ADD R2,R2, #4
SUBS R3,r3, #1; two-layer cyclic control
BNE L1

SUBS R1,r1, #1; one-layer loop control
BNE L0
EXIT MOV PC,LR; function return


END

Running an instance

Data entry Start: 4 2 6 8 7-1

Data entry is over!
Output Buffer data:
4 2 6) 8 7
Output Buffer data:
2 4 6) 7 8


assembly language implementation of small-to-large bubble sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.