Blogging about our lives online.


Assembly Language - Development Environment

The development environment for assembly language outlined in this book is a bare bones, linux-based toolchain. There is no one-click "Build and Go" or "Build and Debug" options here. I have installed the tools on my Arch Linux backup system which I am using through ssh from my laptop.
Assembler: GAS (GNU Assembler, part of the GCC Package).
Linker: ld, included with gcc.
Debugger: gdb (wasn't installed, `pacman -S gdb` solved that).
Profiling Tools: objdump, gprof are included in the binutils package.
I also looked into setting up a development environment using Xcode on my mac. It is fairly trivial to set up a "Standard Tool" project and call assembly code, but the assembly code uses some different conventions on mac vs. Linux. I've decided to stick with the linux style used in the book, but there is a good overview of the basics of Mac assembler in the Mac developer reference.
Using The Assembler
So, a couple notes about using the tools. My first observation was that to know what a program is doing in assembly, you must learn to use the debugger, always compile with -gstabs. The book recommends two stage compilation, assemble and link, but I'm not sure why. A one stage gcc invocation guarantees(?) proper linking without having to explicitly name the library and the dynamic linker.
Example: Using as & ld when linking to a C library
as -o somefile.o somefile.s
ld -dynamic-linker /lib/ -o somefile -lc somefile.o
Compared to using gcc:
gcc -o somefile somefile.s
I think I'll stick with gcc for now. And including debugging symbols is straightforward as well:
gcc -gstabs -o somefile somefile.s
Using the Debugger
The debugger is not quite as mystical when used in assembly language. For the most part each line corresponds to a machine instruction, so you can follow along in the source code and view the results of each operation. When the debugger starts, it prepares the code for execution and then awaits your instructions. If you simply type 'run' it will execute the code to the end. To see what the program is doing, you must specify a breakpoint: 'break *main' stops execution at the start of the main block. From here we can step through the program with the 'step' instruction. Some interesting things to be looking at during execution are the registers 'info registers', data values 'x/d &value' and the stack 'info stack'.
Compiling C source to Assembly
Another good resource for studying assembly language is to write the program in C and compile it with the -S option which outputs the assembly equivalent of the C code. This may also be a good way to fine-tune your C code, but for the purposes at hand it's simply a learning exercise, and I don't plan on doing better than GCC any time soon!
Source Text: Professional Assembly Language (Blum, 2005)

No comments:

Post a Comment