4 Replies - 25306 Views - Last Post: 01 August 2010 - 09:58 AM

#1 Louisda16th   User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

8051 Assembly - Bubble Sort

Posted 17 July 2008 - 08:45 AM

Description: You can try this program out in a simulator such as Keil uVision3 or any other 8051 simulator.You can see how to use quite a few instructions here of the 8051 instruction set.
;----------------------------------------------------------------------------------------------------
;----------------------------------------------------------------------------------------------------
;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:
;			("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.
;			(3,5,"8,2") -> (3,5,"2,8")	-8 is greater - swap it's position with 2.
;Second Pass:
;			("3,5",2,8) -> ("3,5",2,8)	-3 and 5 are in order. Do nothing and move on.
;			(3,"5,2",8) -> (3,"2,5",8)	-5 is greater swap 2's and 5's positions.
;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.
;----------------------------------------------------------------------------------------------------
;----------------------------------------------------------------------------------------------------

ORG 00h
;-----------------------------------Data Required for the Program------------------------------------
MOV R0, #0Fh					;Counter for LOOP1
;----------------------------Sort in Ascending Order Using Bubble Sort-------------------------------
LOOP1:							;Outer Loop - Handles number of passes

	MOV DPTR, #9000h			;Point to beginning of array
	MOV A, R0					;Initialize R1 - the counter for LOOP2
	MOV R1, A					;to the value of R0 - the number of iterations in each pass is same
								;as the number of elements minus serial number of the current pass.

	LOOP2:						;Inner Loop - Handles each pass.

		MOVX A, @DPTR			;Copy a number of the array to the accumulator
		MOV R2, A				;and store it in R2.
		INC DPTR				;Move to the net number
		MOVX A, @DPTR			;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		;Move the second number to the accumulator.
			XCH A, R2			;Exchange contents of the accumulator and R2. This makes A contain
								;the first number and R2 the second.
			MOVX @DPTR, A		;Store the first number at the place where the second one was stored.
			DEC DPL				;Move to the previous memory location.
			MOV A, R2			;Copy the second number to the accumulator
			MOVX @DPTR, A		;and store it in the first number's place.

			INC DPTR			;Move to the next memory location.

	Continue2: DJNZ R1, LOOP2	;Move on to the next iteration of the current pass.

Continue1: DJNZ R0, LOOP1		;Move on to the next pass.

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


Is This A Good Question/Topic? 0
  • +

Replies To: 8051 Assembly - Bubble Sort

#2 hari8715   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 27-March 10

Re: 8051 Assembly - Bubble Sort

Posted 27 March 2010 - 05:44 AM

the programme is very easy to understand,it was written in a simple form but some print mistakes are present in this programme please check it out and correct it,thank you.
Was This Post Helpful? 0
  • +
  • -

#3 Louisda16th   User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: 8051 Assembly - Bubble Sort

Posted 27 March 2010 - 06:44 AM

What mistakes??
Was This Post Helpful? 0
  • +
  • -

#4 dhatu.priya   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 30-May 10

Re: 8051 Assembly - Bubble Sort

Posted 30 May 2010 - 07:23 AM

thank yo so much is an amazing explanation...i under stood it vry wel.....
Was This Post Helpful? 0
  • +
  • -

#5 sezeala   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 01-August 10

Re: 8051 Assembly - Bubble Sort

Posted 01 August 2010 - 09:58 AM

i need a program that checks the number stored in an internal ram , and buzzes when a negative number is detected
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1