C awesomeness (not)

The_C_Programming_Language_logo.svg

Few days ago I was asked question what would be the output of this code:

#include <stdio.h>
int main()
{
      int a = 5;

      printf("%d %d %d %d\n",a++,a++,++a,a++);

      return 0;
}

Nothing special, right? But C standard do not specify how printf evaluates it’s parameters, so it’s left up to the C compiler and here the fun part starts.

Old C compilers would be a bit more predictable as they would start evaluating from the top of the stack to the bottom i.e. right to left, but the new ones heavy optimize and try to execute some instructions in parallel, so the only certain thing is that at the printf “;” point of synchronization “a” would be incremented 4 time, but nobody knows in what order.

Compiled with GCC/Code Blocks the output is:

Screenshot from 2019-02-20 11-46-45

Again nobody guarantee that if you re-compile on different computer /CPU the result will be same!

Even changing the code with one more line like enter the value of a with scanf leads to different result:

Screenshot from 2019-02-20 11-47-47

So good practice will be to avoid any calculations inside printf and to search logic there. Do these separately where compiler has to stick to C standard and rules.

 

Nice tutorial for writing and debugging plain C on ARM Cortex-M3 STM32F103

OLIMEX-STM32-H103

Jacob Mossberg wrote nice tutorial how to program in C ARM Cortex-M3 with GCC and how to debug it with GDB and OpenOCD.

We like very much the hardware he is using 🙂 STM32-H103 and ARM-USB-OCD-H.

 

ESP8266-EVB support for JTAG development with flash load, breakpoints, disassembly, step debugging in Visual Studio C/C++

17-stepover

Ivan Shcherbakov @SysProgs shared interesting article. He wrote support for ESP8266-EVB to VisualGDB Visual Studio plugin for embedded software development in C/C++.

With this plugin you can use JTAG to debug code in ESP8266, also to set breakpoints, view the disassembled code etc. This adds new level for professional developing using ESP8266.

MPIDE Arduino like IDE for PIC32-Pinguino and Duinomite boards

Image

With this tutorial video below we will show you how to setup MPIDE Arduino like IDE to work with PIC32-PINGUINO, PIC32-PINGUINO-OTG, PIC32-PINGUINO-MICRO, DUINOMITE, DUINOMITE-MINI, DUINOMITE-MEGA, DUINOMITE-eMEGA.

Here is video tutorial how to install MPIDE and add support for Pinguino and Duinomite boards:

MPIDE installation is also described in our Wiki

Why to use MPIDE?

It’s an alternative to Pinguino IDE and Duinomite Basic. You have one more option to use these boards.

Pinguino IDE  ( pinguino.cc ) was made as parallel project to Arduino but for PIC microcontrollers, instead of Java it uses Python for the IDE, with the latest Oracle – Google lawsuit we still wonder if Java was the best choice for Arduino IDE, but nevertheless Arduino was made earlier and managed to attract huge community. Pinguino community is still small and the demo codes for Pinguino are not so many as for Arduino.

Microchip and Digilent financed the release of MPIDE which is pure Arduino IDE fork and thus more familiar for the Arduino community to use. As MPIDE already have good PIC32 support it was very easy to add support for PIC32-Pinguino and Duinomite there.

Why to use MPIDE and C for Duinomite?

Duinomite boards are low cost but powerful boards initially made specially for project named Maximite.
Two years ago we found interesting project which makes Basic computer with just PIC32 processor.
I started with Apple ][ so it was very nostalgic to see BASIC computer made with embedded processor and to control GPIOs and resources in BASIC.

At that time Maximite was advertised as “open source” project, i.e. firmware sources were available for download. The hardware schematic also was present in JPEG format.

As hardware engineers we immediately spotted some weakness in the Maximite hardware design – the author being mostly software engineer omitted lot of hardware features PIC32 have.

While PIC32 have ultra low power modes, hardware SPI, I2C, UARTs, parallel interfaces which could work with MHz clock speed, for some weird reason the Maximite author decided to implement them by bit-banging purely in software – cripple-ing these interfaces to very low speeds only. Low power modes were not implemented at all.

So we decided to improve the hardware – this is how we made our board with low power design, hardware UARTs and SPIs, UEXT and Arduino connectors so Arduino shields like Gameduino etc could be placed on top of the board and so on.

We completed our hardware design and we proudly offered the Maximite author to check it and tell us his opinion … but he told us that he is not interested and demanded us to choose different name than Maximite. It was a quite surprising open source project author to refuse contribution. So we named our board Duinomite (Maximite is also explosive so we wanted to show where our board roots are).

Being mostly Hardware company we couldn’t implement the software our self, so we contacted Ken Seggler who at this time made port of Maximite for ChipKit board with same PIC32 processor. He was unemployed at that time and he accepted to make the port and even to implement our new ideas we had about low power sleep modes, the new hardware UART and SPI, SETUP command for general board setup, Gameduino commands, we discussed on public forum how to implement editor inside the BASIC and label-less commands etc.

Unfortunately this move really pissed off the Maximite author. He saw the Maximite BASIC port to our board as direct competition as now our hardware had more features than his own.

He closed the Maximite Basic source, then wrote one nasty page about Duinomite on his web presenting him as victim of the big bad company who steal his work and removed his name out of it.
From day one Duinomite Basic fork is on GitHub and anyone can check how true these blames are – Maximite author’s name have been always on our Duinomite Basic fork and display on startup . We had to fork his BASIC just because he refused to support our hardware.

Meantime Ken found full time job and couldn’t contribute to the project anymore, the Maximite author released new version of Maixmite Basic with label-less commands, build-in editor and his new Maximite hardware has … Arduino connectors  (surprise surprise 🙂 ), so after all he has listened to us and took our contributions silently back !

Duinomite software left at that stage as no one was interested to duplicate Maximite Basic work and to run in parallel two same language implementations. The Maximite author had one problem though – while Duinomite quality build hardware cost just EUR 20 the Maximite less capable hardware in kit form cost was something like x3 times more and obviously people were buying Duinomite hardware, so to increase his followers he made Duinomite support and offered HEX compiled version of his Maximite BASIC for Duinomite – actually doing what we originally asked him for to do!

As far for the Duinomite BASIC fork – none of the younger and capable developers is interested in BASIC. Most of Maximite / Duinomite BASIC users are 40+ years and just users but not contributors / developers, so Duinomite BASIC development is pretty much dead.

Although Duinomite boards are still high seller, I guess 99% of people buy them and use them as regular PIC32 development board and use C instead of Basic judging from the tech supports questions we get.

Now with MPIDE Arduino like IDE people who are familiar with Arduino can develop on Duinomite too.

Free online e-book: Build your own Lisp

Image

Quote from the book – “Mike Tyson – Your typical Lisp user” 🙂

If you’re looking to learn C, or you’ve ever wondered how to build your own programming language, this is the book for you.

Are you ready to design your first Compiler? Excellent book and code

Image

Free Online Compiler Design Textbooks by prof. Seth D. Bergmann http://elvis.rowan.edu/~bergmann/books.html

Excellent book on Compiler design and demo codes for small Pascal, C++ and Java compilers.

Free e-book online: More C++ Idioms

Image

http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms

The objective of this open content book is to present modern C++ idioms to programmers who have moderate level of familiarity with C++, and help elevate their knowledge so that C++ feels much friendlier to them.

Previous Older Entries