You are on page 1of 32

Embedded Systems

Department of Electrical and Computer Engineering


MWU University

Lecture 3
Chapter 3
Embedded Programs

 Program translation process


 Representation of programs and their
execution flow
 Fundamentals of assembly language and
linking
 Mapping tasks in compilation

2
Program translation process

3
Source file

4
Pre-processor
 The pre-processor processes the source code before it
continues with the compilation stage.
 The pre-processor
• Resolves #define statements (constants, variable types, macros)
• Concatenates #include files and source file into one large file
• Processes #ifdef - #endif statements
• Processes #if - #endif statements
 Specifically for embedded systems the pre-processor
also processes vendor-specific directives (non-ANSI)
• #pragma

5
Compiler
 The compiler turns source code into machine code
packaged in object files.
 Common file format are object file format (COFF) or
the extended linker format (ELF).
 A cross-compiler produces object files that will then
be linked for the target instead of the computer
running the compiler

6
Linker
 The linker performs the following
 It combines object files by merging object file sections.
 .text section for code
 .data section for initialized global variables
 .bss section for uninitialized global variables
 It resolves all unresolved symbols.
 External variables
 Function calls
 Reports errors about unresolved symbols.
 Appends the start-up code (see next slide)
 Provide symbolic debug information
 The linker produces a relocatable file. For standard operating systems
with a dynamic loader, the processes is now finished - not
so for embedded systems which need absolutely located binaries.

7
Startup Code
 Startup is a small fragment of assembly code that
prepares the machine for executing a program
written in a high-level language.
 Tasks of the startup code
 Disable all interrupts
 Initialize stack pointers for software stack
 Initialize idata sections
 Zero all uninitialized data areas in data memory (ANSI
standard)
 Call loop: main(); goto loop;

8
Relocator
 The relocator converts a relocatable binary into an
absolutely located binary.
 The relocator is guided by a linker script that specifies:
 Program and data memory for the target part
 Stack size and location
 Logical sections used in the source code to place code and
data
 The relocator then produces an ‘executable’, that is
ready for deployment on the target or for the
simulator.

9
Linker File for MPLINK
 The linker directives fall into four basic categories. Since
MPLINK combines the linker and relocator in one, there is
no clean separation.
 Command line directives
 LIBPATH: Search path for library and object files.
 LKRPATH: Search path for linker command files.
 FILES: Additional files to be linked.
 INCLUDE: Additional linker command files to be included.
 Stack definition directive.
 STACK SIZE=allocSize [RAM=memName]
 Specifies the size and location of the software stack.

10
Linker File for MPLINK
 Memory region definition directives.
 CODEPAGE NAME=memName START=addr END=addr [PROTECTED]
[FILL=fillvalue]
 Specifies a ROM directive that is used for program code, initialized data
values,constant data values, and external memory
 PROTECTED specifies that it can only by explicit request
 Useful for reflashing on the fly.
 DATABANK NAME=memName START=addr END=addr [PROTECTED]
 Specifies a RAM directive that is used for volatile memory.
 Useful for correlated data tables.
 Logical sections definition directives.
 SECTION NAME=secName [ROM=memName | RAM=memName]
 The code or data specified using the #pragma directive will then be located in thespecified
memory area.

11
Sample Linker File

12
Bin2Hex
 The executable still has to be transferred to the
target via a serial line (or even Ethernet with
applicable boot loaders).
 For standard compliance, the binary is
converted into an ASCII representation useful
to PROM programmers and emulators.
 A number of standards exists, Intel HEX is
widespread.
 Each line in an Intel HEX file contains one HEX record.

13
Example Program after Bin2Hex

14
Representation of programs and their execution flow

 Programs in embedded system can be


represented by
 Data Flow Diagram
 State Diagram
 Flow Charts
 Pseudo Code

15
Data Flow Diagram

 Shows the processes and what data is


transferred between different functions.

16
State Diagram

 represent each possible state of software and


what inputs case it to change to another state.

17
Flow Charts

 Flow charts are more detail and time consuming


diagrams.
 Flow Charts are good where single thread
execution need to be described

18
Pseudo Code

 simplicity and robustness.


 high level or logical description of each program step
If serial data available, read the data,
do some proprietary stuff I cannot reveal, then
store the data in a flrst in, flrst out (FIFO) buffer.
If the buffer gets too full, and if XOFF was not sent yet,
send XOFF to the host.
If there are data in the FIFO buffer, and if the output interface
is ready, send a byte from the FIFO buffer to the output.
If the buffer gets close to empty, and if XON was not sent yet,
send XON to the host

19
Fundamentals of assembly language and linking
Basic Elements of Assembly Language

 Integer constants
 Integer expressions
 Character and string constants
 Reserved words and identifiers
 Directives and instructions
 Labels
 Mnemonics and Operands
 Comments
 Examples
20
Integer Constants
 [{+|-}] digits [radix]
 Optional leading + or – sign
 Binary, decimal, hexadecimal, or octal digits
 Common radix characters:
 h – hexadecimal
 d – decimal
 b – binary
 r – encoded real
 Examples: 30d, 6Ah, 42, 1101b
 Hexadecimal beginning with letter: 0A5h
21
Integer Expressions
 Operators and precedence levels:

Examples:

22
Real Number Constants
 [{+|-}] integer.[integer] [exponent]
 Exponent: E[{+|-}]integer
Examples: 2., +3.0, -44.2E+05
 Encoded Reals
 IEEE floating-point format (e.g. 3F800000r)

23
Character and String Constants
 Enclose character in single or double quotes
 'A', "x“
 ASCII character = 1 byte
 Enclose strings in single or double quotes
 "ABC“
 'xyz‘
 Each character occupies a single byte
 Embedded quotes:
 “This isn’t a test"

24
Reserved Words and Identifiers
 Reserved words cannot be used as identifiers
 Instruction mnemonics (MOV), directives (.code),
type attributes (BYTE, WORD), operators (=),
predefined symbols (@data)
 Identifiers
 1-247 characters, including digits
 not case sensitive
 first character must be a letter, _, @, ?, or $
Examples: var1, Count, $first, _main, @@myfile

25
Directives
 Commands that are recognized and acted
upon by the assembler
 Used to declare code, data areas, select memory
model, declare procedures, etc.
 not case sensitive
 Different assemblers have different directives

26
Instructions
 An instruction contains:
 Label (optional)
 Mnemonic (required)
 Operand (depends on the instruction)
 Comment (optional)

27
Labels
 Act as place markers
 marks the address (offset) of code and data
 Follow identifer rules
 Data label
 must be unique
 example: count DWORD 100 (not followed by colon)
 Code label
 target of jump and loop instructions
 example: target: (followed by colon)
….
jmp target
28
Mnemonics and Operands
 Instruction Mnemonics
 memory aid
 examples: MOV, ADD, SUB, MUL, INC, DEC
 Operands
 constant (immediate value), 96
 constant expression, 2+4
 Register, eax
 memory (data label), count
 Constants and constant expressions are often called
immediate values
29
Comments
 Comments are good!
 explain the program's purpose
 when it was written, and by whom
 revision information
 tricky coding techniques
 application-specific explanations
 Single-line comments
 begin with semicolon (;)
 Multi-line comments
 begin with COMMENT directive and a programmer-chosen
character
 end with the same programmer-chosen character 30
Assemble- Link Execute Cycle
 The following diagram describes the steps
from creating a source program through
executing the compiled program.
 If the source code is modified, Steps 2
through 4 must be repeated.

31
Map File
 Information about each program segment:
 starting address
 ending address
 size
 segment type

32

You might also like