0% found this document useful (0 votes)
22 views22 pages

Unit - 3

Uploaded by

harinikollati789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views22 pages

Unit - 3

Uploaded by

harinikollati789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

UNIT – 3

EMBEDDED FIRMWARE

OTHER SYSTEM COMPONENTS:

These system components are necessary for the proper functioning of the embedded systems
processors/controllers and firmware execution.

Reset Circuit:

 The Reset circuit is essential to ensure that the device is not operating at a voltage level
where the device is not guaranteed to operate, during system power ON.
 The Reset signal brings the internal registers and the different hardware systems of the
processor/controller to a known state and starts the firmware execution from the reset
vector (Normally from vector address 0x0000 for conventional processors/controllers.
 The reset vector can be relocated to an address for processors/controllers supporting
bootloader.

 The reset signal can be either active high (The processor undergoes reset when the reset
pin of the processor is at logic high) or active low (The processor undergoes reset when
the reset pin of the processor is at logic low).
 Since the processor operation is synchronised to a clock signal, the reset signal should
be wide enough to give time for the clock oscillator to stabilise before the internal reset
state starts.
 The reset pulse width can be adjusted by changing the resistance value R and
capacitance value C
 The reset signal to a processor can be applied at power ON through

EMBEDDED SYSTEMS DESIGN UNIT-3


o External passive reset circuit comprising a capacitor and a resistor
o Standard Reset IC like MAX810
 Some microprocessors/controllers contain built-in internal reset circuitry and they do
not require external reset circuitry.

Brown-out Protection Circuit


 Brown-out protection circuit prevents the processor/controller from unexpected
program execution behaviour when the supply voltage to the processor/controller falls
below a specified voltage.
 It is essential for battery powered devices. Since there is a greater chance for the battery
voltage to drop below the required threshold.
 The processor behavior may not be predictable if the supply voltage falls below the
recommended operating voltage. It may lead to situations like data corruption.
 A brown-out protection circuit holds the processor/controller in reset state, when the
operating voltage falls below the threshold, until it rises above the threshold voltage.
 Certain processors/controllers support built in brown-out protection circuit which
monitors the supply voltage internally.
V cc

R1

R2
Q

Reset Pulse
DZ A c ve Low
R3

G ND

 If the processor/controller doesn’t integrate a built-in brown-out protection circuit, the


same can be implemented using external passive circuits or supervisor ICs

For the Brown-out protection circuit with Active Low Output

 The Zener diode (DZ) and transistor Q form the heart of the circuit.
 The transistor conducts always when the supply voltage (VCC) is greater than that of

EMBEDDED SYSTEMS DESIGN UNIT-3


sum of VBE and VZ.
 The transistor stops conducting when the supply voltage (VCC) falls below the sum of
VBE and VZ.
 Select the Zener diode with required voltage for setting the low threshold value for V CC.
 The values of R1, R2, R3 can be selected based on the electrical characteristics of the
transistor in use.
Example: DS1232 from Maxim Dallas

Oscillator Unit
 A microprocessor/microcontroller is a digital device made up of digital combinational
and sequential circuits.
 The instruction execution of a microprocessor/controller occurs in sync with a clock
signal.
 The oscillator unit of the embedded system is responsible for generating the precise
clock for the processor.

There are two types of oscillators.


On-chip oscillators: Some microcontrollers (8051) have on chip oscillator circuit and
require only a quartz crystal or ceramic resonator for producing necessary clock signals.
External oscillators: Quartz crystal oscillators are available as IC chips. If built-in oscillator
unit is not present, they can be used for generating clock pulses.

EMBEDDED SYSTEMS DESIGN UNIT-3


Important parameters of oscillators:

• Clock frequency: The execution speed is directly proportional to clock frequency. But
we should not blindly increase the clock frequency to increase execution speed. There
is an upper threshold of clock frequency for logical devices on the chip. Beyond that,
the system becomes unstable and non-functional.
• Power consumption: Power consumption of system is directly proportional to clock
frequency. If we increase the clock frequency, power consumption will also increase.
• Accuracy: The accuracy of program execution depends on the accuracy of clock
signal. The accuracy of quartz oscillator or ceramic resonator is expressed in +/- ppm
(parts per million).

Real Time Clock (RTC)

• It is a system component responsible for keeping track of time.

• RTC holds information like current time (In hour, minutes and seconds) in 12 hour /24
hour format, date, month, year, day of the week etc and supplies timing reference to the
system.
• RTC is intended to function even in the absence of power.

• RTCs are available in the form of Integrated Circuits from different semiconductor
manufacturers like Maxim/Dallas, ST Microelectronics etc
• The RTC chip contains a microchip for holding the time and date related information
and backup battery cell for functioning in the absence of power, in a single IC package.
• The RTC chip is interfaced to the processor or controller of the embedded system. For
Operating System based embedded devices, a timing reference is essential for
synchronizing the operations of the OS kernel.
• The RTC can interrupt the OS kernel by asserting the interrupt line of the
processor/controller to which the RTC interrupt line is connected. The OS kernel
identifies the interrupt in terms of the Interrupt Request (IRQ) number generated by an
interrupt controller.
• One IRQ can be assigned to the RTC interrupt and the kernel can perform necessary
operations like system date time updation, managing software timers etc when an RTC
timer tick interrupt occurs

EMBEDDED SYSTEMS DESIGN UNIT-3


Watch Dog Timer
• The watchdog timer is a timing device that resets the system after a predefined timeout.

• It is activated within the first few clock cycles after power-up.

• It helps in rescuing the system if a fault develops and program gets stuck.

• On restart, the system functions normally. The watchdog timer reset is a required feature
in control applications.

Microoprocessor /
Controller
Watchdog
Free Running
Reset Pin
Counter

Watchdog Reset

Sys tem C l ock

• Depending on the internal implementation, the watchdog timer increments or


decrements a free running counter with each clock pulse and generates a reset signal to
reset the processor if the count reaches zero for a down counting watchdog, or the
highest count value for an up-counting watchdog.
• If the firmware execution doesn’t complete due to malfunctioning, within the time
required by the watchdog to reach the maximum count, the counter will generate a reset
pulse and this will reset the processor.
• If the firmware execution completes before the expiration of the watchdog timer the
WDT can be stopped from action
• Most of the processors implement watchdog as a built-in component and provides status
register to control the watchdog timer (like enabling and disabling watchdog
functioning) and watchdog timer register for writing the count value.
• If the processor/controller doesn’t contain a built-in watchdog timer, the same can be
implemented using an external watchdog timer IC circuit.

EMBEDDED SYSTEMS DESIGN UNIT-3


EMBEDDED FIRMWARE INTRODUCTION:

Embedded firmware refers to the control program and or the configuration settings
that an embedded system developer dumps into code(program) memory of the embedded
system.

The embedded firmware can be developed in various methods like

 Write the program in high level languages like Embedded C/C++ using Integrated
Development Environment. (The IDE will contain an editor, compiler, linker,
debugger, simulator etc. Different families of processors/controllers have different
IDE’s)

 Write the program in Assembly Language using the Instructions supported by


application’s target processor/controller.

EMBEDDED FIRMWARE DESIGN APPROACHES

 The embedded firmware is responsible for controlling the various peripherals of the
embedded hardware and generating response in accordance with the functional
requirements of the product.

 The embedded firmware is the master brain of the embedded system. The embedded
firmware imparts intelligence to an Embedded system. It is a onetime process and it
can happen at any stage.

 The product starts functioning properly once the intelligence imparted to the product
by embedding the firmware in the hardware.

 The product will continue serving the assigned task till hardware breakdown occurs or
a corruption in embedded firmware.

 In case of hardware breakdown, the damaged component may need to be replaced and
for firmware corruptions the firmware should be re-loaded, to bring back the
embedded product to the normal functioning.

 The embedded firmware is usually stored in a permanent memory (ROM) and it is non
alterable by end users.

EMBEDDED SYSTEMS DESIGN UNIT-3


Designing Embedded firmware requires understanding of the particular embedded product
hardware (like various component interfacing, memory map details, I/O port details,
configuration and register details of various hardware chips used and some programming
language (either low level Assembly Language or High-level language like C/C++ or a
combination of the two)

 The embedded firmware development process starts with the conversion of the
firmware requirements into a program model using various modeling tools and
implementation of the tasks and action using a language.

The firmware design approaches for embedded product are purely dependent on the
complexity of the functions to be performed and speed of operation required.

There exist two basic approaches for the design and implementation of embedded firmware,
namely;

 The Conventional procedural based Firmware Design (or) Super loop-based approach

 The Embedded Operating System based approach

The decision on which approach needs to be adopted for firmware development is purely
dependent on the complexity and system requirements

The Super loop-based Approach:

The Super loop-based firmware development approach is adapted for applications that are not
time critical and where the response time is not so important (Embedded systems where
missing deadlines are acceptable).

 It is similar to conventional procedural programming where the code is executed task


by task.

 The tasks are executed in a never-ending loop.

 The task listed on top on the program code is executed first and the tasks just below the
top are executed after completing the first task

A typical super loop implementation will look like:

EMBEDDED SYSTEMS DESIGN UNIT-3


1) Configure the common parameter and perform initialization for various hardware
components, memory, registers etc.

2) Start the first task and execute it.

