A language like C++ is nice because you can do object oriented programming which automatically means better code management.
C++ is a badly designed (small) extension to C. C also has many design flaws mainly because the language was designed by compiler writers (very experiensed asm coders).
Every good C programmer knows roughly what their compiler produces when they write a line of C-code and they know how to avoid major performance issues.
For a good example, say you have to measure current and a resistor value of 10 ohms isnt too bad of a choice based on the current level. You can stick a 10 ohm 1/2 watt resistor in there and it works fine because you measure the voltage across the resistor with the AD converter on the uC chip, and do the division V/10 and you get your answer to what level the current flow is at the time. Done? Well not really. Knowing Asm you know that you can divide by 8 much faster than you can divide by 10 because dividing by 10 requires a more complicated algorithm whether you write it yourself or not. So knowing a little bit about Asm means you'll probably pick 8 ohms instead of 10 ohms and this allows division by 8 with three register shifts (right shifts) which is very very fast compared to having to do any real division.
That kind of thing is not learned because of using asm or any other language.. You learn that by knowing your processor, and C language does not hide any hardware.
I always divide by eight like this:
adc_value >>= 3;
If I need to write 0xFF to memory location 0x45, I can say;
*((char*)0x45) = 0xFF;
Ugly, yes, but very common thing with microcontroller. I rarely see C programmers that know how to address direct memory locations, or know what the above code does.
And with C i can write something like this and get more efficient code than most asm coders do:
int sum = 0;
for(i=0;i++;i<100)
{
sum += value_table;
}
If I have enought memory (as I usually do with modern microcontrollers), I can tell my compiler to unroll the loop.. who asm compiler bothers to do that kind of optimization (by hand)?
Last edited: