go_away

Author Topic: C vs. ASM  (Read 2443 times)

0 Members and 1 Guest are viewing this topic.

Offline stopgoTopic starter

  • Full Member
  • ***
  • Posts: 58
  • Helpful? 0
C vs. ASM
« 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?

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: C vs. ASM
« Reply #1 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

Offline snow

  • Full Member
  • ***
  • Posts: 73
  • Helpful? 0
Re: C vs. ASM
« Reply #2 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.

Offline stopgoTopic starter

  • Full Member
  • ***
  • Posts: 58
  • Helpful? 0
Re: C vs. ASM
« Reply #3 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?

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: C vs. ASM
« Reply #4 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.
« Last Edit: September 21, 2007, 06:25:36 AM by JesseWelling »

Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: C vs. ASM
« Reply #5 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.
The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

Offline stopgoTopic starter

  • Full Member
  • ***
  • Posts: 58
  • Helpful? 0
Re: C vs. ASM
« Reply #6 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.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: C vs. ASM
« Reply #7 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

Offline snow

  • Full Member
  • ***
  • Posts: 73
  • Helpful? 0
Re: C vs. ASM
« Reply #8 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.

 


Get Your Ad Here