• AVR Freaks

Hot!Why C Over Assembly?

Author
Wanglesnap
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2019/01/30 10:40:15
  • Location: 0
  • Status: offline
2019/03/18 13:19:40 (permalink)
0

Why C Over Assembly?

Hello All
I am fairly new to mcus and pics but I have finally clicked with assembly and think it great.I have been a C/C++ programmer for many years so I should be able to transfer from Assembly without difficulty.My question is after looking into compilers and their issues what is the advantage? 
#1

17 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Why C Over Assembly? 2019/03/18 13:59:03 (permalink)
    +2 (2)
    You don't mention which PIC family or compiler you're using.
    In general, C takes care of all of the housekeeping for you, making loop structures, variable assignment and math operations much more convenient and less error prone.
    Understanding assembler makes it easier to get the most out of your PIC, but 99 percent of your job is easier in C.
    I spent 20 years working just in Assembler. Now I've moved most of my development to C, I've never looked back.
    (and be aware, C is much closer to the "bare metal" than C++.)
     

    Nearly there...
    #2
    Mysil
    Super Member
    • Total Posts : 3325
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: Why C Over Assembly? 2019/03/18 15:12:11 (permalink)
    +1 (1)
    Hi,
    With PIC12..., PIC16... and PIC18... devices, that have wery small address range in each instruction,
    the C compiler take care of memory bank selection, and avoid mistakes that mey be difficult to find.
    Also, together with the linker, C compiler take care of function call arguments and local variables.
     
    Remember, 8-bit PIC devices do not have a memory stack to use for function call arguments and local variables.
    The hardware stack have only one word for each call level, the return address.
    So the compiler juggle and reuse local variables, and a small number of 'access memory' bytes,
    for function arguments and results. 
     
    XC8 C compiler do not follow the traditional distinction between Compiler and Linker.
    It do analysis of source code for each function separately, with translation into intermediate code. 
    It then do a linkage analysis of the whole program as basis for memory placement of code and variables,
    before actual code generation.
    Depending upon compiler license, there are several optimization stages, including whole program optimization.
    Without a license, in free mode, some of these optimizations are not performed.
    It still will produce runnable code for any size program, as long as it can fit in memory.
     
    There is a 2 month tryout license available for installation on a computer where the license have not been installed before.
    My suggestion is: Do Not request and install the temporary license with first experiments.
    Instead, you may wait until you have some significant code running in free mode,
    and then try it with full optimization.
     
    With XC8 v2.x versions, there may be some changes, optimization levels are now numbered,
    with optimization levels: 0, 1 and 2, available in free mode.
     
    With XC8 compiler, writing assembly language functions to interface with C code calls,
    isn't the same as with other platforms with a more structured call interface. 
    There are function argument list signatures defined, so it is possible to do,
    but this is not a beginners exercise anyway.
     
    My preference is to use C language on PIC microcontrollers,
    I have done some small pieces of assembly code for some high performance sequences.
    Background is Fortran programming on several different computers from the 1970's and onwards.
     
        Mysil
    #3
    Howard Long
    Super Member
    • Total Posts : 676
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: Why C Over Assembly? 2019/03/18 15:30:03 (permalink)
    +1 (1)
    Same here, I cut my teeth on assembler on embedded stuff starting in the 70s. While I did some C on embedded 68000 and Z80 from the mid 80s, and into the early 90s, the toolchains, cross-compiling, EPROM blowing, ROM emulators and horrendously expensive in circuit emulators made life interesting, so I tended to stick to assembler. This included stuff like writing floating point libraries by hand because a floating point compiler option was so darned expensive.

    On PIC MCUs, although I’ve been using them since the early 90s in assembler, I only started usng C with PICs around 2003. Simce about then, I haven’t written any serious assembler for very many years other than when optimising targetted bottlenecks. While optimising C compilers usually do a pretty good job, don’t believe it when they tell you optimisers are always better than human written code: remember that C compilers don’t understamd what a use case is! Now while I don’t write much assembler nowadays, I do read the disassembled code when debugging every day.
    #4
    NKurzman
    A Guy on the Net
    • Total Posts : 17504
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Why C Over Assembly? 2019/03/18 15:42:00 (permalink)
    +3 (3)
    Why C? Becuse it is faster to code and learn for a wider group of programmers. An experienced ASM programmer will create smaller, faster code. They may even be faster than a C programmer. But getting to that skill level takes many years.
    C also reduces programmer work load on larger programs. It is why it was created to begin with.
    #5
    malaugh
    Super Member
    • Total Posts : 398
    • Reward points : 0
    • Joined: 2011/03/31 14:04:42
    • Location: San Diego
    • Status: offline
    Re: Why C Over Assembly? 2019/03/18 15:57:33 (permalink)
    +3 (3)
    No-one mentioned portability yet.   Another reason to use C is to be able to use your code on a different CPU.   Most companies use different CPUs on different projects, and moving a C function involves re-writing any hardware drivers.  In assembly all the code has to be rewritten from scratch. 
    #6
    NorthGuy
    Super Member
    • Total Posts : 5490
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Why C Over Assembly? 2019/03/18 21:00:52 (permalink)
    +2 (2)
    When you write a lot in assembler, you start using macros. After few years you have created so many macros that using assembler gets much easier, especially if you use macros for code generation (as you can with MASM for example). At this point you'll be reluctant to move to C.
     
    The C is basically a ready-made macro assembler where lots of macros are already created for you. If you want to create a relatively simple project (as most projects for PICs are), it's much faster and easier to do it in C. This is especially true because most people, even when writing in assembler, use C-like procedural flow. Of course, if you want to push the limits, you have to go back to the assembler.
     
    #7
    Wanglesnap
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2019/01/30 10:40:15
    • Location: 0
    • Status: offline
    Re: Why C Over Assembly? 2019/03/19 07:25:10 (permalink)
    0
    Thanks I needed other view points ,I am not yet experienced enough to make judgements of my own.I do not know of any compiler within the price point of a hobbist that may later turn professional.
    post edited by Wanglesnap - 2019/03/19 07:33:32
    #8
    Jan Audio
    Starting Member
    • Total Posts : 53
    • Reward points : 0
    • Joined: 2018/09/24 08:12:24
    • Location: 0
    • Status: offline
    Re: Why C Over Assembly? 2019/03/19 07:57:24 (permalink)
    0
    (*disclaimer*)I have read somewhere :
    With PIC you can make improvements with ASM
    If you use other brand : STM32, the compiler is hard to beat.
     
    I also read Microchip is probably not continuing todays chips and changing architecture.
    So for me : when finished learning ASM over 15 years, these chips will be more advance with hard to beat compilers.
    Unreadable stuff.
    #9
    nigelwright7557
    Super Member
    • Total Posts : 226
    • Reward points : 0
    • Joined: 2006/11/06 08:15:51
    • Location: 0
    • Status: offline
    Re: Why C Over Assembly? 2019/03/19 08:52:30 (permalink)
    +1 (1)
    The newer PIC32's are so complex to use that C is much better.
    They were designed to use C anyway.
     
    Assembler, C then Harmony. That's the way it is going.
     
     
    #10
    Howard Long
    Super Member
    • Total Posts : 676
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: Why C Over Assembly? 2019/03/19 10:50:33 (permalink)
    +3 (3)
    Jan Audio
    (*disclaimer*)I have read somewhere :With PIC you can make improvements with ASMIf you use other brand : STM32, the compiler is hard to beat. I also read Microchip is probably not continuing todays chips and changing architecture.So for me : when finished learning ASM over 15 years, these chips will be more advance with hard to beat compilers.Unreadable stuff.


    This sounds like random opinion and FUD plucked out of thin air rather than factual.
    #11
    Tim Cruise
    Junior Member
    • Total Posts : 94
    • Reward points : 0
    • Status: offline
    Re: Why C Over Assembly? 2019/03/19 10:54:36 (permalink)
    +1 (1)
    To write software faster in human readable codes and earn more money.
     
    To write software slower in non-human readable codes and earn less money.  LoL: LoL
    #12
    mlp
    boots too small
    • Total Posts : 765
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: Why C Over Assembly? 2019/03/20 12:46:02 (permalink)
    +3 (3)
    Howard Long
    don’t believe it when they tell you optimisers are always better than human written code

    But also: DO believe it when they tell you that optimisers will apply each optimisation consistently, in every case where it's applicable, and won't get fatigued and accidentally forget "just that one". You can't guarantee that of even the best human programmer.

    Mark (this opinion available for hire)
    #13
    NorthGuy
    Super Member
    • Total Posts : 5490
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Why C Over Assembly? 2019/03/20 13:30:14 (permalink)
    +1 (1)
    mark.pappin
    ... won't get fatigued and accidentally forget "just that one".



    Compilers have bugs too ... just like humans.
    #14
    KTrenholm
    Super Member
    • Total Posts : 709
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: offline
    Re: Why C Over Assembly? 2019/03/20 13:54:28 (permalink)
    +2 (2)
    NorthGuy
    mark.pappin
    ... won't get fatigued and accidentally forget "just that one".



    Compilers have bugs too ... just like humans.




    I've heard the next release of Human is going to fix the "forgetting why you entered a room" bug.
    #15
    andersm
    Super Member
    • Total Posts : 2605
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: Why C Over Assembly? 2019/03/20 15:45:21 (permalink)
    0
    Boy, it's like 1983 never ended.
    #16
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Why C Over Assembly? 2019/03/20 16:00:37 (permalink)
    +2 (2)
    andersm
    Boy, it's like 1983 never ended.

    What a momentous year! https://en.wikipedia.org/wiki/1983
    January 1 – The migration of the ARPANET to TCP/IP is officially completed (this is considered to be the beginning of the true Internet).
    March 1 - First collection of twelve Swatch models was introduced in Zürich, Switzerland
    March 8IBM releases the IBM PC XT.
    March 23Strategic Defense Initiative: U.S. President Ronald Reagan makes his initial proposal to develop technology to intercept enemy missiles. The media dub this plan "Star Wars".
    April 4 – The Space Shuttle Challenger is launched on its maiden voyage: STS-6.
    June 19 – Don Bluth's Dragon's Lair is released in arcades.
    September 6 – The Soviet Union admits to shooting down Korean Air Lines Flight 007, stating that the pilots did not know it was a civilian aircraft when it violated Soviet airspace.
    September 18 – U.S. heavy metal band Kiss officially appears in public without makeup for the first time on MTV.
    December 2Michael Jackson's Thriller video is aired on MTV for the first time.
    Flashdance and Return of the Jedi are box-office hits.
    McDonald's introduces the McNugget.
    The DeLorean Motor Company ceases production.
     
     

    Nearly there...
    #17
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3122
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Why C Over Assembly? 2019/03/20 19:52:37 (permalink)
    0 (2)
    Sounds like we are all oldies.
     
    Star Trek:TOS and Marvel Comics got me into computers.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #18
    Jump to:
    © 2019 APG vNext Commercial Version 4.5