Setup
Before we can start writing assembly programs, we need to do some basic setup. If your Pi is not already setup and running, stop here and do that. Once it is running, login to the terminal. You won't need the graphical interface for any of this, and it is honestly easier to just work completely in the terminal for most of the chapters in this series. If you are an experienced user and you feel you cannot do without the GUI (both of these in one seems unlikely), feel free to use the GUI, but understand that I won't be using the GUI, and this series will assume that you are working purely from a terminal as well.Once you have logged into a terminal, you will need to check what version of GCC is installed. This is important, because the assembler that comes with versions of GCC older than 4.8 does not have support for some ARMv7 instructions that we will be using on our Pi 2. (The original Pi processor is ARMv6, and thus it does not have these instructions.) Check your version of GCC by running gcc -v. This will spit out a lot of information. At the bottom you will find the version information. My Pi 2 says it's GCC version is 4.6.3. This is what it came with. If your version is lower than 4.8, you will need to run sudo apt-get install gcc-4.8. Note that this will not change the default version. If you run gcc -v again, it will still say the old version. For compatibility reasons, installing GCC 4.8 won't replace the old version. Now though, you should be able to run gcc-4.8 -v, and it will give you the version information about the new GCC you just installed. If you want to compile with GCC 4.8, you will have to run gcc-4.8 instead of gcc. Installing GCC 4.8 will replace the assembler though, which is all we really care about here.
I should add a note here, if the version of GCC says 4.8 or higher (recent versions of Raspbian now come with 4.9.2), then you can just use gcc, instead of specifying the version.
Writing a Simple Program
This part is not so much about learning assembly as it is about learning to use a terminal editor. You have two options by default. They are Nano and Vim. In my opinion, Vim is the better of the two, however it has such a steep learning curve that am not even going to try to teach you how to use it (besides that, I am not even that proficient with it yet). If you prefer Emacs, it is not installed by default, but you probably already know how to install it. I am going to use Nano for this series. You can run nano in the terminal to start Nano and create a new file. You can run nano filename to open an existing file. When you are in Nano, you will see a list of controls at the bottom. The important ones are Ctrl+R to open a file, Ctrl+O to save a file, and Ctrl+X to exit (I have never actually used Ctrl+R...). You can also use Ctrl+W to search, Ctrl+K to cut a line of text, and Ctrl+U to paste the last text that was cut. If you expect to use these last few, I would suggest experimenting with them a bit to get familiar with how they work.Let's start by writing a simple program that compiles with C. You will probably want to create a directory to do all of this in. I'll leave that up to you (in Linux, use the command mkdir directory_name to create a new directory in the current one, and use cd directory_name to move into that directory; if you did not know this already, you may want to find a Linux command line tutorial before continuing). Once you are in your new directory, start Nano. I ran nano num.s, to create a new file named num.s. (The file extension for assembly files is ".s".) You can instead specify the file name when you use Ctrl+X to save it the first time, if you prefer.
The first thing we need in the file is a line with the text .text. .text is the section of your program with executable code, and it is generally called the text section (less commonly the "code section"). Since we are compiling with GCC, we need a "main" function. Assembly does not have functions in the same sense as other languages, though we generally treat it like it does. Instead, assembly has labels that are references to memory addresses. These can be the addresses of data or instructions. To create a "main" function, we need to do two things. First, we need to create a label named "main" that references the instruction in our code where the program should start. This is done by typing main:, on its own line. The second thing we need to do is make our "main" label visible outside of our assembly file (so that the C library can see it; more on why this is necessary later). Directly above the "main" label, add a line, .global main. In the future, we will type the global directive before we create the actual label.
As this point, your program should look like this:
.text
.global main
main:
Now, the C compiler knows where our program starts (the instruction directly following the "main" label). Next we need to write the actual code. To keep things simple, our program is going to return an error code. The convention is for functions to put their return values in the register r0. When we return from main, the value in r0 is going to be used as the error code returned by our program. Normally you don't get to see what error codes are returned by programs, but I'll show you a way to do it when we get there.
To put a value in r0, we will use the mov instruction. This instruction can copy a value in one register to another, or it can take an immediate value (a number encoded in the instruction) and put it in a register. There are some limitations to this, but we don't need to worry about them yet. Underneath the "main" label, press tab once to indent, and then type mov r0, #27. This will put the value 27 in the register r0. Immediate values must generally be preceded with a # symbol.
Now that we have put our return value in r0, we can return from the main function. This is done with a special branch instruction that takes a memory address to branch to. By default, the program will run one instruction after another sequentially. Branches are instructions that tell the processor to go to some other instruction and continue from there. This is how we do things like calling functions and skipping instructions in false if statements. We also use a branch instruction to return from a function. In this case we will use the bx instruction, which takes a register that contains the memory address of the instruction we want to go to. We will discuss ARM registers more in a moment, but for this instruction all we need to know about is the lr register, which contains the address for the return point when a function is called. To return from the main function, we will use the instruction bx lr. This will go back to whatever code C used to call the main function, allowing C to shut down anything it did before the program exits.
Our program should now look like this:
.text
.global main
main:
mov r0, #27
bx lr
All this program does is to return the exit code 27. Here we need to save our program (Ctrl+O). Now, exit Nano (Ctrl+X). At the command line again, run gcc num.s. This will compile our program and create an executable with the default name a.out. We can run our program with ./a.out. Disappointingly, it does nothing. Like I said before, normally, we don't actually get to see the error codes. We can display the error code of the last program that ran by running echo $?. This should print out the number 27. (Note that if you run echo $? again, it will print out 0, because that is the error code returned from the first time you ran it.) You can edit your program and change the number put into r0, but there are two things to keep in mind. First, negative numbers won't work. The error code is an unsigned byte, so a negative number will print out as its unsigned representation. Second, an unsigned byte can only fit in the range of values from 0 to 255 inclusive. If you put in a number outside of this range, it will only print out whatever the unsigned representation of the lower 8 bytes are. Additionally, due to limitations we will discuss later, some numbers outside of this range won't even allow your code to compile. If the compiler gives you an error like "invalid constant (8888) after fixup", it means that the number you are trying to put in the register cannot be encoded in the instruction you are trying to put it in, so you will have to find another way.
The main point of this exercise was to become familiar with the editor and make sure everything works the way we expected. An important thing to note is that we did not use the GCC 4.8 that we installed earlier, but we did use the assembler installed with it. From here on out, most of our programs will not be compiled with GCC. GCC adds some overhead to pretty much everything it compiles. It adds startup and shutdown code that we really only care about if we are using C functions. So, for the most part, the only time we will use GCC to compile our programs is when we are using C functions.
No comments:
Post a Comment