3) Execute the second task.

4) Execute the next task.

5) ….

6) Execute the last defined task.

7) Jump back to the first task and follow the same flow.

Visualisa on of the opera onal sequence list above in terms of a ‘c’ program code as

Void main(){

configura on()

ini aliza ons()

while(1)

task1();

task2();

…..

taskn();

Almost all task in embedded applications are non-ending and are repeated infinitely
throughout the operation.
 By analyzing C code we can see that the task 1 to n are performed one after another

EMBEDDED SYSTEMS DESIGN UNIT-3


and when the last task is executed, the firmware execution is again redirected to task
1 and it is repeated forever in the loop.
 This repetition is achieved by using an infinite loop while(1){} loop.
 This approach is also referred as Super loop based Approach.
Since the task are running inside an infinite loop, the only way to come out of the loop is
either a hardware reset or Interrupt assertion.
 A Hardware reset brings the program execution back to the main loop. Whereas the
interrupt suspend the task execution temporarily and perform the corresponding
interrupt routine and on completion of the interrupt routine it restart the task
execution from the point where it got interrupted.

Example of Super Loop Based Design is Electronic video game toy containing keypad and
display unit.
 The program running inside the product must be designed in such a way that it reads
the key to detect whether user has given any input and if any key press is detected
the graphic display is updated. The keyboard scanning and display updating happens
at a reasonable high rate
 Even if the application misses the key press, it won‘t create any critical issue, rather
it will treated as a bug in the firmware.

Advantages of Super Loop based Design:


 Super Loop based design does not require an OS, since there is no need for
scheduling which task is to be executed and assigning priority to each task.
 In a super Loop based design, the priorities are fixed and the order in which the task
to be executed are also fixed.
 Hence the code for performing these task will be residing in the code memory without
an operating system image.
 This type of design is deployed in low-cost embedded products where the response
time is not time critical.
 Some embedded products demand this type of approach if some tasks itself are
sequential.

EMBEDDED SYSTEMS DESIGN UNIT-3


Drawback of Super Loop based Design:
 Any failure in any part of a single task will affect the total system
– If the program hang up at any point while executing a task, it will remain
there forever and ultimately the product will stop functioning
– Some remedial measures
• Use of Hardware and software Watch Dog Timers (WDTs) helps in
coming out from the loop when an unexpected failure occurs or when
the processor hang up

– May cause additional hardware cost and firmware overhead


 Another major drawback is lack of real timeliness
– 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.
– To identify the key press, you may have to press the key for a sufficiently
long time till the keypad status monitoring task is executed internally. – Lead
to lack of real timeliness.

Embedded Operating System (OS) Based Approach:


The operating System (OS) based approach contains OS, which can be either a General
purpose Operating System (GPOS) or real Time Operating System (RTOS).
 GPOS based design is very similar to the conventional PC based Application
development where the device contain an operating system and you will be creating
and running user applications on top of it
– Examples of Microsoft Windows XP OS are PDAs, Handheld devices,
Portable Devices and point of Sale terminals.
 RTOS based design approach is employed in embedded product demanding Real
Time Responses.
– RTOS respond in a timely and predictable manner to events.
– RTOS contain a real time Kernel responsible for performing pre- emptive
multi-tasking, scheduler for scheduling the task, multiple thread etc.
– RTOS allows a flexible scheduling of system resources like the CPU and
Memory and offer some way to communicate between tasks.

EMBEDDED SYSTEMS DESIGN UNIT-3


– Examples of RTOS are
• Windows CE, pSOS, VxWorks, ThreadX, Micro C/OS II, Embedded
Linux, Symbian etc…

EMBEDDED FIRMWARE DEVELOPMENT LANGUAGES


For embedded firmware development we can use either
– Target processor/controller specific language (Assembly language) or
– Target processor/ controller independent language (High level languages) or
– Combination of Assembly and High-level Language

a) Assembly language based development


