As we advance learning the foundations of ARM assembler, our examples will become longer. Since it is easy to make mistakes, I think it is worth learning how to use GNU Debugger gdb to debug assembler. If you develop C/C++ in Linux and never used gdb, shame on you. If you know gdb this small chapter will explain you how to debug assembler directly.
gdb
We will use the example store01 from chapter 3. Start gdb specifying the program you are going to debug.
Ok, we are in the interactive mode of gdb. In this mode you communicate with gdb using commands. There is a builtin help command called help. Or you can check the GNU Debugger Documentation. A first command to learn is
Ok, now start gdb again. The program is not running yet. In fact gdb will not be able to tell you many things about it since it does not have debugging info. But this is fine, we are debugging assembler, so we do not need much debugging info. So as a first step let's start the program.
Ok, gdb ran our program up to main. This is great, we have skipped all the initialization steps of the C library and we are about to run the first instruction of our main function. Let's see whats there.
Uh-oh! The instructions referring the label addr_of_myvarX are different. Ok. Ignore that for now, we will learn in a future chapter what has happened. There is an arrow => pointing the instruction we are going to run (it has not been run yet). Before running it, let's inspect some registers.
We can modify registers using p which means print but also evaluates side effects. For instance,
gdb has printed $1, this is the identifier of the result and we can use it when needed, so we can skip some typing. Not very useful now but it will be when we print a complicated expression.
Now we could use $2, and so on. Ok, time to run the first instruction.
Well, not much happened, let's use disassemble, again.
Ok, let's see what happened in r1.
Great, it has changed. In fact this is the address of myvar1. Let's check this using its symbolic name and C syntax.
Great! Can we see what is in this variable?
Perfect. This was as expected since in this example we set zero as the initial value of myvar1 and myvar2. Ok, next step.
You can use disas (but not disa!) as a short for disassemble. Let's check what happened to r3
So far so good. Another more step.
Ok, lets see what happened, we stored r3, which contained a 3 into myvar1, right? Let's check this.