ES Lecture 4
ES Lecture 4
ES Lecture 4
SYSTEMS (17EC62)
• Once the program model is created, the next step is the implementation of
the tasks and actions by capturing the model using a language which is
• The order in which the tasks to be executed are fixed and they are
hard coded in the code itself.
• Also the operation is an infinite loop based approach.
• If the program hangs up at some point while executing a task, it will remain there forever and
ultimately the product stops functioning.
• Watch Dog Timers (WDTs) can be used to overcome this, but this, in turn, may cause additional
hardware cost and firmware overheads.
• If the number of tasks to be executed within an application increases, the time at which each task is
repeated also increases.
• This brings the probability of missing out some events.
• For example, in a system with keypads, in order to identify the key press, you may have to press the
keys for a sufficiently long time till the keypad status monitoring task is executed internally by the
firmware.
• Interrupts can be used for external events requiring real time attention.
Embedded Operating System (OS) Based
Approach
• Similar to the different hardware specific drivers, OS based applications also require
'Driver software' for different hardware present on the board to communicate with them .
Embedded Operating System (OS) Based
Approach (continued)
• Real Time Operating System (RTOS) based design approach is employed in embedded
products demanding Real-time response.
• RTOS responds in a timely and predictable manner to events.
• Real Time operating system contains a Real Time kernel responsible for performing pre-
emptive multitasking, scheduler for scheduling tasks, multiple threads, etc.
• A Real Time Operating System (RTOS) allows flexible scheduling of system resources like
the CPU and memory and offers some way to communicate between tasks.
• ’Windows CE’, ’pSOS’, ’VxWorks’, ’ThreadX’, ’MicroC/OS-II', ’Embedded Linux’,
• Mobile phones, PDAs (Based on Windows CE/Windows Mobile Platforms), handheld devices,
etc. are examples of 'Embedded Products' based on RTOS.
• Most of the mobile phones are built around the popular RTOS 'Symbian'. (sic)
Embedded Firmware Development
Languages
MOV A, #30
Here MOV A is the Opcode and #30 is the operand
• The same instruction when written in machine language will look like
01110100 00011110
where the first 8-bit binary value 01110100 represents the opcode MOV A and
Assembly Language Based Development
(continued)
• For example
• Modular programming is employed when the program is too complex or too big.
• In 'Modular Programming', the entire code is divided into submodules and each module is
made re-usable.
• Modular Programs are usually easy to code, debug and alter.
Assembly Language Based Development
(continued)
Assembly language to
machine language
conversion process
Machine Code
(Hex File)
Assembly Language Based Development
(continued)
• Keyword 'PUBLIC' and 'EXTRN' are used while accessing shared variables and
subroutines.
Assembly Language Based Development
(continued)
• When the linker processes a library, only those object modules in the library
that are necessary to create the program are used.
• Library file is some kind of source code hiding technique.
• For example, 'LIB51' from Keil Software is an example for a library creator and
it is used for creating library files for A51 Assembler/C51 Compiler for 8051
specific controller.
Assembly Language Based Development
(continued)
• Linker and Locator
• Linker and Locater is another software utility responsible for "linking the various object
modules in a multi-module project and assigning absolute address to each module".
• Linker generates an absolute object module by extracting the object modules from the
library, if any, and those obj files created by the assembler, which is generated by assembling
the individual modules of a project.
• It is the responsibility of the linker to link any external dependent variables or functions
declared on various modules and resolve the external dependencies among the modules.
• An absolute object file or module does not contain any re-locatable code or data.
• All code and data reside at fixed memory locations.
• The absolute object file is used for creating hex files for dumping into the code memory of
the processor/controller.
• 'BL51' from Keil Software is an example for a Linker & Locater for A51 Assembler/C51
Compiler for 8051 specific controller.
Assembly Language Based Development
(continued)
• Object to Hex File Converter
• This is the final stage in the conversion of Assembly language (mnemonics) to
machine understandable language (machine code).
• Hex File is the representation of the machine code and the hex file is dumped into
the code memory of the processor/controller.
• The hex file representation varies depending on the target processor/controller
make.
• HEX files are ASCII files that contain a hexadecimal representation of target
application.
• Hex file is created from the final 'Absolute Object File' using the Object to Hex File
Converter utility.
• 'OH51' from Keil software is an example for Object to Hex File Converter utility
for A51 Assembler/C51 Compiler for 8051 specific controller.
Advantages of Assembly Language Based
Development
• Most of the code for low level programming like accessing external device specific
registers from the operating system kernel, device drivers, and low level interrupt routines,
etc. are making use of direct assembly coding since low level device specific operation
support is not commonly available with most of the high-level language cross compilers.
• Code Reverse Engineering
• Non-Portable
• Target applications written in assembly instructions are valid only for
that particular family of processors (e.g. Application written for Intel
x86 family of processors) and cannot be re-used for another target
processors/controllers (Say ARM11 family of processors).
• If the target processor/controller changes, a complete re-writing of the
application using the assembly instructions for the new target
processor/controller is required.
High Level Language Based Development
• Any high level language (like C, C++ or Java) with a supported crosscompiler
for the target processor can be used for embedded firmware development.
• The most commonly used high level language for embedded firmware
application development is 'C.
• 'C' is well defined, easy to use high level language with extensive cross
platform development tool support.
• Nowadays cross-compilers for C++ is also emerging out and embedded
developers are making use of C++ for embedded application development.
High Level Language Based Development
(continued)
• The various steps involved in high level language based embedded firmware
development is same as that of assembly language based development except
that the conversion of source file written in high level language to object file is
done by a cross-compiler.
• In Assembly language based development it is carried out by an assembler.
• The various steps involved in the conversion of a program written in high level
language to corresponding binary file/machine language is illustrated in the
figure.
High Level Language Based Development
(continued)
Machine Code
(Hex File)
High Level Language Based Development
(continued)
• The program written in any of the high level languages is saved with the
corresponding language extension (.c for C, .cpp for C++ etc).
• Any text editor like 'Notepad' or 'WordPad' from Microsoft or the text editor
provided by an Integrated Development (IDE) tool can be used for writing the
program.
• Most of the high level languages support modular programming approach and
hence we can have multiple source files called modules written in corresponding
high level language.
• The source files corresponding to each module is represented by a file with
corresponding language extension.
High Level Language Based Development
(continued)
• Developer Independency
• The syntax used by most of the high level languages are universal
and a program written in the high level language can easily be
understood by a second person knowing the syntax of the
language.
• High level languages always instruct certain set of rules for
writing the code and commenting the piece of code.
• If the developer strictly adheres to the rules, the firmware will be
100% developer independent.
Advantages of High Level Language
Based Development (continued)
• Portability
• Target applications written in high level languages are converted to target
processor/controller understandable format (machine codes) by a cross-compiler.
• An application written in high level language for a particular target processor can
easily be converted to another target processor/controller specific application, with
little or less effort by simply re-compiling/little code modification followed by
recompiling the application for the required target processor/controller, provided, the
cross-compiler has support for the processor/controller selected.
• This makes applications written in high level language highly portable.
• Little effort may be required in the existing code to replace the target processor
specific files with new header files, register definitions with new ones, etc.
• This is the major flexibility offered by high level language based design.
Limitations of High Level Language Based
Development
• Poor Optimization by Cross-Compilers
• Some cross-compilers available for high level languages may not be so efficient
in generating optimised target processor specific instructions.
• Target images created by such compilers may be messy and non-optimised in
terms of performance as well as code size.
• For example, the task achieved by cross-compiler generated machine instructions
from a high level language may be achieved through a lesser number of instructions
if the same task is hand coded using target processor specific machine codes.
• The time required to execute a task also increases with the number of
instructions.
• However modern cross-compilers are tending to adopt designs incorporating
Limitations of High Level Language Based
Development (continued)
• High level language based code snippets may not be efficient in accessing
low level hardware where hardware access timing is critical (of the order of
nano or micro seconds).
• High Investment Cost
• The investment required for high level language based development tools
(Integrated Development Environment incorporating cross-compiler) is high
compared to Assembly Language based firmware development tools.
Mixing Assembly and High Level
Language
• The programmer must be aware of how parameters are passed from the 'C'
routine to Assembly and values are returned from assembly routine to 'C'
and how 'Assembly routine' is invoked from the 'C' code.
• Passing parameter to the assembly routine and returning values from the
assembly routine to the caller 'C' function and the method of invoking the
assembly routine from 'C' code is cross-compiler dependent.
Mixing Assembly Language with High
Level Language (continued)
• Consider an example Keil C51 cross compiler for 8051 controller.
• The steps for mixing assembly code with 'C' are:
• Write a simple function in C that passes parameters and returns values the way
you want your assembly routine to.
• Use the SRC directive (#PRAGMA SRC at the top of the file) so that the C
compiler generates an .SRC file instead of an .OBJ file.
• Compile the C file. Since the SRC directive is specified, the .SRC file is
generated. The .SRC file contains the assembly code generated for the C code you
wrote.
• Rename the .SRC file to .A51 file.
• Edit the .A51 file and insert the assembly code you want to execute in the body
of the assembly function shell included in the . A51 file.
Mixing Assembly Language with High
Level Language (continued)
• Most often the functions written in ’C’ use parameter passing to the
function and returns value/s to the calling functions.
• Parameters are passed to the function and values are returned from the
function using CPU registers, stack memory and fixed memory.
• Its implementation is cross compiler dependent and it varies across
cross compilers.
Mixing High Level Language with
Assembly Language (continued)
R7 is used for returning char value and register pair (R7, R6) is
used for returning int value.
The 'C' subroutine can be invoked from the assembly program
using the subroutine call Assembly instruction.
For example
LCALL Cfunction