Assembly language is human readable notation of machine language whereas machine
language is a processor understandable language. Processor deal only with binaries.

Machine language is a binary representation and it consist of 1sand 0s.

 Machine language is made readable by using specific symbols called ‘mnemonics’.


Hence machine language can be considered as an interface between processor and
programmer.
 Assembly language and machine languages are processor dependent and assembly
program written for one process or family will not work with others.
Assembly language programming is the task of writing processor specific machine code
in mnemonics form, converting the mnemonics into actual processor instructions
(machine language) and associated data using an assembler.

The general format of an assembly language instruc on is opcode followed by the Operand.
Opcode tells what to do and Operand gives the informa on to do the task.

For example consider the instruction MOV A, #30,

• Move the decimal value 30 to the accumulator registerof 8051

• Here MOV A is the opcode and 30 is Operand

• Same instruction in machine language like this 01110100 00011110

• Here the first 8 bit represents opcode MOV A and next 8 bit represent the operand 30.

EMBEDDED SYSTEMS DESIGN UNIT-3


Assembly language instructions are written in one per line.

A machine code program thus consisting of a sequence of assembly language instructions,


where each statement contains a mnemonic.

Each line of assembly language program split into four fields as given below

LABEL OPCODE OPERAND COMMENTS

Label is an optional field.

 A label is an identifier to remembering where data or code is located.


 LABEL is commonly used for representing a memory location, address of a
