8051 Assembly - Bubble Sort

November 15, 2017 | Author: Jayanth Dev | Category: N/A
Share Embed Donate


Short Description

Bubble Sort...

Description

Code Snippets Collection Home

Languages

Popular Snippets

Recent Search

Recent Comment

Exploits Code

Search

8051 Assembly - Bubble Sort ► Assembly

Ads by Google

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

► Sorting

► C++ Code

► Source Code

? ;--------------------------------------------------------------------------------------------------;---------------------------------------------------------------------------------------------------;Program to sort an array of number using the Bubble Sort algorithm in ascending order. ;---------------------------------------------------------------------------------------------------;Input Location: 9000h ;Output Location: 9000h itself ;Number of elementS: 010h ;In the bubble sort algorithm, adjacent elements are compared and sorted with respect to each other. ;This process is done repeatedly starting from the first element everytime till the array is sorted. ;Here's an example. The numbers in the array currently being compared are enclosed in quotes: ;Given array: (5,1,8,2) ;First Pass: Google+ Tools ; ("5,3",8,2) -> ("3,5",8,2) -Swap 5 and 3 since 5 is greater. ; (3,"5,8",2) -> (3,"5,8",2) -5 and 8 are in order so don't swap them. Move on . Make Google+ profile picture ; (3,5,"8,2") -> (3,5,"2,8") -8 is greater - swap it's position with 2. Make Google plus banners for profile ;Second Pass: ; ("3,5",2,8) -> ("3,5",2,8) -3 and 5 are in order. Do nothing and move on. Create and share your Google Plus profile ; (3,"5,2",8) -> (3,"2,5",8) -5 is greater swap 2's and 5's positions. banners. ;Third Pass: ; ("2,3",5,8) -> ("2,3",5,8) -3 is greater so swap. ;This sorts the array as (2,3,5,8) which is in ascending order. Notice that the number of passes is ;one less than the number of elements in the array and number of iterations in each pass is the ;total number of elements minus the current pass number. Louisda16th on July 19, 2008 ;---------------------------------------------------------------------------------------------------;---------------------------------------------------------------------------------------------------You can see how to use quite a few ORG 00h instructions ;-----------------------------------Data Required for the Program----------------------------------- here of the 8051 instruction set. MOV R0, #0Fh ;Counter for LOOP1 ;----------------------------Sort in Ascending Order Using Bubble Sort------------------------------Language LOOP1: ;Outer Loop - Handles number of passes

MOV DPTR, #9000h MOV A, R0 MOV R1, A LOOP2:

Bubble Sort ;Point to beginning of array ;Initialize R1 - the counter for LOOP2 Tags ;to the value of R0 - the number of iterations in each pass i s same ;as the number of elements minus serial number of the current pass. 8051 Assembly Bubble Sort ;Inner Loop - Handles each pass.

MOVX A, @DPTR MOV R2, A INC DPTR MOVX A, @DPTR

;Copy a number of the array to the accumulator ;and store it in R2. ;Move to the net number ;and store that in the accumulator.

SUBB A, R2

;Subtract the first from the second.

JNC Continue2

;If no carry is generated the second is greater and the numbers are ;in order with respect to each other. Otherwise they must be swapped.

SwapNumbers: MOVX A, @DPTR XCH A, R2 MOVX @DPTR, A DEC DPL MOV A, R2 MOVX @DPTR, A

;Move the second number to the accumulator. ;Exchange contents of the accumulator and R2. This makes A contain ;the first number and R2 the second. ;Store the first number at the place where the second one was stored. ;Move to the previous memory location. ;Copy the second number to the accumulator ;and store it in the first number's place.

INC DPTR

;Move to the next memory location.

Continue2: DJNZ R1, LOOP2 Continue1: DJNZ R0, LOOP1

;Move on to the next iteration of the current pass. ;Move on to the next pass.

Here: SJMP Here ;End of program - Loop here indefinitely. END ;---------------------------------------------------------------------------------------------------;----------------------------------------------------------------------------------------------------

Comments

2 comments

1

Leave a message...

Best

Com m unity

Chanan s ingh

Share

a year ago



plz let me know how to mov the array numbers to dptr location ?. 3



p ra s a n n a

Reply





Share ›

13 days ago

program is right but should i get to know that in which manner result will get stored in increasing order or in deacresing order??? 0



Reply

C o m m e n t fe e d



Share ›

Su b s cri b e vi a e m a i l

Copyright 2000-2011 © Code Snippets Collection Tips for Linux

Daily funny commics

Google plus banners snippets

Google plus profile picture

code snippets

HTML5 example

HTML5 examples

Create Your Own Google Plus Profile Banner

HTML5 canvas demo

code

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF