Writing firmware: assembly or high level?

前端 未结 17 1695
时光说笑
时光说笑 2020-12-13 02:29

Related to:

  • Testing firmware
  • starting a microcontroller simulator/emulator
  • Interpreting assembly code

If you are writing co

相关标签:
17条回答
  • 2020-12-13 03:27

    Most microcontroller manufacturers provide some sort of cross-compiler where you can compile the code on your PC and then transfer it over to the microcontroller.

    Why C?
    An advantage of C is that your code will be easier to port to other microcontrollers in the future. The history of computing has shown that code typically outlasts hardware implementations.
    A second advantage is control structures (if, for, while) that make code more readable and maintainable.

    Why Assembly Language?
    You can hand craft optimizations.

    Verdict
    As is often the case with this sort of question, the trade-offs are very dependent on the specific use.
    Be aware that it is often possible to mix the two by making assembly calls within C code, so you can find a balance that is right for your project.

    Specific to the PIC hardware
    It seems that you don't have the option of GCC with most PIC hardware. On the other hand, as a commenter noted, the Microchip C30 compiler for the 16-bit PIC24 and dsPIC33 is gcc.
    PIC is also not yet supported by SDCC.
    New Info: according to a comment, SDCC has workable support for PIC.
    There are some other open source options, but I don't have experience with them.

    0 讨论(0)
  • 2020-12-13 03:28

    One issue I ran into with writing assembly for a microcontroller is the need to be very careful how your code was laid out. Having a jump table cross memory boundaries, and causing your code to jump to really weird places is rather disturbing. Coding in C, the compiler covers that base for you.

    0 讨论(0)
  • 2020-12-13 03:34

    Definitely C, except when

    • program memory is extremely limited. Say after painstaking hand-optimizing your assembler code you manage to fit your program in this 1024 bytes of Flash, with 0 bytes left. In this case no C compiler will be any good.

    • you want to have absolute timing control. If any amount of interrupt latency is too long you'll have to rely on assembler.

    0 讨论(0)
  • 2020-12-13 03:34

    Plain C or Pascal, Modula2. But due to compiler availability that means C.

    The extra's of C++ and similars are only interesting out of style grounds, since dynamic allocation and program size is usually very limited.

    Also a more complicated runtime can be a pain if your apps get tight.

    Assembler can be useful too, but only if you sell really gigantic quantities, and a smaller firmware means a smaller, cheaper chip (less flash) and the program's size is overseeable (read: there is some chance that you will get it bugfree in time)

    0 讨论(0)
  • 2020-12-13 03:35

    I've had good experience with IAR C compilers for 8051's in the past.

    My recent approach has always been this:-

    Write it in C with a good optimizing compiler, and ONLY THEN if there's a problem with size or speed, consider rewriting certain parts in assembler.

    However, since taking this approach I've never needed to write a single line of assembler...

    0 讨论(0)
提交回复
热议问题