program, sub-routine, code portion etc.
 The max length of the label differs between assemblers.
 Labels are always suffixed by a colon and begin with a valid character.
 Labels can contain numbers from 0 to 9 and special character.
 Labels are used for representing subroutine names and jump locations in
Assembly language programming.
Assembly language program written in assembly code is saved as .asm file or an .src file.

Any text editor can be used for writing assembly instructions.

Similar to other high level programming, you can have multiple source files called modules in
assembly language programming. Each module is represented by .asm or .src file. This
approach is known as modular programming.

 Modular program is employed when program is too complex or too big.


 Modular programs are usually easy to code, debug and alter.
Conversion of assembly language into machine language is carried out by a sequence of
operations.

Source File to Object file Translation

Translation of assembly code to machine code is performed by assembler. The


assemblers for different target machines are different and it is common that assemblers from
multiple vendors are available in the market for the same target machines. Some assemblers
are supplied by single vendor only. Some assemblers are freely available. Some are commercial

EMBEDDED SYSTEMS DESIGN UNIT-3


and requires license from vendors. A51 Macro Assembler from Keil software is a popular
assembler for 8051 family microcontroller.

Each source module is written in assembly and is stored in .src or .asm file. Each file
can be assembled separately to examine the syntax errors and incorrect assembly instructions.
On assembling of each .src/.asm file a corresponding object file is created with extension .obj.

