Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $9.99/month after trial. Cancel anytime.

Assembly Language Programming: ARM Cortex-M3
Assembly Language Programming: ARM Cortex-M3
Assembly Language Programming: ARM Cortex-M3
Ebook400 pages3 hours

Assembly Language Programming: ARM Cortex-M3

Rating: 0 out of 5 stars

()

Read preview

About this ebook

ARM designs the cores of microcontrollers which equip most "embedded systems" based on 32-bit processors. Cortex M3 is one of these designs, recently developed by ARM with microcontroller applications in mind. To conceive a particularly optimized piece of software (as is often the case in the world of embedded systems) it is often necessary to know how to program in an assembly language.

This book explains the basics of programming in an assembly language, while being based on the architecture of Cortex M3 in detail and developing many examples.

It is written for people who have never programmed in an assembly language and is thus didactic and progresses step by step by defining the concepts necessary to acquiring a good understanding of these techniques.

LanguageEnglish
PublisherWiley
Release dateMar 4, 2013
ISBN9781118563342
Assembly Language Programming: ARM Cortex-M3

Related to Assembly Language Programming

Related ebooks

Software Development & Engineering For You

View More

Related articles

Reviews for Assembly Language Programming

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Assembly Language Programming - Vincent Mahout

    Preface

    To be able to plan and write this type of book, you need a good work environment. In my case, I was able to benefit from the best working conditions for this enterprise. In terms of infrastructure and material, the Institut National de Sciences Appliquées de Toulouse, France (Toulouse National Institute of Applied Sciences), and in particular their Electrical and Computer Engineering Department, has never hesitated to invest in computer systems engineering, so that the training of our future engineers will always be able to keep up with rapid technological change. I express my profound gratitude to this institution. These systems would not have amounted to much unless, over the years, there was an educational and technical team bringing their enthusiasm and dynamism to implement them. The following pages also contain the hard work of Pascal Acco, Guillaume Auriol, Pierre-Emmanuel Hladik, Didier Le Botlan, José Martin, Sébastien Di Mercurio and Thierry Rocacher. I thank them sincerely. Two final respectful and friendly nods go to François Pompignac and Bernard Fauré who, before retirement, did much work to fertilize this now thriving land.

    When writing a book on the assembly language of a μprocessor, we know in advance that it will not register in posterity. By its very nature, an assembly language has the same life expectancy as the processor it supports – perhaps 20 years at best. What’s more, this type of programming is obviously not used for the development of software projects and so is of little consequence.

    Assembly language programming is, however, an indispensable step in understanding the internal functioning of a µprocessor. This is why it is still widely taught in industrial computer training, and particularly in training engineers. It is clear that a good theoretical knowledge of a particular assembly language, combined with a practical training phase, allows for easier learning of other programming languages, whether they are the assembly languages of other processors or high-level languages.

    Thus, this book intends to dissect programming in the assembly language of a μcontroller constructed around an ARM Cortex-M3 core. The choice of this µcontroller rests on the desire to explain:

    – a 32-bit processor: the choice of the ARM designer is essential in the 32-bit world. This type of processor occupies, for example, 95% of the market in the domain of mobile telephony;

    – a processor of recent conception and architecture: the first licenses for Cortex-M3 are dated October 2004 and those for STMicroelectronics’ 32-bit flash microcontrollers (STM32) were given in June 2007;

    – a processor adapted to the embedded world, based on the observation that 80% of software development activity involves embedded systems.

    This book had been written to be as generic as possible. It is certainly based on the architecture and instruction set of Cortex-M3, but with the intention of explaining the basic mechanisms of assembly language programming. In this way we can use systematically modular programming to show how basic algorithmic structures can be programmed in assembly language. This book also presents many illustrative examples, meaning it is also practical.

    Chapter 1

    Overview of Cortex-M3 Architecture

    A computer program is usually defined as a sequence of instructions that act on data and return an expected result. In a high-level language, the sequence and data are described in a symbolic, abstract form. It is necessary to use a compiler to translate them into machine language instructions, which are only understood by the processor. Assembly language is directly derived from machine language, so when programming in assembly language the programmer is forced to see things from the point of view of the processor.

    1.1. Assembly language versus the assembler

    When executing a program, a computer processor obeys a series of numerical orders – instructions – that are read from memory: these instructions are encoded in binary form. The collection of instructions in memory makes up the code of the program being executed. Other areas of memory are also used by the processor during the execution of code: an area containing the data (variables, constants) and an area containing the system stack, which is used by the processor to store, for example, local data when calling subprograms. Code, data and the system stack are the three fundamental elements of all programs during their execution.

    It is possible to program directly in machine language – that is, to write the bit instruction sequences in machine language. In practice, however, this is not realistic, even when using a more condensed script thanks to hexadecimal notation (numeration in base 16) for the instructions. It is therefore preferable to use an assembly language. This allows code to be represented by symbolic names, adapted to human understanding, which correspond to instructions in machine language. Assembly language also allows the programmer to reserve the space needed for the system stack and data areas by giving them an initial value, if necessary. Take this example of an instruction to copy in the no. 1 general register of a processor with the value 170 (AA in hexadecimal). Here it is, written using the syntax of assembly language studied here:

    EXAMPLE 1.1. – A single line of code

            MOV Rl, #OxAA ; copy (move) value 170 (AA in hexa)

                              ; in register Rl

    The same instruction, represented in machine language (hexadecimal base), is written: E3A010AA. The symbolic name MOV takes the name mnemonic. R1 and #0xAA are the arguments of the instruction. The semicolon indicates the start of a commentary that ends with the current line.

    The assembler is a program responsible for translating the program from the assembly language in which it is written into machine language. Upon input, it receives a source file that is written in assembly language, and creates two files: the object file containing machine language (and the necessary information for the fabrication of an executable program), and the printout assembly file containing a report that details the work carried out by the assembler.

    This book deals with assembly language in general, but focuses on processors based on Cortex-M3, as set out by Advanced RISC Machines (abbreviated to ARM). Different designers (Freescale, STmicroelectronics, NXP, etc.) then integrate this structure into µcontrollers containing memory and multiple peripherals as well as this processor core. Part of the documentation regarding this processor core is available in PDF format at www.arm.com.

    1.2. The world of ARM

    ch1-page2-01.gif

    ARM does not directly produce semiconductors, but rather provides licenses for microprocessor cores with 32-bit RISC architecture.

    This Cambridge-based company essentially aims to provide semiconductors for the embedded systems market. To give an idea of the position of this designer on this market, 95% of mobile telephones in 2008 were made with ARM-based processors. It should also be noted that the A4 and A5 processors, produced by Apple and used in their iPad graphics tablets, are based on ARM Cortex-Type A processors.

    Since 1985 and its first architecture (named ARM1), ARM architectures have certainly changed. The architecture upon which Cortex-M3 is based is called ARMV7-M.

    ARM’s collection is structured around four main families of products, for which many licenses have been filed¹:

    – the ARM 7 family (173 licenses);

    – the ARM 9 family (269 licenses);

    – the ARM 10 family (76 licenses);

    – the Cortex-A family (33 licenses);

    – the Cortex-M family (51 licenses, of which 23 are for the M3 version);

    – the Cortex-R family (17 licenses).

    1.2.1. Cortex-M3

    ch1-page3-01.gif

    Cortex-M3 targets, in particular, embedded systems requiring significant resources (32-bit), but for these the costs (production, development and consumption) must be reduced. The first overall illustration (see Figure 1.1) of Cortex-M3, as found in the technical documentation for this product, is a functional diagram. Although simple in its representation, every block could perplex a novice. Without knowing all of the details and all of the subtleties, it is useful to have an idea of the main functions performed by different blocks of the architecture.

    1.2.1.1. Executive units

    These units make up the main part of the processor – the part that is ultimately necessary to run applications and to perform them or their software functions:

    – CM3CORE: This is the core itself. This unit contains different registers, all of the read/write instruction mechanisms and data in the form of the arithmetical and logical unit for the proper execution of different instructions. The functioning of this block will be explained in detail in Chapter 2. It is necessary to understand its mechanism in order to write programs in assembly language.

    – Nested Vector Interrupt Controller (NVIC): Cortex-M3 is intended to be embedded in a µcontroller, which includes peripheral units to allow interfacing with the outside world. These units can be seen as independent micromachines. The exchanges between them and Cortex-M3 must consequently be rhythmic and organized so that the sequence of tasks complies with rules (the concept of priorities) and determinism set in advance by the programmer. NVIC plays the role of director. It is in charge of receiving, sorting and distributing the different interrupt requests generated by the collection of µcontroller units. It also manages events that threaten the smooth running of the code being executed (reset, memory bus problem, division by 0, etc.).

    Figure 1.1. Cortex-M3 functional diagram

    ch1-fig1.1.gif

    – Memory Protection Unit (MPU): This block is optional – a designer using Cortex-M3 to make their µcontroller can choose not to implement this function. This block allows the allocation of specific read and/or write privileges to specific memory zones. In this way, when different independent software tasks are executed in parallel (or more precisely in sharing the common resources of the processor), it is possible to allocate a memory zone to each task that is inaccessible to the other tasks. This mechanism therefore allows programmer to secure memory access. It usually goes hand-in-hand with the use of an operating system (real-time or otherwise) for the software layer.

    – Bus matrix: This unit is a kind of gigantic intelligent multiplex. It allows connections to the external buses:

    - the ICode bus (32-bit AHB-Lite type²) that carries the memory mappings allocated to the code and instructions;

    - the DCode bus (also 32-bit AHB-Lite type) that is responsible for reading/writing in data memory zones;

    - the System bus (again 32-bit AHB-Lite type), which deals with all system space access;

    - the Private Peripheral Bus (PPB): all peripherals contained in the µcontroller are added to the Cortex-M3 architecture by the designer. ARM designed a specific bus to allow exchanges with peripherals. This bus contains 32 bits, but in this case it is the Advanced Peripheral Bus (APB) type. This corresponds to another bus protocol (which you may know is less efficient than AHB type, but it is more than sufficient for access to peripheral units). It should be noted that the bus matrix plays an important role in transmitting useful information to development units, which are mentioned in the next section.

    1.2.1.2. Development units

    The development of programs is an important and particularly time-consuming step in the development cycle of an embedded application. What is more, if the project has certification imperatives, it is necessary that tools (software and/or material) allowing maximum monitoring of the events occurring in each clock cycle are at its disposition. In Cortex-M3, the different units briefly introduced below correspond to these monitoring functions. They are directly implanted in the silicon of the circuit, which allows them to use these development tools at a material level. An external software layer is necessary, however, to recover and process the information issued by these units. The generic idea behind the introduction of hardware solutions is to offer the programmer the ability to test and improve the reliability of (or certify) his or her code without making any changes. It is convenient (and usual) to insert some print (Hello I was here) into a software structure to check that the execution passes through this structure. This done, a code modification is introduced, which can modify the global behavior of the program. This is particularly true when time management is critical for the system, which, for embedded systems controlled by a µcontroller, is almost always the case. The units relating to monitoring functions in Cortex-M3 include:

    – Flash Patch and Breakpoint (FPB): the FPB is the most basic function for this process. It is linked to the concept of a stopping point (breakpoint), which imposes a stop on a line of code (that is to say, an instruction in the case of assembly language) located beforehand. This unit is used to mark instructions so that when they come into effect, the processor puts itself into a particular mode of operation: debug mode. Development software (and other pieces of software that use it) can therefore observe the state of the processor and directly influence the running of the program in progress.

    – Data Watchpoint and Trace (DWT): the concept of a point of observation is the counterpart to the concept of a stopping point for the data. The DWT stops the program running when it works on marked data rather than a marked instruction. This action can be in reading, writing, passing on values, etc. The unit can also send requests to the ITM and ETM units.

    – Embedded Trace Macrocell (ETM): the concept of trace includes the capacity for the hardware to record a sequence of events during program execution. The recovery of these recordings allows the programmer to analyze the running of the program, whether good or bad. In the case of ETM, only information on instructions is stored in a first in, first out (FIFO)-type structure. As with the MPU unit, this unit is optional.

    – Instrumentation Trace Macrocell (ITM): this unit also allows the collection of trace information on applications (software, hardware or time). The information is more limited than with the ETM unit but is nevertheless very useful in isolating a vicious bug. This is especially true if the optional ETM is not present.

    – Advanced High-performance Bus Access Port (AHB-AP): this is an (Input/Output) port within Cortex-M3 that is designed to debug. It allows access to all records and all addressable memory space. It has priority in the arbitration policies of the bus matrix. This unit is connected upstream by the Serial Wire JTAG (Joint Test Action Group) port (SW/JTAG), which is the interface (with its physical layers) that connects to the outside world, equipped with its part of the JTAG probe. The JTAG protocol is a standardized protocol used by almost all semiconductor manufacturers.

    – Trace Port Interface Unit (TPUI): the TPUI plays the same role in trace functions as the SW/JTAG plays in debug functions. Its existence is principally linked to the fact that it is necessary to sort the external world recordings collected by the ITM. There is an additional challenge, however, when an ETM unit is present, as it must also manage the data stored there. Its secondary role is therefore to combine and format this double stream of data before transmitting it to the port. In the outside world, it is necessary to use a Trace Port Analyzer to recover the data.

    1.2.2. The Cortex-M3 core in STM32

    ch1-page3-01.gif

    As already stated, ARM does not directly make semiconductors. The µcontroller core designs are sold under license to designers, who add all of the peripheral units that make up the interface with the exterior. For example, the STM32 family of µcontrollers, made by STMicroelectronics, contains the best selling µcontrollers using Cortex-M3. Like any good family of µcontrollers, the STM32 family is available in many versions. In early 2010, the STMicroelectronics catalog offered the products shown in Figure 1.2.

    Figure 1.2. STM32 family products

    ch1-fig1.2.gif

    1.2.2.1. Functionality

    The choice of the right version of µcontroller can be a significant step in the design phase of a project: based on the needs (in terms of function, number of Input/Output, etc.) but also on the proper constraints (cost, consumption, size, etc.), each version of the processor will be more or less well adapted

    Enjoying the preview?
    Page 1 of 1