EmbeddedRelated.com
Forums

need help with a small number sorting program please!!

Started by knad...@gmail.com March 6, 2008
Hi,

We have some practice exercies in our course, I was able to do several of them using ASMIDE (the 68hc12 assembler) however there's this one exercise which has me completely stumped.

Basically here it is:

There are 10 unsigned 8 bit numbers stored from $0900 onwards. Write a program which finds the largest and smallest numbers and store them at $090A and $090B respectively.

ORG $0900
number FCB $22,$99,$44,$33,$66,$55,$88,$77,$FF,$11
largest RMB 1
smallest RMB 1

Any ideas? I tried thinking of using MINA and MAXA, but that only compares the accumulator A with that of the memory, only to. I believe a loop is needed but I don't know how to implement it.

I know how the program should be written. 22 should be stored in the accumulator, and a loop is used to check 99,44 and etc if 22 is greater than the numbers listed in the memory, if one is larger than 22 then that number takes 22's place until theres no more higher number. For the smallest it's the same principle but it will continue until the smallest is found.

I really need help with this please, the tutorials/reference manuals which I have supplied by my tutor are useful but none of them explain loops properly.

Thanks!!
>There are 10 unsigned 8 bit numbers stored from $0900 onwards. Write
>a program which finds the largest and smallest numbers and store
>them at $090A and $090B respectively.
>
>Any ideas?

I'm not going to give you an answer, but I will write a little bit of
a story instead...

So, a guy walks into an interview for a firmware developer job. A
part of the interview is to write a program to scan through a list of
10 unsigned 8 bit numbers and find the largest and the smallest. The
applicant immediately turns to the internet to ask others how to
answer the problem.

1) the applicant shows ingenuity for having a set of cohorts that
will help him in his career without paying them compensation.

2) the applicant should have listened when he was in class.

Personally, I worked summer jobs and marked papers to get the money
to go to school to answer questions like this. I passed that course.
Ahh also yes we did have our 1st lecture, however that was on 2's
complement, BCD etc which I already know how to do.
Yes I see where your coming from but the thing is that we just started
this stuff like last week when semester started, also I don't see my
tutor until mid next week. This is the last exercise which I'm eager
to finish off by then to work on other stuff... So if you know how to
do it I'm *only* after a hint in the right direction...

thanks.
Hi

This is not an exact answer for your question, but below are some functions
written in assembly language (in fact C functions wit assembly part), which
do something... And which run.

May be there is better, because I don't know assembly language of HC12 very
well, and I write 99.99% of my soft in C language. But sometimes, assembly
is quicker than what compiler do.

So, keep in your mind I'm not a specialist of assembly. I wrote some
functions, and after debug, I saw they are OK, that's all.

Hope this helps you... Good luck... And go to C language ! It's so easy !

Joel

//************************************************************************
// Simple copy of nb_octets from a source array to a destination array
// C function, with assembly part
void CAL_CopierTab (char *dest, const char *srce, char nb_octets)
{ asm (
"ldab %nb_octets\n" // b <-- nb of bytes
"pshx \n" // save X
"pshy \n" // save Y
"ldy %dest \n" // Y points on destination
"ldx %srce \n" // X points on source

"CopieTab1: \n"
"ldaa 0,x \n" // A <-- *X
"staa 0,y \n" // *Y <-- A
"inx \n"
"iny \n"
"dbne b,CopieTab1\n" // done ? sort

"puly \n"
"pulx \n"
);
}

"pshx", "pshy" and "puly", "pulx" are because of call structure of a C
function... So, maybe you can omit them.
So, I can suppose in assembly language for a 10 bytes array :
ldab #10 ; B register contains number of
bytes
ldy #dest_address ; Y <- destination address
ldx #srce_address ; X <- source address

CopieTab1:
ldaa 0, X ; A <-- content of the address
pointed by X
staa 0, Y ; A --> address pointed by Y
inx ; next source address
iny ; next destination address
dbne b, CopieTab1 ; decrement counter B and branch if not zero

