A common question we get is should I program my microcontroller in C or assembler? This usually means do I bother with assembler or just use C. Sometimes it just means that I do not know the difference. So what is the difference?
Assembly language is low level programming. A microcontroller can carry out a range of simple instructions, usually between 30 and 100 different ones, each of which does one operation such as add two values, move a value or test a bit. These each have a different binary value, so in theory you could program a microcontroller by writing a series of binary numbers. However, human beings are rubbish at strings of 1’s and 0’s and so would make lots of mistakes, so assembly language is these binary instructions translated into pseudo-English. For example,
mov R1, 20
add R1, R2
Don’t worry about what these mean, the point is each line of assembly language translates into one binary instruction that the microcontroller understands, hence low level.
With a high level language, such as C or Basic, each line of code can translate into many binary instructions, so there is no longer a direct relationship between the two and we rely on the C or Basic compiler to produce the simple instructions that the AVR or PIC microcontroller will run.
Writing code in C or Basic is much easier but it hides the structure of the microcontroller from you, and leads to another common question we get – can you give me the C instruction set for AVR or PIC? There is no “C instruction set” for a particular microcontroller, just a series of standard C (or Basic) commands that the compiler converts into assembly language for the target microcontroller. So, the first thing you need to understand is basic C programming, For loops, While loops, if..else and switch instructions plus all the standard arithmetic and logic commands. These are the same for any target, including x86 PC programs or any microcontroller
In theory, a C program can be recompiled to run on different microcontrollers, but in practice hardware differences get in the way. A lot of microcontroller code needs to change hardware settings to access I/O ports, set up timers or use communication interfaces such as serial ports, and this means manipulating the hardware control registers that are microcontroller specific.
It is much easier to understand these registers and how they interface with the rest of the microcontroller if you start with learning assembly language because this forces you to understand more about your device and how it works. Once you have written a few programs in assembler you will understand more about the microcontroller and moving on to C is much easier.
The only benefit of assembly language is smaller, faster code but memory is cheap now, a microcontroller with a 16 KB program memory is not much more than an 8KB one, and C compilers have got better at producing smaller code, although free ones aren’t so good. The downside of assembler is you have to do everything, including memory management, and C constructions like loops are much easier and faster to write.
So, it makes sense to produce programs in C or Basic, but you will never understand the way the microcontroller fits together and the different resources available or its limitations if you don’t learn assembly language first. Start with assembler, understand the device, then move on to C is our advice.