The object file does not contain the absolute address of where the generated code needs
to be placed on the program memory and hence it is called relocatable segment. It can be placed
at any code memory location and it is responsibility of the linker/loader to assign absolute
address for this module. Absolute address allocation is done at absolute object file creation
stage. Each module can share variables and subroutine among them.

Exporting a variable from a module is done by declaring that variable as PUBLIC in


source module. Importing a variable or a function from a module is done by declaring that
variable or function as EXTRN in the module where it is going to be accessed.

 PUBLIC keyword informs the assembler that the variable / function need to be
exported.
 EXTRN inform that the variable/function needs to be imported from some other
modules.
While assembling a module , on seeing variable/func on with keyword EXTRN , assembler
understand that these variables or func on come from an external module and it proceeds
assembling the en re module without throwing any errors, though the assembler cannot find
the defini on of variables and implementa on of that func on.

Corresponding to a variable /function declared as PUBLIC in a module, there can be one or


modules using these variables/function using EXTRN keyword. For all those modules using
variables or function with EXTRN keyword, there should be one and only one module which

EMBEDDED SYSTEMS DESIGN UNIT-3


export those variables/functions PUBLIC keyword. If more than one module in a project tries
to export variables or functions with the same name using PUBLIC keyword, it will generate
linker errors.

 If a variable or function declared as EXTRN in one or two modules, there should be


one module defining these variables or function and exporting them using PUBLIC
keyword.
 If no module in a project exports the variable or functions which are declared as EXTRN
in other modules it will generate linker warnings or error depending on the error
level/warning level setting of the linker.

Library files creation and usage

Libraries are specially formatted, ordered program collection of object modules that may be
used by the linker at a later time.

 When a linker processes a library, only those object modules in the library that are
necessary to create the program are used. Library files are generated with the extension
‘.lib’.
 Library file is some kind of source code hiding technique. An application can make use
of the various function available in the library without revealing the source code.
Example LIB51 from keil software.

Linker and Locator

Linker and locator is another software utility responsible for “linking the various object
modules in a multi module project and assigning absolute address to each module”.

 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 modules 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.

EMBEDDED SYSTEMS DESIGN UNIT-3


Example: ‘BL51’ from keil so ware.

Object to Hex File Converter

This is the final stage in the conversion of Assembly language to machine understandable
Language.

 Hex file is the representation of the machine code and the hex file is dumped into the
code memory of the processor.
 Hex file representation varies depending on the target processor make.
o For INTEL processor the target hex file format will be ‘Intel HEX’ and for
Motorola, hex file should be in ‘Motorola HEX’ format.
 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.
Example: ‘OH51’ from keil so ware.

Advantage of Assembly Language Based Development

Assembly language based development is the most common technique adopted from the
beginning of the embedded technology development.

Thorough understanding of the processor architecture, memory organization, register set and
mnemonics is very essential for Assembly Language based Development.

 Efficient Code Memory and data Memory Usage (Memory Optimization)

o Since the developer is well versed with the target processor architecture and
memory organization, optimized code can be written for performing operations.
This lead to the less utilization of code memory and efficient utilization of data
memory. Memory is the primary concern in any embedded product.
 High Performance

o Optimized code not only improves the code memory usage but also improve the
total system performance. Though effective assembly coding optimum
performance can be achieved for target applications.

EMBEDDED SYSTEMS DESIGN UNIT-3


 Low level Hardware access