As you can see, I use accu B for the counter, X index for source, Y for
destination. Accu A is used as accu from source to destination :
A <- source
A -> destination

So, you can use accu A to store the min number, for example, in ordre to
search for the min :
ldaa #$ff ; A <- the max possible value
loop:
cmpa 0, x ; compare A avec byte pointed by (X
+ 0)
bcs next ; branch if Carry clear
ldaa 0, x ; a <- byte pointed by (X + 0)
next:
dbne loop

Be careful, I'm not sure for "cmpa 0, x", but I suppose it's OK when I read
data sheets.
Data sheet says "cmpa" do (A) - (M), and move flags NZVC.
So, I think if Carry is set, it's because (M) is greater than (A).
So, if carry set, I've nothing to do, else I've to change A value.

In C language, "on the fly" (I don't have time to check) :

#define Byte unsigned char // replace "Byte" by
"unsigned char"

// C function which returns the min value of an array
Byte LookForMini (Byte *array, Byte nb_elem)
{ Byte mini;

mini = 255; // the maxi value possible
while (nb_elem)
{ if (*array < mini) mini = *array;
array++; // next element of the array
nb_elem--; // decrement counter of elements
}
return mini;
}

And you call this function as follow :
void main (void)
{ Byte value; // the value you need
Byte array[10];

// Init the array...

// Find for the mini value
// "sizeof (array)" : the compiler will calculate for you
// the number of bytes of the array, so, if you change
// it, you will not have to change each call
value = LookForMini (array, sizeof (array)/sizeof (Byte));
// some things to do...

// Find for the maxi value
value = LookForMaxi (array, sizeof (array));
// some things to do...
}
Some comments :
"sizeof" asks the compiler to calculate the number of bytes of something.
Example : "sizeof (long)" will return 4, if a long use 4 bytes with this
compiler.
So, "sizeof (array)" calculates the number of bytes of that array
divided by "sizeof (Byte) calculates the number of ELEMENTS of that array.
I know, it's a little bit heavy ?

But this system is good for each type you can find, define, use... and make
easier update of a program. For example, on a big program, you just change
the number of elements of the array, and you don't have to look for each
time you use that array, and modify, without any error, etc...

But I'm not here to give you a course about C Language.
Just know C is a very very good language !

//************************************************************************
// Below an other example which rotate left an array of bytes
// Shift left of an array of "nb_elem"
void CAL_DecalerAigus1 (char *tab, char nb_elem)
{
asm (
"ldab %nb_elem \n" // b <-- nb d'ents
"pshx \n" // sauve X
"ldx %tab \n" // X pointe sur le tableau
"clra \n" // efface A et flags

"Aigus1: \n"
"rol 0,x \n" // dalage gauche de
l'octet pointpar X
"inx \n"
"dbne b,Aigus1 \n" // fini ? sort
"pulx \n"
);
}
> -----Message d'origine-----
> De: 6... [mailto:6...] De la part de
> knadoor
> Envoy jeudi 6 mars 2008 16:48
> : 6...
> Objet: [68HC12] Re: need help with a small number sorting program
> please!!
>
> Yes I see where your coming from but the thing is that we just started
> this stuff like last week when semester started, also I don't see my
> tutor until mid next week. This is the last exercise which I'm eager
> to finish off by then to work on other stuff... So if you know how to
> do it I'm *only* after a hint in the right direction...
>
> thanks.
>
>
>
>
>
In a classical search algorithm you would assume that the first
element is both the largest and the smallest value (store them away
in their appropriate spots).

Is the second element smaller than the smallest? Yes - store it as
the smallest.
Is the second element larger than the largest? Yes - store it as the largest.

Is the third element smaller than the smallest? Yes - store it as the smallest.
Is the third element larger than the largest? Yes - store it as the largest.

...

Is the last element smaller than the smallest? Yes - store it as the smallest.
Is the last element larger than the largest? Yes - store it as the largest.

done. The largest is left in its final resting place. The smallest is
left in its final resting place.

As for the how to...look for a compare instruction and a branch less
than and branch greater than instruction.

Andrei
thanks.