UNIX programming course

Nibbles assignment

In the assignments, you should implement a nibbles game in IA-32 assembly. The requirements for the lab can be found here: nibbles.pdf. For the assignment, a library of helper functions (to set chars on the screen etc) have been implemented. Download that here: helpers.tar.gz

The lecture notes for the assembly part is available below:

Intel IA-32 assembly (two lectures). For the UNIX Programming course at BTH:

Download PDF for printing (6 pages per page) or for viewing online

Tournament

The course includes a competition, producing the smallest implementation of a nibbles game. More information can be found on the Nibbles Assembly tournament page.

X86 reference

An X86 instruction set reference (AT&T-style) can be found here: x86ref.pdf.

Short assembly programs

I've collected a number of short x86/IA-32 assembly programs on theIA-32 assembly examples page.

GDB

I use a few assembly-related macros for GDB. My ~/.gdbinitcontains these.

The supplied Makefile contains a rule to compile code to run under GDB. With this, it is possible to use the normal debugging utils in GDB. Some hints:

Running a program in GDB

Start gdb with the program-name as argument, i.e. gdb prg. In Emacs you can start GDB by doing M-x gdb and then entering the program name.

A typical session can be seen below:

...
(gdb) b _start
Breakpoint 1 at 0x8048196: file prg.S, line 13.
(gdb) run
Starting program: /home/ska/tmp/prg

Breakpoint 1, _start () at prg.S:13

13             movl  $1, %eax
Current language:  auto; currently asm
(gdb) list
8              movl  $3, %ebx        # %ebx = 3
9       out:
10             ret
11      .globl _start
12      _start:
13             movl  $1, %eax
14             call  if_stmt
15             ## %ebx will be 3 here
16             pushl %ebx
17             call  exit
(gdb) s
14             call  if_stmt
(gdb) p $eax
$1 = 0x1
(gdb)

You can find a quick reference to GDB at www.refcards.com.

Misc tools

objdump

objdump can disassemble object-files, useful for looking what the binary representation of a program is:

[ska@ipdska tmp]$ objdump -d prg.o

prg.o:     file format elf32-i386

Disassembly of section .text:

00000000 <_start>:
   0:   b8 01 00 00 00          mov    $0x1,%eax
   5:   bb 02 00 00 00          mov    $0x2,%ebx
   a:   cd 80                   int    $0x80

readelf

readelf can (among other things) show the sections of an ELF-file. Useful for checking your progress in the nibbles competition! A script to calculate the size of the data + text section is the following (only tested in Linux)

#!/bin/sh

while [ $# -ne 0 ]; do
    echo -n "$1: "
    readelf -S $1 | egrep " .text| .data" | sed 's/ 00/ 0x0/g' | mawk 'BEGIN{sum=0;}{sum = sum + $7;}END{print sum}'
    shift
done

Pass the ELF-files to check to the script.

Links

A very nice book about IA-32 assembly for Linux/UNIX which is written in AT&T-syntax is "Professional Assembly Language" by Richard Blum. It can be found at adlibris.

PC Assembly Language - A very nice book about IA-32 assembly by Dr. Paul Carter. Uses the Intel syntax, and has (too my taste) slightly too long examples, but still is a very good reference to IA-32 assembly.

Programming from the ground up - Introduction to assembly language in book-form by Jonathan Bartlett. Uses AT&T-style assembly and focuses on assembly in UNIX systems.

developer.intel.com/.../245470.htm - The IA-32 Software Developer's Manual (volume 1-3). A couple of thousand pages about everything around x86 processors. Volume 2 contains the instruction set reference. Uses Intel-syntax.

linuxassembly.org - A collection of much information about programming assembly in UNIX-systems.

www.cs.colorado.edu/.../nasmdoca.html - The NASM manual (instruction set reference). Good reference, uses intel-syntax.

Intel Assembler 80x86 CodeTable - Intel assembly reference page, intel-syntax, but otherwise very nice.

Feedback?

Direct feedback to ska[at]bth[dot]se (change the obvious!)


$Id: unix_programming.html 4941 2005-10-07 07:13:47Z ska $