Society of Robots - Robot Forum

Software => Software => Topic started by: stopgo on September 20, 2007, 10:22:38 PM

Title: C vs. ASM
Post by: stopgo on September 20, 2007, 10:22:38 PM
If I really want to grok robotics and create the best machines, should I code in ASM or C?  Does the answer vary from situation to situation?
Title: Re: C vs. ASM
Post by: JesseWelling on September 20, 2007, 11:33:52 PM
C is close to the hardware when it you need it to be, yet you can work at pretty high level if you need to. You just generally need to make the hierarchy yourself...

So I don't think you are going to learn much from assembly programing that you wouldn't learn from programing in C. Except to be more appreciative of compiler writers.... :P
Title: Re: C vs. ASM
Post by: snow on September 21, 2007, 02:29:11 AM
C is good for this microcontrollers that we have today - a lot of flash, couple of hardware timers etc.

You use ASM if you really want to squezze most of you microcontroller or if you do some time criticall stuff... and it is sometimes good for debugging. But ASM is different for different MCU, whereas C is basicly the same everywhere. And sometimes you will swear about simple stuff like comparing two values and doing something based on that comparison in ASM... which is a piece of cake in C.


So my advice is to learn C and spend other time on learining about electronics, motors, sensors, etc.
Title: Re: C vs. ASM
Post by: stopgo on September 21, 2007, 03:15:19 AM
Thanks for the responses!  I'm fluent in C.  My ASM skill is basically read-only, for the moment.  Having written a compiler, I can appreciate JesseWelling's point - I'll probably get equal- or better-quality machine code out of a compiler than what I can write myself in 5-10x the time.

I guess I was thinking that making myself code in ASM would really drive the nature of my mcu into me.  But from what I've seen so far, JesseWelling sounds right:  the C code actually looks like it can get pretty close to the ASM anyway.  And so snow's probably right - my time would be much better spent working on other things.  The issue looks settled.  Thanks guys!

Now a related question: anyone know how good gcc-avr's optimizations are, relative to those of other AVR compilers?
Title: Re: C vs. ASM
Post by: JesseWelling on September 21, 2007, 06:24:39 AM
I haven't done any benchmarking but I can tell you that the -Os optimization really works... I can go from 49k with an -O3 optimization to a 36k with -s on one of my projects. As for speed... dunno... but I think gcc uses intermediate code between the C and the resulting executable, and runs optimizations on that, I think it should be on par with regular gcc.
Title: Re: C vs. ASM
Post by: rgcustodio on September 22, 2007, 10:11:32 AM
@stopgo:
Quote
Now a related question: anyone know how good gcc-avr's optimizations are, relative to those of other AVR compilers?
IAR's compilers might beat avr-gcc.

IAR's compiler is targeted and generates code specially optimized for the AVR.

avr-gcc is a big conglomeration of code that supports different platforms. Sometimes you will feel avr-gcc is slow in compiling code. The difference b/n actual speeds might just be a few cycles and b/n actual binary file could just be a few KBs.

The price of avr-gcc, though, can't be beat! There's a free (albeit crippled, forces a limit on code size) IAR tool suite. Look for it at IAR's website. From there you might be able to make your choice.
Title: Re: C vs. ASM
Post by: stopgo on September 23, 2007, 03:03:42 AM
Thanks guys!  I think I'll use avr-gcc while I'm still learning the ropes and then maybe check out IAR's stuff to see if I think the difference is substantial.
Title: Re: C vs. ASM
Post by: Admin on October 02, 2007, 07:17:42 AM
I never use gcc optimization, I turn it off because it causes me problems with my empty while loop delays. But Ive never had problems without the optimization ;D
Title: Re: C vs. ASM
Post by: snow on October 02, 2007, 02:38:27 PM
Some inline asm code in that empty loop would keep compiler with optimizations away. I think. I normally use _delay_loop_2 function for delays and have optimizations turned on and it works ok.