|
3
Gordon Drive, P.O.Box 1347 Rockland, Maine 04841 U.S.A.
|
|
© 2004 Avocet Systems, Inc.
|
Call
Us Today at 207-596-0080
|
|
Avocet Systems, Inc. : The Complete Solution for Embedded Systems
Development Tools
|
|
|
Tech
Tips
Getting a Leg Up on Legacy Code
With all the tools available today for the quick development of embedded C code,
you're sure to be stuck with hundreds of thousands of lines of working, tested
code written in assembly. Each required change that comes down the pike seems
easy enough, but adds to the problem that you know you have to face someday.
What you need is a fast and easy way to start to make changes in C without throwing
out all of that reliable and fast assembly code. Well, as always, Avocet has
the answer, and the first step is easier than you think! Soon you'll have more
control over the structure of your code and any subsequent changes that are
needed.
Avocet compilers are designed to be compatible with the assembly code you've
already written with our assemblers. As you begin your work in C you'll notice
that the compiler generates such tight code that you may want to convert more
of it than you originally thought.
The first step, as I said, is an easy one. Identify your background loop and
move it into function main(), and move your initialization code to function
init(). The result should look like:
void init()
{
#asm code for initialization
#endasm
}
void main()
{
init();
while(True)
{
#asm code for background
loop
#endasm
}
}
#asm
the rest of your code
#endasm
This should take only a few minutes. Even if your code is several modules, you
need only start with your main module. Though a little code will be added at
initialization, your program should operate exactly as through the peephole
optimizer. Now you're ready and new changes may now be made in C.
At this point it's time to analyze. What parts of your program are changed most
often? Which sections would most benefit from high-level debugging? Which sections
are the hardest ones to document? These are the sections that you'll want to
tackle first. Break them out into blocks that can be called like functions.
The data that a function uses should be broken up into three categories: global
variables, local variables, and parameters.
Take the global variables from the section of code you want to start with and
declare them in C. Now instead of just DS, you have int, char, long, float,
arrays, unsigneds, and structures. They'll still be able to be used in assembly
by adding an underscore to the beginning of the label.
All variables (except large structures) that are only used within a function,
should be declared locally. Minimize the number of contacts between this function
and the outside world, and declare them as parameters. It's a good idea for
large structures to be left as global as they might otherwise create unnecessary
overhead, and you'll probably be better off using pointers anyway.
If you see pieces that you wish to leave in assembly but call as functions from
C, start by sketching them out in C. Now compile those functions to assembly
code and use them as a base from which to work. This will automatically show
you how to take advantage of C parameters. You may be surprised to find that
the resulting code generated is compact enough that you'll want to leave it
in C. The point here is that, wherever possible, let the compiler do the work
for you.
Finally, with an Avocet compiler, you may be able to convert those sections
of code you thought HAD to stay in assembly. if you have memory mapped peripherals,
use the following construct:
struct P_struct in_port[8] @ 0x2330
This allows you to determine an absolute location for high-level C types. This
becomes even more powerful when used on structures. You can lay out a complete
8 channel UART with an array of structures including bit-fields and assign it
at the base address for the chip. This will make your code MUCH easier to read
and maintain, and it will run just as fast.
Keep in mind that by using this technique to convert your assembly code you
don't have to do all of the work at once. For just a small initial investment,
you can start to experience the advantages of high-level code. As you begin
to convert more of your code and make changes in C, you'll be sure to see the
benefits. Then, when you upgrade your processor, most of your work will already
be done.
|
|
|