Programming Microcontrollers: C Language or Assembly?

Programming Microcontrollers: C Language or Assembly?
A microcontroller is a programmable device that simplifies hardware design and enhances product functionality, with the program being the soul of the microcontroller.Currently, slightly more complex electronic products are centered around microcontrollers, supplemented by various peripheral circuits to meet different functional requirements.The programming of microcontrollers can be achieved through assembly language and C language.
1Characteristics of Assembly Programming
Assembly is a low-level language that is closer to hardware.During programming, the bus and addresses are very important; in assembly language, mnemonics generally replace the operation codes of machine instructions, and labels represent the addresses of operands. Therefore, there is a one-to-one correspondence between assembly language and machine instructions, leading to completely different assembly instruction sets for different series of microcontrollers.This can be quite troublesome, as changing the microcontroller or upgrading old products makes the original assembly code difficult to port across platforms, necessitating a complete rewrite.Thus, assembly’s portability is very poor, and due to direct manipulation of machine instructions, its readability is also very low.These are the two biggest drawbacks of assembly language, but its advantage is very high execution efficiency.From a convenience perspective, assembly facilitates hardware but is not friendly to engineers.

Programming Microcontrollers: C Language or Assembly?2Characteristics of C Language Programming

In recent years, the number of people using assembly programming has decreased significantly. Even those writing drivers do not solely use assembly; instead, they combine assembly with C language.Programming microcontrollers in C has two major advantages.
The first advantage: easy to port
Colleagues who have experience in microcontroller C programming often have the experience of first configuring the microcontroller’s registers during initialization. After this configuration, the code does not frequently interact with the registers, making the structure of C code very clear. When porting code, only the register configuration part needs to be modified for the new platform, while the main functionality remains largely unchanged.This is why many excellent programmers advocate for hierarchical programming, where each functional block corresponds to a C file and a header file (H file). When porting, you only need to copy these two files, which simplifies the porting process.

Programming Microcontrollers: C Language or Assembly?

From this perspective, programming can completely disregard the configuration of the microcontroller’s registers. Good examples include programming using the official STM32 library and CMSIS.
The second advantage: easy to read
Since C language establishes relationships through registers and hardware drivers/interfaces, programmers no longer need to memorize tedious instructions. The hierarchical and modular programming approach of C language greatly enhances code readability.Compared to assembly, C language can implement more complex functionalities with less code and is easier to understand.This is also why C language is becoming increasingly popular.

Programming Microcontrollers: C Language or Assembly?3Advantages of C Language Compared to Assembly

From the previous descriptions, it is clear that C language has more advantages: it is easy to port across platforms, easy to understand, and easy to maintain.In summary, from a programming perspective, C language is more favored by programmers.
Programming Microcontrollers: C Language or Assembly?

Leave a Comment