Académique Documents
Professionnel Documents
Culture Documents
Embedded Programs
3.1 Introduction
3.1.1 High-Level Code
3.1.2 Machine Language
3.1.3 Assembly Language Code
3.2 Program translation process
3.3 Representation of programs and their execution flow
3.4 Fundamentals of assembly language and linking
3.4.1 Directives
3.4.2 Labels in Instructions
3.4.3 Source Executable Code
3.5 Mapping tasks in compilation
1
3.1 INTRODUCTION
Programming provides us with the flexibility to use
the same hardware core for different applications.
A device whose performance can be modified through
some sort of instructions is called a programmable or
configurable device
Most I/O ports and peripherals are examples of these
blocks; their configuration should be part of our
program design
Assembly programming for the MSP430
microcontrollers is introduced in this chapter
The two most popular assemblers for the MSP430
are the IAR assembler by Softbaugh and Code
Composer Studio (CCS) from Texas Instruments 2
3.1 INTRODUCTION
Programs are written using a programming language
with specific rules of syntax. These languages are
found in three main levels:
Machine language,
Assembly language, and
High level language
3
3.1 INTRODUCTION
oThe program toggles on and off an LED connected to
pin 0 of port 1 (P1.0) of the microcontroller, as
illustrated in Figure 3.1. Toggling is achieved by
changing the voltage level at the pin
4
3.1 INTRODUCTION
The algorithm to achieve this goal is described by
the flowcharts of Figure 3.2, where (a) illustrates
the process in its general terms, and (b) expands
the different steps in more detailed actions.
Figure 3.2 Flow diagram for the blinking LED: a) General Concept; b) Expanded flow diagram 5
3.1.1 HIGH-LEVEL CODE
Figure 3.3 shows a code in C language which executes
the desired task.
Readers with some proficiency in C will understand
this source code without problem and associate it with
the flow graph. The compiler takes care of the “set up
stack” step.
8
3.1.2 MACHINE LANGUAGE
Here, each line represents an instruction machine
language is not user friendly and hence the need of
other programming levels
Instructions for the MSP430 CPU consist of one, two
or three 16-bit words.
The leading one is the instruction word, with
information relative to the instruction and operands.
9
Figure 3.4 Executable machine language code for the example of Figure 3.1
3.1.3 ASSEMBLY LANGUAGE CODE
The first step toward a friendlier syntax was the assembly
language, compiled with assemblers
instruction is now a machine instruction encoded in amore
“human like” form.
Translating a machine instruction into its assembly form is to
disassembly and the software tool for this task is a disassembler.
Figure 3.5 shows the assembly and hex versions of the
executable code
10
Figure 3.5 Assembly version for Figure 3.4-Hex machine version shown for comparison
3.2 PROGRAM TRANSLATION PROCESS
High level languages arose to allow people to
program without having to know all the hardware
details of the computer
An important feature is that an instruction in a high
level language would typically correspond to several
instructions in machine code
mapping of each of the high level language
instructions into machine code and their
corresponding use of the functional units
the programmer has in effect lost the ability to use
the hardware in the most efficient way
For most programmers, human-friendly
characteristics of high level language for computer
programming make it a “natural choice” for
embedded programming as well 11
3.2 PROGRAM TRANSLATION PROCESS
C language has features that make it attractive for this
sort of applications
Perhaps the most important one is that it allows the
programmer to manipulate memory locations with very
little overhead added
Since input/output units can be treated as memory, C will
also allow the programmer to control them directly
In general, you use C language for embedded systems
when one or more of the following reasons apply:
tight control of each and every resource of the architecture is
not necessary;
efficiency in the use of resources such as memory and power are
not a concern;
ease of software reuse;
company policy or user requirements;
lack of knowledge of assembly language and of the particular
architecture. 12
3.2 PROGRAM TRANSLATION PROCESS
Figure 3.6 LEDs and Switches Interfaced to Port 1 and Port 0 of 8051 Microcontroller
Note that just like the line in the main body of the program,
every line of a C program must end with a semicolon (i.e. ;)
except in some special occasions.
Alternative version of this program using directive #define.
#include <reg66x.h>
#define ON 0xFF
void main(void)
{
P1 = ON;
}
In the above example the value of “ON” is defined as 0xFF
using:
#define ON 0xFF
and the statement in the body of the program is then written as:
P1 = ON; 25
3.3 REPRESENTATION OF PROGRAMS AND THEIR
EXECUTION FLOW
29
3.3 REPRESENTATION OF PROGRAMS AND THEIR
EXECUTION FLOW
Type Size Range
unsigned char 1 byte 0 to 255
(signed) char 1 byte -128 to 127
unsigned int 2 bytes 0 to 65535
(signed) int 2 bytes -32768 to 32767
bit 1 bit 0 or 1 (RAM bit-addressable part of memory only)
sbit 1bit 0 or 1 (SFR bit-addressable part of memory only)
sfr 8bit RAM addresses 80h-FFh only
37
3.3 REPRESENTATION OF PROGRAMS AND THEIR
EXECUTION FLOW
44
3.3 REPRESENTATION OF PROGRAMS AND THEIR
EXECUTION FLOW
#include <reg66x.h>
#define ON 0xFF
#define OFF 0x00
sbit S0 = P0^0;
void DELAY(void);
void main(void)
{
unsigned char N;
for(;;)
{
while(S0)
{
for(N=0;N<10;N++)
{
P1=ON;
DELAY();
P1=OFF;
DELAY();
}
}
}
}
45
Note that because there is no way to indirectly address registers in the 8051, addresses
for sbit type variables must be declared outside of any functions within the code
3.4 FUNDAMENTALS OF ASSEMBLY LANGUAGE AND
LINKING
Although simpler to read than machine form, the
code of Figure 3.5, which is a pure assembly listing,
still has unfriendly notes.
For example, the user needs the memory map to
identify addresses 0x0022 and 0x0021, and
knowledge of machine instruction lengths to know
how many bytes the PC should jump.
A friendlier version is found in Figure 3.7, written
with IAR assembler syntax. This is part of a
complete source file.
Each line in the source file is a source statement.
46
3.4 FUNDAMENTALS OF ASSEMBLY LANGUAGE AND
LINKING
47
Figure 3.8 Example of a list file line
3.4 FUNDAMENTALS OF ASSEMBLY LANGUAGE AND
LINKING
3.4.1 Directives
Directives are for the assembler only
They do not translate into machine code or data to be
loaded into the microcontroller memory
They serve to organize the program, create labels
and symbolic names, and so on
Directives depend on the assembler, but instructions
belong to the CPU architecture and are independent
of the assembler
• Comments: Anything on a line after and including a
semicolon (;) is a comment
• Names: These are names given to numbers, expressions,
hardware devices, and so on
• Labels: These always start on the first column
• Directives EQU, #define, #include and ORG: The code
50
example of Figure 3.7 introduces some directives
3.4 FUNDAMENTALS OF ASSEMBLY LANGUAGE
AND LINKING
Example 3.8 Even if the source file is not yet
complete, let us look at what happens when the
assembly code of Figure 3.7 is processed.
Labels DELAY and LED, as well as the symbolic
name COUNTER are identified as 50000 (=C350h), 1
(=01h), and R15, respectively.
All other symbolic names found, such as P1DIR and
P1OUT, are defined in the header file.
For other labels, the value will be generated, and the
ORG directive allows us to understand the process.
ORG 0F800h is used just before the beginning of the
source code
Therefore the first machine language instruction
4031 0300 following the directive goes to that
51
address, 0F800h
3.4 FUNDAMENTALS OF ASSEMBLY LANGUAGE AND
LINKING