o 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 routine etc. are making use of direct assembly coding since low level
device specific operation support is not commonly avail with most of the high
level language compilers

 Code Reverse Engineering

o Reverse Engineering is the process of understanding the technology behind a


product by extracting the information from the finished product.
o Most of the products employ code memory protection, if it may be possible to
break the memory protection and read the code memory, it can easily be
converted into assembly code using disassembler program for the target
machine.
Drawbacks of Assembly Language based Development

 High Development time

o Assembly language programs are much harder to program than high level
languages.
o Developer must have thorough knowledge of architecture, memory
organization and register details of target processor in use.
o Learning the inner details of the processor and its assembly instructions are
high time consuming and it create delay impact in product development.
– Solution: Use a readily available developer who is well versed in target
processor architecture assembly instructions.
o Also more lines of assembly code are required for performing an action which
can be done with a single instruction in a high level language like C.
 Developer Dependency

o There is no common rule for developing assembly language based applications


whereas all high level language instructs certain set of rules for application
development.
o In Assembly language programming, the developers will have the freedom to

EMBEDDED SYSTEMS DESIGN UNIT-3


choose the different memory locations and registers.
o Upgrading an assembly program or modifying it on a later stage is very difficult.
o Well documentation of assembly language is a solution for reducing the
developer dependency in assembly language programming
o If the code is too complex and large, documenting all lines of code may not be
productive.

 Non- Portable

o Target applications written in assembly instructions are valid only for that
particular family of processors. Example - Application written for Intel X86
family of processors. It cannot be reused for other target processors. If the target
processor changes, a complete rewriting of the application using assembly
instructions for the new target processor is required.

b) High Level Language Based Development

Any High level language with a supported cross compilers for the target processor can be used
for embedded firmware development.

 Cross Compilers are used for converting the application development in high level
language into target processor specific assembly code.

 Most commonly used language is C. C is well defined easy to use high level language
with extensive cross platform development tool support.

EMBEDDED SYSTEMS DESIGN UNIT-3


The program written in any of the high level language is saved with the corresponding language
extension. Any text editor provided by IDE tool supporting the high level language in use can
be used for writing the program.
Most of the high level language support modular programming approach and hence you can
have multiple source files called modules written in corresponding high level language. The
source file corresponding to each module is represented by a file with corresponding language
extension.
Translation of high level source code to executable object code is done by a cross compiler.
The cross compiler for different high level language for same target processor are different.
Without cross-compiler support a high level language cannot be used for embedded firmware
development.
Example C51 Compiler from Keil.

Advantages of High Level Language based Development

 Reduced Development Time:

o Developers requires less or little knowledge on the internal hardware details


and architecture of the target processor.

o Syntax of high level language and bare minimal knowledge of memory


organization and register details of target processor are the only pre- requisites
for high level language based firmware development.

o With High level language, each task can be accomplished by lesser number of
lines of code compared to the target processor specific assembly language
based development

 Developer Independency:

o The syntax used by most of the high level languages are universal and a program
written in high level language can be easily be understood by a second person
knowing the syntax of the language.

o High level language based firmware development makes the firmware,


developer independent.

o High level language always instruct certain set of rules for writing code and

EMBEDDED SYSTEMS DESIGN UNIT-3


commenting the piece of code

 Portability:

o Target applications written in high level languages are converted to target


processor understandable format by a cross compiler.

o An application written in high level language for a particular target processor


can be easily converted to another target processor with little effort by simply
recompiling the code modification followed by the recompiling the application
for the required processor. This makes the high level language applications are
highly portable.

Limitations of High level language based Development

 Some cross compilers avail for the high level languages may not be so efficient
ingenerating optimized target processor specific instructions

 Target images created by such compilers may be messy and no optimized in terms of
performance as well as code size.

3. Mixing Assembly and High level Language

High level language and assembly languages are usually mixed in three ways

