Two more Gnu tools

Author: William T Krieger, Oct 27, 2003

This is a (painfully) brief overview of two more Gnu tools at our disposal: gdb and make. These tools may ease your assembly programming chores in CSC 220. You are not required to learn or use them. This is just FYI.

The Gnu documentation for these programs can be found at:

http://www.gnu.org/manual 

1. Debugging with gdb

The Gnu debugger is called gdb. With a tiny learning curve, gdb will allow you to:

1.1. Compiling for gdb

To use gdb, your assembly files must be compiled with the --gstabs option (for "generate symbol tables") to the assembler, as. [Note: I tried a variety of ways to pass this option to the assembler via gcc but was not successful] For example, to assemble file z.s for debugging requires the following steps:

as --gstabs -o z.o z.s
gcc z.o

At this point, the executable a.exe is ready to debug using gdb:

gdb a.exe

That's a load of typing, especially if you break your program into multiple files. There are two ways to streamline your development process:

1.2. Steering gdb

Once inside gdb, the most common things you'll want to do are [gdb commands are in brackets]:

Sometimes it's helpful to just let your program run until it crashes and use the debugger to see at what line the crash occurs.

1.3. Getting more help

Googling this:

gdb "assembly code" tutorial 

led me to these online gdb tutorials (like this one, but with more details and examples):

2. Make

Make, according to its Gnu manual: "automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them."

Well, programs in our class are relatively small. You may still want to use make because, once setup, it reduces the amount of typing you need to do. It will also ensure that files that are changed are re-compiled when creating a new executable.

Using make requires the following steps:

  1. Create a file called Makefile describing the files in your program
  2. Run make or make <target> to execute the commands to create the target file/program

2.1. Creating a Makefile

By default, make uses a file called Makefile to guide its work. Hey, here's a sample Makefile that could be used for our Program #1 assignment (it's on the k: drive for you to copy BTW):


a.exe: prog1.o read_num.o write_num.o get_min.o get_max.o sort_num.o
    gcc prog1.o read_num.o write_num.o get_min.o get_max.o sort_num.o

ASFLAGS = --gstabs

prog1.o: prog1.s
    as $(ASFLAGS) -o prog1.o prog1.s

read_num.o: read_num.s
    as $(ASFLAGS) -o read_num.o read_num.s

write_num.o: write_num.s
    as $(ASFLAGS) -o write_num.o write_num.s

get_min.o: get_min.s
    as $(ASFLAGS) -o get_min.o get_min.s

get_max.o: get_max.s
    as $(ASFLAGS) -o get_max.o get_max.s

sort_num.o: sort_num.s
    as $(ASFLAGS) -o sort_num.o sort_num.s

Here are some notes of the format of a Makefile:

The bulk of you Makefile consists of rules; the rules you want make to follow when building your program. Each rule has the form:

<target> : <prerequisties>
    <command>

So, for example these lines in our example Makefile:

prog1.o: prog1.s
     as $(ASFLAGS) -o prog1.o prog1.s

mean that the target file prog1.o depends on its prerequisite prog1.s and the as command runs the assembler to create prog1.o. Note that the command is only executed if prog1.o needs to be re-made because its prerequisite prog1.s has changed or is more 

A couple more important notes:

2.2. Running make

Once your Makefile is correctly constructed and saved to your local directory, you can run make by issuing the command:

make

If your Makefile is copacetic, make will run the commands you've specified to build any target that needs to be updated.

2.3. Getting more help

Googling this:

Makefile "assembly code" tutorial

led me to this online make tutorial (like this one, but with more details and examples):