home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1623 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  9.4 KB

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