This simple driver for a SPARC disassembler disassembles its own main routine.

<*>= [D->]
#include <stdio.h>
#include "sparcdis.h"   

We do nothing at all clever about disassembling relocatable addresses. A real function would keep track of labels and show these things more symbolically.

<*>+= [<-D->]
static char *dis_rel (void *unused, unsigned addr) { 
  static char buf[80];
  sprintf(buf, "0x%08x", addr);
  return buf;
Defines dis_rel (links are to index).

We fetch by casting pointers and integers.

<*>+= [<-D->]
static unsigned fetch_word(void *unused, unsigned lc) {
  return *(unsigned *)lc;
Defines fetch_word (links are to index).

The main program disassembles from main through empty

<*>+= [<-D]
static void empty(void);
main() {
  unsigned start = (unsigned)main;

  for (start = (unsigned)main; start < (unsigned)empty; start += 4) {
    sparc_disassemble(start, (Printer) fprintf, stdout, 
                             dis_rel, NULL, fetch_word, NULL);

static void empty() {}
Defines empty, main (links are to index).