|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
|
![]()
Programming: To C or Not to C Most of the programming that I do at work involves low-cost (less than $5)
8-bit processors. We put out a lot of different projects a year. I would estimate that we did at least 50 projects last year that
involved programming these microcontrollers. One of the most frequently asked questions that I get from vendors or colleagues is
"What language do you program in? C?" I then have to disguise a smirk when I see the shocked look I get when I respond that most
of our projects are programmed in assembly.
The shocked look is because of how many projects that we do at work. We manage several projects at a time, and will do more than
60 projects throughout the year. This can seem daunting since each project will have, on average, 10,000+ lines of code. That's a lot
of eyestrain! However, programming in C isn't always the best solution in the World of Robert. It may not be the best choice for your
world either. This isn't because I have a secret desire to excel as a masochist. It is because of the trade-offs you encounter when
programming in C. Let's take a look at what those trade-offs are.
As you likely know, microprocessors run on machine language. There is some more breaking down on processor function that we can do,
but for this article it will suffice to say that code needs to be expressed in a binary format for the processor to perform it. The C
language isn't the next step up from machine language. There is a step in between called assembly language. It involves some mnemonics
that can vary dramatically from processor to processor. The C language is a higher language that is more universal between processors
and closer to the natural way that we might explain code operation. This C language is converted by a C compiler into the assembly
language for that processor. This conversion has some costs associated with it. We will look at a quick summary of the trade-offs and
then look into a couple of real-life examples.
Using C Language
Advantages:
Disadvantages:
Using Assembly Language
Advantages:
Disadvantages:
To illustrate my point, I thought that I might use the C language as it is compiled in my computer. I have written a small program that doesn't do anything more useful than
illustrate my point.
This program seems fairly simple, and I'm sure that you could code a simple program that would add these numbers without too
much overhead. Let's take a look to see how this code breaks down after being compiled. There are line numbers next to the compiled code and address locations next to the program
assembly code.
This is probably a little more involved than what you had pictured to be necessary to perform two additions. It does do a
great job to illustrate some of the extra overhead in code that is added when you use a C compiler. The reason why we see so much overhead is necessary for your computer to work
with several with virtually any combination of subroutines, interrupts, or set of instructions. You need to make sure that memory is properly allocated and registers are saved
at all times to prevent data corruption. These precautions can build up to be a lot of overhead. I counted at least 20 instructions above that could have been eliminated. That's
a lot of overhead when the entire program is a little more than 30 instructions.
There were some neat advantages to this example though. We were able to write a simple program without having to know anything about the architecture of the processor. We didn't
need to have any knowledge about the eax register, etc.
Small microcontrollers will have the same type of resources that your computer has. The big difference between your microcontroller and your computer is measured in the millions.
It's memory size. You won't always have the luxury of pushing and popping everything every time you make a subroutine call. You won't always have the clock cycles to spare, or have
the ROM space to burn. These restrictions might push you into a bigger and more expensive processor when it really isn't necessary. However, if you decide to use assembly language,
then you will have to handle your own memory management and learn a little more about the processor that you are going to use.
It will take careful consideration of your particular situation to decide which path to choose. The best solution may even be a combination of C language and assembly language.
Don't be afraid of the assembly world. It hasn't gone the way of the dinosaur. Higher languages have their place, and they allow users to create more and more complex programs
and applications. Lower level languages do require a little more work, but also allow great opportunities to stretch resources to a maximum and dramatically increase performance.
Consider the old ways if you need to save a few dollars, or want to create a very high performance application.
|
|||||||||||||||||||||||||||||||||
|
Copyright © 2003 ChipCenter-QuestLink About ChipCenter-Questlink |
||||||||||||||||||||||||||||||||||