Lesson 1 - Hello, world!
Welcome to Lesson ! of ARM assembler programming on the Raspberry Pi
Some time ago I thought it would be cool if I could run SBCL, the best version of Common Lisp in the universe, on my Android phone. The problem is, of course, that the phone is an ARM platform and there is no ARM version of it. To the rescue raspberry pi! You can easily set up an ARM development platform for less than £20! I will assume you have already sussed out how to set up your pi, with a version of GNU/Linux running and that you know how to get to a command prompt. This can be done either directly or, the way I am doing it, via an ethernet network.
OK, experience tells me that before attempting a port of something like SBCL to a new platform, it is a good idea to get to know the assembler language for that platform. "But where on web are all the ARM assembler tutorials?" I asked myself, and answer came there none. Well that is a bit unfair, but none fitted the bill. So it seems I will have to do write it myself and here it is. I have done a lot of assembler programming, and for more platforms than I or anyone else could possibly remember, but as far as ARM and the pi are concerned, I am a newbie. So I can make mistakes, indeed I will make mistakes. One of the curiosities of assembler programming is that it is quite easy to write a program that does the right thing for the wrong reasons. So if you find a mistake or have any difficulties, leave a message, and I will read it. I don't guarantee I will have time to reply, but your contribution will be appreciated. Also I will never consider the conversation dead; so don't be put off if no one has contributed for a while.
The particular pi I am working on has Debian GNU/Linux installed, but you could probably do everything here with any UNIX-like OS on your pi. In fact you could probably do it all on your Android phone, but unless you are particularly amused by the idea that a misplaced '#' might accidentally zap your phone's SD card then it is not a good idea. Actually there is not much chance of it happening early on but when we go "bare metal", that will be a distinct possibilty. (If you don't know what "bare metal" means then you will when we get to it!)
There is a rumour that assembler language is difficult. Actually it isn't, it's dangerous. Writing well in assembler is difficult but that is true of any computer language. Programmers come to rely on tools that catch the most egregious of their mistakes. The "lower" the level the language the less reliable these tools are. Thus while it is virtually impossible for the writer of EJBs to accidentally bring down a multi-billion dollar international computer network, an assembler programmer could easily do it ten times before breakfast. But on the pi, who cares? You can easily have an SD card dedicated to assembler programming and if you wreak it re-flash it.
So here is Lesson 1
In the beginning there was UNIX and when UNIX spoke it said "Hello, world!" Since that time the first thing we must learn to do in any computer language is to say "Hello, world!"
How is the pi going to say "Hello, world!"? In the end it boils down to changing individual dots on a screen or individual blobs of ink on a piece of paper. But this would be very low level stuff. It can be done this way but we are going to use a layer of abstraction called the operating system (OS). From the point of view of the pi hardware there is nothing special about the OS; it's just a program that runs all the time and that contains solutions to problems that programmers, in any language, need. These solutions are accessed by what are known as system calls. In our case we shall use the system call prints to print our message. For those of you who are curious, GNU/Linux is written in the C programming language and system calls simply invoke functions in the standard C libraries. But you don't really need to know about C, all you need to know is how to access these libraries; that is, how to do the system calls.
Baby Steps
Assuming you have set up your pi and have accessed a command prompt you should see a prompt something like:
pi@raspberrypi:~$ _
If it looks any different then you have altered it yourself and I will assume you know what you are doing! The prompt tells us that we are "in" our home directory - actually it tells us that he present working directory is the home directory, but let's not get too technical. Putting everything in the home directory is a recipe for recurring nightmares if not complete insanity; so we will need another directory to put our stuff in. I have called mine whirlwind, but that's because I'm crazy. You can call yours anything you like. You need to proceed something like:
pi@raspberrypi:~$ mkdir whirlwind
pi@raspberrypi:~$ cd whirlwind
pi@raspberrypi:/whirlwind$ _
So now we are somewhere we want to be! Now we need a file to hold our assembler code. A good name for it is helloworld.s; the ".s" will tell our development tools that it contains assembler. "What development tools?" I hear you ask - all will be clear later.
If you have been following the pi Python tutorials, you will probably be used to using the joe editor to edit your stuff. In this case you can invoke it with:
pi@raspberrypi:/whirlwind$ joe helloworld.s
Personally I don't like joe, no offence intented if that's you name, I much prefer nano; so this is how I invoke my editor:
pi@raspberrypi:/whirlwind$ nano helloworld.s
If you like you can use another editor such as emacs, but you may have to install it yourself.
What does the code look like?
Well here is what it looks like in nano: (click on the image to make it bigger)
Wot no file to download?
- Sorry I'm a firm believer in the "type it in yourself" school of learning to program.
What next?
Well having typed in the code - in case you're worrying the sold block is the cursor - you must save it and exit the editor. Your directory will now contain the file helloworld.s - type ls to check this. We are going to use a tool (or technically a tool chain) called the GNU Compiler Collection, or GCC for short, to turn our code into an executable program. This is probably already installed on your system. If it isn't you will soon find out! This is incredibly easy; to invoke GCC you need to do:
pi@raspberrypi:/whirlwind$ gcc helloworld.s
Your directory will now contain two new files: helloworld.o and a.out.
What the hell is a.out?
Actually it is your executable program. The name dates from the early days of computer science and its precise meaning is lost in the mists of antiquity. For now we only need to know how to run it:
pi@raspberrypi:/whirlwind$ ./a.out
Hello, world!
If all has gone well you have now assembled and run your first assembler program. If not, you might have to do some re-editing, but that is how you learn!
Exercises
- Try to understand how the code works. All will be explained next lesson, but any respectable hacker will want to try to understand for hm/her self. Play with the code - comment things out etc. - see what happens.
- Do you think
helloworld.ois needed for the program to run? If so why? Find out by deleting it and re-running the program. - Use the interwebs to find out about GCC. Can you invoke it is such a way that the final program has a more sensible name?
- Modify the program so that it prints out two strings in the reverse order to that in which they are stored.
- (**) Can you make a version that prints out a non-ascii message. E.g 世界你好! (That's in Chinese - if it doesn't display properly you will need to install an appropriate font!)
Note: I don't usually ask people to do things I can't do myself. The exception is exercises marked (**). I don't guarantee I have done these, but I think they are worth trying nevertheless.