• Mixing assembly language with high level language

• Mixing high level language with Assembly

• In line assembly programming

Mixing Assembly Language with High level Language (Assembly Language with ‘C’)

Assembly routines are mixed with C in situations where entire program is written in C and the
cross compiler in use do not have built in support for implementing certain features like
Interrupt Service Routine or if the programmer want to take the advantage of speed and
optimized code offered by machine code generated by handwritten assembly rather than cross
compiler generated machine code.

EMBEDDED SYSTEMS DESIGN UNIT-3


When accessing certain low level hardware, the timing specification may be very critical and a
cross compiler generated binary may not be able to offer the required time specifications
accurately. Writing the hardware access routine in processor specific assembly language and
invoking it from C is the most advised method to handle such situations.

Mixing C and Assembly is little complicated in the sense. 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 the assembly routine is invoked from the C code. These are
cross compiler dependent. There is no universal rule for it. You must get the information from
the documentation of cross compiler you are using different cross compilers implement these
features in different ways depending upon the general purpose registers and the memory
supported by the target processor Example

1. Write a simple function in C that passes parameters and return values the way you want your
assembly routine to

2. Use the SRC directive (#pragma SRC) so that compiler generate an SRC file instead of .OBJ
file

3. Compile the C code. Since the SRC directive is specified the .SRC file is generated. The
.SRC file contain the assembly code generated for the C code you wrote

4. Rename .SRC to .A51 file

5. Edit .A51 file and insert the assembly code you want to execute in the body of the assembly
function shell included in the .A51 file.

#pragma src
Unsigned char my_assembly_func(unsigned int argument){
return (argument+1);
}

The special compiler directive SRC generates the Assembly code corresponding to the ‘C’
function and each line of the source code is converted to the corresponding Assembly
instruction. You can easily identify the Assembly code generated for each line of the source
code since it is implicitly mentioned in the generated .SRC file. By inspecting this code
segments you can find out which register are used for holding the variables ofthe ‘C’ function
and you can modify the source code by adding the assembly routine you want.

EMBEDDED SYSTEMS DESIGN UNIT-3


Mixing High level Language with Assembly Language (‘C’ with Assembly Language)
Mixing the code written in high level language like C and assembly language is useful in the
following scenarios
• The source code is already available in Assembly language and a routine written in a high
level language like C need to be included to the existing code.
• The entire code is planned in Assembly code for various reasons like optimized code,
optimal performance, efficient code memory utilization and proven expertise in handling
the Assembly, etc. But some portions of the code may be very difficult and tedious to
code in
Assembly

• To include built in library functions written in C language provided by the cross compiler

Most often the functions written in C use parameter passing to the function and returns values
to the calling function. By mixing C with Assembly
• How parameters are passed to the function

• How values are returned from the function

• How the function is invoked from the assembly language environment

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.
Example Keil C51 cross compiler.C51 allows passing of maximum of three arguments through
general purpose registers R2 to R7. If three arguments are char variables, they are passed to the
functions using registers R7,R6, andR5. If the parameters are int variables they are passing
using register pairs(R7,R6),(R5,R4) and (R4,R3). If the number of arguments is greater than
three, the first three arguments are passed through registers and the rest is passed through fixed
memory locations. Return values are usually passed through fixed memory locations. R7 is
used for returning char value and register pair (R7,R6) used for returning int values.

Inline Assembly

This is another technique for inserting target processor/controller specific assembly


instructions at any location of source code written in high level language C. This avoids the

EMBEDDED SYSTEMS DESIGN UNIT-3


delay in calling an assembly routine from a C code. Special keywords are used to indicate that
the start and end of the Assembly instructions. The keywords are cross compiler specific. C51
uses #pragma asm and #pragma endasmto indicate a block of code written in assembly.
Eg: #pragma asm

MOV A,#13H

#pragma endasm

EMBEDDED SYSTEMS DESIGN UNIT-3

You might also like