You are on page 1of 3

Exp 3) ascending/ Descending order programs

Ascending order program:

For this program, we need two registers as counters. In order to arrange 10 numbers in
ascending order we need count value 10 x 10 ie 100. Similarly for n numbers count required will
be n x n= n^2. Reason for that is, in one cycle only two adjacent numbers are compared. After
comparison larger number is stored at higher memory location while small number is stored at
lower memory location. Thus arranging adjacent numbers in ascending order. But our goal is to
arrange entire block in ascending order so in order to compare every number with another it
requires n x n cycles or comparison loops.
PROGRAM

In above program a block of numbers is stored at location 20H-29H. First number is


taken into Accumulator. Then the number is moved to register R1, this is done to fetch next
number into accumulator. Second number is fetched into accumulator. Subtraction is carried out
between accumulator and contents of R1 (ie. first number). If first number is large it is stored in
second location while the smaller number is transferred at first location.

After the above process completes, one cycle is completed. In second cycle, number at
second memory location and at third memory location is compared and action is taken. In third
cycle number at third memory location and fourth are compared and action takes place. Similarly
in further cycles the adjacent numbers are compared and action takes place.

Completion of 10 such cycles completes one batch. 10 such batches will be executed by
the program. In case smallest number is stored at 10th (last) memory location. It will take 10
such batches for it to reach first memory location. So execution of 10 batch cycles is essential.
8051 Program to arrange numbers in Descending Order:
For this program, we need two registers as counters. In order to arrange 10 numbers in
descending order we need count value 10 x 10 ie 100. Similarly for n numbers count required
will be n x n= n^2. Reason for that is, in one cycle only two adjacent numbers are compared.
After comparison larger number is stored at higher memory location while small number is
stored at lower memory location. Thus arranging adjacent numbers in descending order. But our
goal is to arrange entire block in descending order so in order to compare every number with
another it requires n x n cycles or comparison loops.

PROGRAM:

In above program a block of numbers is stored at location 20H-29H. First number is taken into
Accumulator. Then the number is moved to register R1, this is done to fetch next number into
accumulator. Second number is fetched into accumulator. Comparison is carried out between
accumulator and contents of R1 (ie. first number). If first number is small it is stored in second
location while the larger number is transferred at first location.

After the above process completes, one cycle is completed. In second cycle, number at second
memory location and at third memory location are compared and action is taken. In third cycle
number at third memory location and fourth are compared and action takes place. Similarly in
further cycles the adjacent numbers are compared and action takes place.

Completion of 10 such cycles completes one batch. 10 such batches will be executed by the
program. In case largest number is stored at 10th (last) memory location. It will take 10 such
batches for it to reach first memory location. So execution of 10 batch cycles is essential.
Exp 1. Finding number of 1’s and number of 0’s in a given 8-bit number
Program

Result:

Input: 97h Output: r1=3(one’s) r2=5(zero’s)

Exp.2) Write a program to find average of n-numbers(8-bit)


Program

Result:

Input: enter the values in 40h location

Output: a=6 & it may be displayed on 55h memory location

You might also like