home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_03 / 9n03068a < prev    next >
Text File  |  1990-12-20  |  3KB  |  215 lines

  1. /************************************************************************/
  2. /*                                    */
  3. /*                mon86.c                    */
  4. /*                                    */
  5. /*            Simple 8086 rom based monitor.             */
  6. /*                                    */
  7. /************************************************************************/
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <dos.h>
  12.  
  13.  
  14. void debug();
  15. int smatch();
  16.  
  17.  
  18. struct table
  19. {
  20.     char    *t_name;        /* the name for this entry    */
  21.     int    (*t_fn)();        /* the function to execute    */
  22. };
  23.  
  24. int mem(), go(), error();
  25.  
  26.  
  27. struct table cmd[] =
  28. {
  29.     "mem",    mem,
  30.     "go",    go,
  31.     "",    error
  32. };
  33.  
  34.  
  35. void main(argc, argv)
  36. int argc;
  37. char *argv[];
  38. {
  39.     if(argc != 2)
  40.     {
  41.         printf("%s: incorrect argument count\n", argv[0]);
  42.         exit(1);
  43.     }
  44.     else
  45.         printf("mon86 - demo 8086 monitor\n%s version\n", argv[1]);
  46.     debug();
  47. }
  48.  
  49.  
  50. void debug()
  51. {
  52.     char line[BUFSIZ];
  53.         char key[32];
  54.     char *lp, *p;
  55.         struct table *tp;
  56.     char *skipwh();
  57.  
  58.     for(;;)
  59.         {
  60.         printf("\nmon86: ");
  61.         gets(line);
  62.         lp = skipwh(line);
  63.         if(*lp == NULL)
  64.             continue;
  65.         p = key;
  66.         while(*lp != NULL && !iswhite(*lp))
  67.             *p++ = *lp++;
  68.         *p = NULL;
  69.         for(tp = cmd; *(tp -> t_name) != NULL; tp++)
  70.             if(smatch(key, tp -> t_name))
  71.                             break;
  72.         (*(tp -> t_fn))(lp);
  73.     }
  74. }
  75.  
  76.  
  77. int iswhite(c)
  78. register int c;
  79. {
  80.     return (c != NULL && (c == ' ' || c == '\t' || c == '\n' || c == '\r'));
  81. }
  82.  
  83.  
  84.  
  85. char *skipwh(p)
  86. register char *p;
  87. {
  88.     while(iswhite(*p))
  89.         ++p;
  90.     return p;
  91. }
  92.  
  93.  
  94.  
  95. /*                                    */
  96. /* smatch:                                */
  97. /*    match two strings. return true if equal                */
  98. /*                                    */
  99. static int smatch(s1, s2)
  100. char *s1, *s2;
  101. {
  102.     while(*s1 != '\0')
  103.     {
  104.         if(*s1 != *s2)
  105.             break;
  106.         ++s1;
  107.         ++s2;
  108.     }
  109.     return *s1 == *s2;
  110. }
  111.  
  112.  
  113.  
  114. int mem(lp)
  115. char *lp;
  116. {
  117.     unsigned long l = 0l;
  118.     unsigned int seg, offset;
  119.     char far *fp;
  120.         char buf[80];
  121.  
  122.     printf("Memory examine and change\n");
  123.     lp = skipwh(lp);
  124.     while(hexdigit(*lp) >= 0)
  125.         l = (l << 4) + hexdigit(*lp++);
  126.     if(*lp == ':')
  127.     {
  128.         ++lp;
  129.         seg = l;
  130.         l = 0l;
  131.         while(hexdigit(*lp) >= 0)
  132.             l = (l << 4) + hexdigit(*lp++);
  133.         offset = l;
  134.     }
  135.     else               
  136.     {
  137.         seg = 0;
  138.         offset = l;
  139.     }
  140.     fp = MK_FP(seg, offset);
  141.     for(;;)
  142.         {
  143.         printf("%04x:%04x - %02x ", FP_SEG(fp), FP_OFF(fp), *fp & 0xff);
  144.         gets(buf);
  145.         lp = skipwh(buf);
  146.         if(smatch(".", lp))
  147.             break;
  148.         if(!hexdigit(*lp) >= 0)
  149.         {
  150.             ++fp;
  151.             continue;
  152.         }
  153.         while(hexdigit(*lp) >= 0)
  154.             l = (l << 4) + hexdigit(*lp++);
  155.         *fp++ = l & 0xff;
  156.     }
  157. }
  158.  
  159.  
  160. int go(lp)
  161. char *lp;
  162. {
  163.     int  (far *fp)();
  164.     unsigned long l = 0l;
  165.         unsigned int seg, offset;
  166.  
  167.     lp = skipwh(lp);
  168.     while(hexdigit(*lp) >= 0)
  169.         l = (l << 4) + hexdigit(*lp++);
  170.     if(*lp == ':')
  171.     {
  172.         ++lp;
  173.         seg = l;
  174.         l = 0l;
  175.         while(hexdigit(*lp) >= 0)
  176.             l = (l << 4) + hexdigit(*lp++);
  177.         offset = l;
  178.     }
  179.     else               
  180.     {
  181.         seg = 0;
  182.         offset = l;
  183.     }
  184.     fp = MK_FP(seg, offset);
  185.     printf("Go from %04x:%04x", FP_SEG(fp), FP_OFF(fp));
  186.         (*fp)();
  187. }
  188.  
  189. int error()
  190. {
  191.     printf("*** Unknown command\n");
  192. }
  193.  
  194.  
  195. hexdigit(c)
  196. int c;
  197. {
  198.     register char *p;
  199.     register i;
  200.     static char *set = "0123456789abcdef",
  201.         *alt_set = "0123456789ABCDEF";
  202.  
  203.     for(i = 0, p = set; *p != NULL; i++, p++)
  204.         if(c == *p)
  205.             return i;
  206.     for(i = 0, p = alt_set; *p != NULL; i++, p++)
  207.         if(c == *p)
  208.             return i;
  209.     return -1;
  210. }
  211.  
  212.  
  213.  
  214.  
  215.