home *** CD-ROM | disk | FTP | other *** search
/ For Beginners & Professional Hackers / cd.iso / software / debug / dasm85 / dasm.c next >
Encoding:
C/C++ Source or Header  |  1990-08-01  |  9.2 KB  |  262 lines

  1. /*
  2.     I had quite a few requests for this, and some of my replies bounced,
  3. so here it is.  Please note: Some of the copies I mailed were sent out
  4. before I noticed that there had originally been a statement to the
  5. effect that the program was not shareware.  That was from the dark ages,
  6. and you may remove that notice.
  7.  
  8.     This compiles properly under MSC 6.0, under the common C compiler,
  9. and under gnucc.  I have not tested the program on anything but the PC,
  10. because I don't have any files to disassemble anymore.  Use it in good
  11. health. 
  12. */ 
  13. /*  DASM85.C    30-Nov-88  9:07:32 by Kevin D. Quitt
  14.  
  15.     Disassemble 8085 code from a binary file.
  16.  
  17.     Two passes - the first collects labels, the second performs
  18.     the disassembly.  References to addresses with instructions are
  19.     noted as sync errors.
  20.  
  21.     Written for Microsoft C to run on an IBM PC.
  22. */
  23. #include    <stdio.h>
  24. #include    <string.h>
  25.  
  26. #define MAX_LABELS  (2048)
  27.  
  28. unsigned int    Labels[ MAX_LABELS ];
  29. unsigned char   In_File[ 132 ];
  30. unsigned char   Out_File[ 132 ];
  31. unsigned char   Temp[ 132 ];
  32.  
  33. char    *Op_Code[256]   =
  34.    {
  35. /*    1           2           3           4                                 */
  36.     "nop",      "lxi\tb,",  "stax\tb",  "inx\tb",
  37.     "inr\tb",   "dcr\tb",   "mvi\tb,",  "rlc",
  38.     "???",      "dad\tb",   "ldax\tb",  "dcx\tb",
  39.     "inr\tc",   "dcr\tc",   "mvi\tc,",  "rrc",
  40.     "???",      "lxi\td,",  "stax\td",  "inx\td",
  41.     "inr\td",   "dcr\td",   "mvi\td,",  "ral",
  42.     "???",      "dad\td",   "ldax\td",  "dcx\td",
  43.     "inr\te",   "dcr\te",   "mvi\te,",  "rar",
  44.     "rim",      "lxi\th,",  "shld\t",   "inx\th",
  45.     "inr\th",   "dcr\th",   "mvi\th,",  "daa",
  46.     "???",      "dad\th",   "lhld\t",   "dcx\th",
  47.     "inr\tl",   "dcr\tl",   "mvi\tl,",  "cma",
  48.     "sim",      "lxi\tsp,", "sta\t",    "inx\tsp",
  49.     "inr\tm",   "dcr\tm",   "mvi\tm,",  "stc",
  50.     "???",      "dad\tsp",  "lda\t",    "dcx\tsp",
  51.     "inr\ta",   "dcr\ta",   "mvi\ta,",  "cmc\ta,",
  52. /*    0           1           2           3                             */
  53.     "mov\tb,b", "mov\tb,c", "mov\tb,d", "mov\tb,e",
  54.     "mov\tb,h", "mov\tb,l", "mov\tb,m", "mov\tb,a",
  55.     "mov\tc,b", "mov\tc,c", "mov\tc,d", "mov\tc,e",
  56.     "mov\tc,h", "mov\tc,l", "mov\tc,m", "mov\tc,a",
  57.     "mov\td,b", "mov\td,c", "mov\td,d", "mov\td,e",
  58.     "mov\td,h", "mov\td,l", "mov\td,m", "mov\td,a",
  59.     "mov\te,b", "mov\te,c", "mov\te,d", "mov\te,e",
  60.     "mov\te,h", "mov\te,l", "mov\te,m", "mov\te,a",
  61.     "mov\th,b", "mov\th,c", "mov\th,d", "mov\th,e",
  62.     "mov\th,h", "mov\th,l", "mov\th,m", "mov\th,a",
  63.     "mov\tl,b", "mov\tl,c", "mov\tl,d", "mov\tl,e",
  64.     "mov\tl,h", "mov\tl,l", "mov\tl,m", "mov\tl,a",
  65.     "mov\tm,b", "mov\tm,c", "mov\tm,d", "mov\tm,e",
  66.     "mov\tm,h", "mov\tm,l", "hlt\t",    "mov\tm,a",
  67.     "mov\ta,b", "mov\ta,c", "mov\ta,d", "mov\ta,e",
  68.     "mov\ta,h", "mov\ta,l", "mov\ta,m", "mov\ta,a",
  69. /*    0           1           2           3                             */
  70.     "add\tb",   "add\tc",   "add\td",   "add\te",
  71.     "add\th",   "add\tl",   "add\tm",   "add\ta",
  72.     "adc\tb",   "adc\tc",   "adc\td",   "adc\te",
  73.     "adc\th",   "adc\tl",   "adc\tm",   "adc\ta",
  74.     "sub\tb",   "sub\tc",   "sub\td",   "sub\te",
  75.     "sub\th",   "sub\tl",   "sub\tm",   "sub\ta",
  76.     "sbb\tb",   "sbb\tc",   "sbb\td",   "sbb\te",
  77.     "sbb\th",   "sbb\tl",   "sbb\tm",   "sbb\ta",
  78.     "ana\tb",   "ana\tc",   "ana\td",   "ana\te",
  79.     "ana\th",   "ana\tl",   "ana\tm",   "ana\ta",
  80.     "xra\tb",   "xra\tc",   "xra\td",   "xra\te",
  81.     "xra\th",   "xra\tl",   "xra\tm",   "xra\ta",
  82.     "ora\tb",   "ora\tc",   "ora\td",   "ora\te",
  83.     "ora\th",   "ora\tl",   "ora\tm",   "ora\ta",
  84.     "cmp\tb",   "cmp\tc",   "cmp\td",   "cmp\te",
  85.     "cmp\th",   "cmp\tl",   "cmp\tm",   "cmp\ta",
  86. /*    0           1           2           3                             */
  87.     "rnz",      "pop\tb",   "jnz\t",    "jmp\t",
  88.     "cnz",      "push\tb",  "adi\t",    "rst\t0",
  89.     "rz",       "ret",      "jz\t",     "???",
  90.     "cz",       "call\t",   "aci\t",    "rst\t1",
  91.     "rnc",      "pop\td",   "jnc\t",    "out\t",
  92.     "cnc",      "push\td",  "sui\t",    "rst\t2",
  93.     "rc",       "???",      "jc\t",     "in\t", 
  94.     "cc",       "???",      "sbi\t",    "rst\t3",
  95.     "rpo",      "pop\th",   "jpo\t",    "xthl",
  96.     "cpo",      "push\th",  "ani\t",    "rst\t4",
  97.     "rpe",      "pchl",     "jpe\t",    "xchg",
  98.     "cpe",      "???",      "xri\t",    "rst\t5",
  99.     "rp",       "pop\tpsw", "jp\t",     "di",
  100.     "cp",       "push\tpsw","ori\t",    "rst\t6",
  101.     "rm",       "sphl",     "jm\t",     "ei",
  102.     "cm",       "???",      "cpi\t",    "rst\t7"    
  103.    };
  104.  
  105. char    Op_Code_Size[]  =
  106.    {
  107. /*  0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F           */
  108.     1,  3,  1,  1,  1,  1,  2,  1,  1,  1,  1,  1,  1,  1,  2,  1,  /* 00   */
  109.     1,  3,  1,  1,  1,  1,  2,  1,  1,  1,  1,  1,  1,  1,  2,  1,  /* 10   */
  110.     1,  3,  3,  1,  1,  1,  2,  1,  1,  1,  3,  1,  1,  1,  2,  1,  /* 20   */
  111.     1,  3,  3,  1,  1,  1,  2,  1,  1,  1,  3,  1,  1,  1,  2,  1,  /* 30   */
  112.  
  113.     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  /* 40   */
  114.     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  /* 50   */
  115.     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  /* 60   */
  116.     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  /* 70   */
  117.  
  118.     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  /* 80   */
  119.     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  /* 90   */
  120.     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  /* A0   */
  121.     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  /* B0   */
  122.  
  123.     1,  1,  3,  3,  1,  1,  2,  1,  1,  1,  3,  1,  3,  3,  2,  1,  /* C0   */
  124.     1,  1,  3,  2,  3,  1,  2,  1,  1,  1,  3,  2,  3,  1,  2,  1,  /* D0   */
  125.     1,  1,  3,  1,  3,  1,  2,  1,  1,  1,  3,  1,  3,  1,  2,  1,  /* E0   */
  126.     1,  1,  3,  1,  3,  1,  2,  1,  1,  1,  3,  1,  3,  1,  2,  1   /* F0   */
  127.    };
  128.  
  129.  
  130. /*  File extension utilities.                                  114
  131. */
  132. void    Extend( out, in, ext )
  133.  
  134. char    *out, *in, *ext;
  135.    {
  136.     strcpy( out, in );
  137.     if ( strchr( out, '.' )  ==  NULL )
  138.        {
  139.         strcat( out, "." );
  140.         strcat( out, ext );
  141.        }
  142.    }
  143.  
  144.  
  145. void    Strip( out, in )
  146.  
  147. char    out[], in[];
  148.    {
  149.     char    *p;
  150.  
  151.     strcpy( out, in );
  152.     if ( (p = strchr( out, '.' ))  !=  NULL )
  153.         *p  = (char)0;
  154.    }
  155.  
  156. int     main( argc, argv )
  157.  
  158. int     argc;
  159. char    *argv[];
  160.    {
  161.     FILE    *In, *Out;
  162.     int     c, op, pc,i,j,m,k,ptr;
  163.  
  164.     pc  = 0;
  165.  
  166.     if ( (argv[1][0]  ==  '?')  ||  ( argc  !=  2) )
  167.        {
  168.         printf( "\nSyntax is DASM85 binfilespec\n" );
  169.         printf( "The extension is optional, BIN is assumed. Output is to\n" );
  170.         printf( "the same file name, with a DSA extension.\n\n\n" );
  171.         printf( "This program is freeware.\n\n");
  172.         return  0;
  173.        }
  174.  
  175.     Extend( In_File, argv[1], "BIN" );
  176.     if ( (In = fopen( In_File, "rb"))  ==  NULL )
  177.        {
  178.         printf( "Can't open input file %s!\r\n", In_File );
  179.         return  -1;
  180.        }
  181.  
  182.     Strip( Temp, argv[1] );
  183.     Extend( Out_File, Temp, "DSA" );
  184.     if ( (Out = fopen( Out_File, "wt"))  ==  NULL )
  185.        {
  186.         printf( "Can't open disassembly list file %s!\r\n", Out_File );
  187.         return -2;
  188.        }
  189.  
  190.     for(i = 0; i<2048; i++)
  191.         Labels[i] = 0xffff;
  192.     
  193.     k = 0;
  194.     while ( (op = fgetc( In ))  !=  EOF )
  195.        {
  196.         if ( Op_Code_Size[ op ]  ==  2 )
  197.             fgetc(In);
  198.         if ( Op_Code_Size[ op ]  ==  3 )        
  199.            {
  200.             i = fgetc(In);
  201.             j = fgetc(In);
  202.             j <<= 8;
  203.             i |= j;
  204.             for( m = 0 ; m <= k ; m++)
  205.                {
  206.                 if( i == Labels[m])
  207.                     break;
  208.                 if( i < Labels[m])
  209.                    {
  210.                     for( j = k ; j > m ; j-- )
  211.                         Labels[j] = Labels[j-1];
  212.                     Labels[m] = i;
  213.                     k++;
  214.                     break;
  215.                    }
  216.                }
  217.            }
  218.        }
  219.  
  220.     rewind( In );
  221.     ptr = 0;
  222.     while ( (op = fgetc( In ))  !=  EOF )
  223.        {
  224.  
  225.         while( pc > Labels[ptr] )
  226.             fprintf( Out, ";\t\t\t\tSync error, L%04X\n", Labels[ptr++]);
  227.  
  228.         if( pc == Labels[ptr] )
  229.            {
  230.             fprintf( Out, "L%04X:\t%s", pc, Op_Code[ op ] );
  231.             ptr++;
  232.            }
  233.         else fprintf(Out, "\t%s", Op_Code[ op ] );
  234.         
  235. /*  Two byte opcode are followed by a byte parameter.
  236. */
  237.         
  238.         if ( Op_Code_Size[ op ]  ==  2 )
  239.            {
  240.             i = fgetc(In);
  241.             if( i < 0xA0 )
  242.                 fprintf( Out, "%02Xh", i );
  243.             else    fprintf( Out, "0%02Xh", i );
  244.            }
  245.  
  246. /*  Three byte opcodes are followed by two bytes in reverse order.
  247. */
  248.         if ( Op_Code_Size[ op ]  ==  3 )
  249.            {
  250.             i = fgetc(In);
  251.             j = fgetc(In);
  252.             fprintf( Out, "L%02X%02X", j,i );
  253.            }
  254.  
  255.         fprintf( Out, "\n" );
  256.         pc  += Op_Code_Size[ op ];
  257.        }
  258.  
  259.     fclose( In );
  260.     fclose( Out );
  261.    }
  262.