Program 25: Sort the array in ascending order. - EazyNotes

8085 Programs

Program 25: Sort the array in ascending order. Flowchart:

Start

Initialize counter-1.

Initialize counter-2 and H-L pair.

Move the number from memory to accumulator.

Increment H-L pair to point to next number's memory location.

Decrement counter-1.

No

Is

counter-1 = 0?

Yes Stop

Compare number in memory with number in accumulator.

Is

No

A > M?

Yes Interchange the numbers.

Increment H-L pair.

Decrement counter-2.

No

Is

counter-2 = 0?

Yes

Gursharan Singh Tatla

Page 1 of 3

8085 Programs

Program:

Address 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 200A 200B 200C 200D 200E 200F

Mnemonics MVI MVI LXI

MOV INX CMP JC

JZ

2010

MOV

2011 2012 2013 2014 2015 2016 2017 2018 2019 201A 201B 201C 201D

MOV DCX MOV INX DCR JNZ

DCR JNZ

HLT

Operand B, 05H

C, 05H

H, 3000H

A, M H M

2015H

2015H

D, M M, A

H M, D

H C 2007H

B 2002

Opcode 06 05 0E 05 21 00 30 7E 23 BD DA 15 20 CA 15 20

56

77 2B 72 23 0D C2 07 20 05 C2 02 20 76

Remarks Initialize counter-1. Immediate value 05H. Initialize counter-2. Immediate value 05H. Load H-L pair with address 3000H. Lower-order of 3000H. Higher-order of 3000H. Move the number from memory to reg. A. Increment H-L pair. Compare the number with next number. Don't interchange if number < next number. Lower-order of 2015H. Higher-order of 2015H. Don't interchange if number = next number. Lower-order of 2015H. Higher-order of 2015H. Otherwise, swap the numbers. Move next number from memory to D. Move first number from A to memory. Decrement H-L pair. Move next number from D to memory. Increment H-L pair. Decrement counter 2. If counter-2 0, repeat. Lower-order of 2007H. Higher-order of 2007H. Decrement counter-1. If counter-1 0, repeat. Lower-order of 2002H. Higher-order of 2002H. Halt.

Gursharan Singh Tatla

Page 2 of 3

8085 Programs

Explanation: This program sorts an array in ascending order. Let us assume that there are five numbers in the array and its starting address is 3000H. Initially, counter-1 and counter-2 are initialized with the size of the array. H-L pair is pointed to the starting address of the array. In the first iteration, first number is compared with the second number. If first number < second number, then do not interchange them. Otherwise, if first number > second number, then swap them. In the next iteration, first number is compared with the third number. If first number < third number, then do not interchange them. Otherwise, if first number > third number, then swap them. In the next iteration, first number is compared with the fourth number and the process continues until counter-2 becomes zero. When counter-2 becomes zero, counter-1 is decremented and the process continues until all the numbers are arranged in ascending order.

Output:

Before Execution:

3000H:

05H

3001H:

15H

3002H:

01H

3003H:

65H

3004H:

32H

After Execution:

3000H:

01H

3001H:

05H

3002H:

15H

3003H:

32H

3004H:

65H

Gursharan Singh Tatla

Page 3 of 3

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download