Why C in Hardware
Why C in Hardware
language in
Hardware Programming?
linkedin.com/in/kapilyadav22/
What Does a Processor
Understand?
Processor understands only machine language,which is composed of
binary numbers containing zeroes(0's) and ones(1's).
Example-0101010101110.
Every unique CPU (processor) have its own specific Machine Language.
Programming in Machine Language is difficult as we need to memorize
a long sequence of bits.
linkedin.com/in/kapilyadav22/
What Does a Processor
Understand?
Machine language programs are executable, they can be run directly.
Creating a program in a machine-level language is a very difficult task as it is
not easy for the programmers to write the program in machine instructions.
It is not easy to understand, and its maintenance is also very high.
A machine-level language is not portable as each computer has its machine
instructions, Machine languages are platform-dependent.
linkedin.com/in/kapilyadav22/
Levels of Programming
Languages
linkedin.com/in/kapilyadav22/
Before use of C language
Before C, Micro-controller based systems, programs were developed using
assemblers and fused into EEPROMs.
There was no mechanism to find what the program was doing.
LED, switches etc., were used to check the correct execution of program.
As time proceeded, microprocessor specific and assembly-only as
programming language got reduced.
Embedded systems moved onto C.
linkedin.com/in/kapilyadav22/
WHY C IS USED IN HARDWARE
PROGRAMMING?
linkedin.com/in/kapilyadav22/
1.Processor Independent
C language is not specific to any microprocessor/micro-controller or any
system.
It can work on various hardware configuration. C doesn’t require same set of
hardware to run a program.
C language is platform independent whereas the applications written in C are
not. Hence the concept “Write once Compile anywhere” came.
Example:
We can compile and execute a C program written for one hardware in another
hardware with little to no change. If there is a change required, it is only in
terms of platform (Operating System) specific functions and system calls. This
is said to be machine independent.
While writing a program in C, user doesn’t have to know the underlying
hardware specific details of the machine, since the compiler takes care of it.
When developers compile a C program for a particular hardware, respective
assembly code is generated. It is then linked with libraries (static and dynamic)
to give an executable file. I
It should be noted that only C code is machine independent, the executable file
generated is Machine as well as Platform (OS) dependent. So, whenever a code
written in C must be used in another machine, it should be compiled and
executed again.
linkedin.com/in/kapilyadav22/
2.Portability
Portability of a language is mainly important when an application program must
be moved to other operating systems.
Code developed in C is more portable and user can compile it on other platforms
with least modifications.
C code is efficient, easy to understand, maintain and debug.
A program can be written in one machine and can be run on other machines.
Example:-
Let us consider simple example of two integer addition!
The size of integer variable depends on the underlying machine architecture.
Popularly called as “Word Size” the size can be different. For example, in a 32-bit
machine occupies 2 bytes whereas an integer variable in 64-bit machine takes 4-
bytes.
The max value stored in a 4 byte (unsigned integer) can vary between 0 to
4,294,967,295 whereas in case of 2 byte the range reduces to 0 to 65535. In case
of sum of two numbers (say A and B) exceeding 65535 it will create integer
overflow in a 32-bit machine by giving incorrect output.
In order to avoid or deduct the issue early, developer can use “typedef” and create
a synonym (ex: 32 for int). This will be helpful when program is ported to another
machine and the developer can make appropriate changes in the code, thereby
making the program portable and avoid error.
linkedin.com/in/kapilyadav22/
3.Performance
Add a little bit of C code gets compiled into raw binary executable which can
be directly loaded into memory and executed. C provides optimized machine
instructions for the given input, which increases the performance of the
embedded system. Most of the high-level languages rely on libraries, hence
they require more memory which is a major challenge in embedded systems.
For example, In Java, we need JVM (Java Virtual Machine) in addition to the
jar files (Executable) which adds additional overhead in terms of performance
and memory consumption. Newer languages provide support for garbage
collection, dynamic typing etc., which degrades their performance. Since C
does none of that, there is little to no overhead.
linkedin.com/in/kapilyadav22/
4.Bit Manipulation
C is more flexible, structured language that provides “low level bit wise data
manipulation” using the bit-wise operators. Using bit-wise operators, one
could play with available bits, which comes handy when it comes to
Embedded Systems (Ex: Handling registers).
Example: 1 – Setting or clearing a bit
linkedin.com/in/kapilyadav22/
4.Bit Manipulation
Example 2 – Encoding Algorithms
If an important text or image has to be sent to others, we can encode the data and
send it. Encoding will help in securing the data from hackers, since the original
message will not be seen even if it is hacked. At the receiving end, decoding should
be done by the receiver to obtain the original data. This is a boon to developers. And
this is why C is the most preferred language for embedded systems than other
languages.
In this example, if the data bit is 1, then bitwise OR is performed with integer 1 and
encoding is done.
If the data bit is 0, then bit-wise AND is performed with (~1) and encoding is done.
Hence original data is changed and encoded. Such encoding algorithm
implementation is done with use in C language.
linkedin.com/in/kapilyadav22/
5.Memory Management
It provides direct memory control without sacrificing benefits of high-level
languages. We can directly access memory using pointers and perform various
operations using them.
When memory requirement is not known, we can allocate it dynamically and when
memory requirement is known, we can allocate the memory statically. This is one
of the main reasons for why is C the most preferred language for embedded
systems.
Example 1 – Simple dynamic memory allocation
When amount of memory required is unknown, dynamic memory allocation is
used, which also helps in optimized memory usage. In the below code, memory to
pointer ptr is dynamically allocated during run time and malloc() returns a void
pointer which can be type casted into pointer.
After allocating and performing appropriate operations (ex: addition in this case)
the memory is freed up using the free() function.
linkedin.com/in/kapilyadav22/
5.Memory Management
The pointer facility in C allows user to manage the memory in his own way. For
example we are allocating 100 bytes of memory and storing it into void pointer,
as returned by the malloc() function.
Now these 100 bytes can be used the way the developer wants it. For example let
us consider a requirement where the user wants to store a length and string
contents continuously in memory like (len1,string1) | (len2,string2) |
(len3,string3) …. (lenN,stringN) and print them.
Since the malloc() returns a void pointer, the user can do typecasting and assign
them to character or integer pointers to access appropriate values. Pointer
typecasting combined with pointer arithmetic features of C makes such things
possible, which will be very difficult in other programming languages.
In case of Embedded systems, developers constantly need to manage and
manipulate memory (ex: A frame of memory returned by the device driver from
the hardware) where such powerful aspects of C comes handy for the developer.
linkedin.com/in/kapilyadav22/
5.Memory Management
linkedin.com/in/kapilyadav22/
5.Memory Management
linkedin.com/in/kapilyadav22/
6.Readability
C language syntax is easy to understand and it is medium-level language. It is
having the capability to interact with high-level languages like JAVA and C#
and at the same time, C language is having the capability to interact with
low-level Assembly language.
Since C language is a procedure-oriented language, it helps to write modular
code, which is highly readable. Embedded application code is written once
then the main task is to maintain them throughout the product life, fix the
bug, control version, testing, and validation. For all the above points we need
to have code should be readable.
7.Scalable
C language is scalable to the extent that Linux is a million lines of code is
written into C language. On average every day around 20-30 thousand lines
of code are added into Linux OS itself. So C language is scalable up to the
extent thought become limits.
8.Maintainability
There are many source control software and website are available that help to
maintain source code, the main CVS, PVCS, Git-hub, etc. In the C language
also there are many compile-time directives that help to maintain the source
code version.
The main compile-time keywords are #if, #else, #elif, #endif, #define etc.
Using the above keyword, the user can do version control to make a good
working code.
linkedin.com/in/kapilyadav22/
Embedded Systems:
A computer system with a specific function within a larger system
(mechanical or electrical system) is an embedded system. It is also possible
that the Embedded Systems has real time computing constraints, thereby
making them as Real-Time-Systems (RTS). Today Embedded Systems are
everywhere starting from wearable devices to high-speed transportation
systems.
Practically we can view Embedded Systems as a customized software
running on a customized hardware. Since the hardware is very specific,
software also needs to be tuned, which puts additional constraint on
developer.
Along with that, Embedded Systems are typically resource constrained
systems, where usage of every other HW resource (ex: memory) must be
done efficiently. C language is a killer when it comes to all these aspects!
linkedin.com/in/kapilyadav22/
Embedded Systems:
An embedded system is a normally small electronic device that is meant for a
single purpose. As it is a small device then it is having the following limitation.
linkedin.com/in/kapilyadav22/