Preface Preface
This book introduces the concepts of how computer hardware works from a programmer's point of view. A programmer's job is to design a sequence of instructions that will cause the hardware to perform operations that solve a problem. This book looks at these instructions by exploring how C/C++ language constructs are implemented at the instruction set architecture level.
The specific architecture presented in this book is the ARM® as implemented on the Raspberry Pi®. The operating system is Raspbian, which is based on a Linux kernel and includes a GNU programming environment.
The basic guidelines I followed in creating this book are:
Understanding how the hardware works helps you to become a better programmer.
Learning is easier if it builds upon concepts you already know.
“Real world” hardware and software make a more interesting platform for learning theoretical concepts.
The tools used for teaching should be inexpensive and readily available.
In general, I believe that most applications should be written in high-level languages, not in assembly language. Even low-level programming is probably best done in C most of the time. C was introduced in 1978 specifically for low-level programming. C code is usually much easier to write and to maintain than assembly language. C compilers have evolved to a point where they produce better machine code than all but the best assembly language programmers can. In addition, the hardware technology has increased such that there is seldom a significant advantage in writing the most efficient machine code. In short, it is hardly ever worth the effort to write in assembly language.
You might well ask why you should study assembly language, given that I think you should use higher-level languages most of the time. Well, I believe very strongly that the best programmers have a good understanding of how computer hardware works. I think this principle holds in most fields: the best drivers understand how automobiles work; the best musicians understand how their instrument works; etc.
So this is not a book on how to write programs in assembly language. Most of the programs you will be asked to write will be in assembly language, but they are very simple programs intended to illustrate the concepts. I believe that this book will help you to become a better programmer in any programming language, even if you never write another line of assembly language after reading it.
I also wish to point out that some of us enjoy writing in assembly language. I personally find satisfaction in having a more direct connection with the hardware. And as you will see in Chapter 19, programming I/O devices in assembly language can actually be easier to read than using a high-level language.
Two issues arise immediately when studying assembly language:
I/O interaction with a user through even the keyboard and screen is a very complex problem, well beyond the programming expertise of a beginner.
There is an almost endless variety of instructions that can be used.
There are several ways to deal with these problems in a textbook. Some books use a simple operating system for I/O, e.g., MS-DOS. Others provide libraries of I/O functions that are specific for the examples in the book. Several textbooks deal with the instruction set issue by presenting a simplified “idealized” architecture with a small number of instructions that is intended to illustrate the concepts.
In keeping with the “real world” criterion of this book, I deal with these two issues by:
Showing you how to call the I/O functions already available in the C Standard Library.
Presenting only a small subset of the available instructions.
This has the added advantage of not requiring additional software to be installed. A standard installation of Raspbian on a Raspberry Pi is all you need to do the programming covered in this book.