Advertisement
iocoder

disassembler.c

Apr 25th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. /* instruction table:
  2.  * -------------------
  3.  * OPCODE MNEMONIC FORMAT
  4.  *   00     LDA     3/4
  5.  *   0C     STA     3/4
  6.  *   XX     ADDR     2
  7.  */
  8. struct instruction {
  9.     unsigned char opcode;
  10.     char opcode[6] mnemonic;
  11.     int format; /* 1: format 1, 2: format 2, 3: format 3/4 */
  12. };
  13.  
  14. unsigned int LC = 1000;
  15.  
  16. while (LC < 2000) {
  17.     unsigned int instruction_address = LC;
  18.     unsigned char indx;       /* indexed?       */
  19.     unsigned char indirect;   /* indirect?      */
  20.     unsigned char immediate;  /* immediate?     */
  21.     unsigned char baserel;    /* base relative? */
  22.     unsigned char pcrel;      /* pc relative?   */
  23.     unsigned char absolute;   /* format 4?      */
  24.     unsigned char operand[9]; /* the operand    */
  25.  
  26.     /* read the byte stored at LC, then increase LC by one */
  27.     unsigned byte opcode = get_byte_from_memory(LC++);
  28.  
  29.     /* translate the opcode (using the lookup table described above) */
  30.     struct instruction *instr = opcode_to_instruction(opcode);
  31.  
  32.     /* now disassemble the instruction based on its format */
  33.     if (instr->format == 1) {
  34.         /* format one, the easiest one!  */
  35.         indx = 0;
  36.         indirect = 0;
  37.         immediate = 0;
  38.         baserel = 0;
  39.         pcrel = 0;
  40.         absolute = 0;
  41.         strcpy(operand, "");
  42.     } else if (instr->format == 2) {
  43.         /* format 2, read an extra byte (the registers) */
  44.         unsigned char regs = get_byte_from_memory(LC++);
  45.         unsigned char r1 = regs>>4;
  46.         unsigned char r2 = regs & 0x0F;
  47.         operand[0] = to_register(r1);
  48.         operand[1] = ',';
  49.         operand[2] = to_register(r2);
  50.         operand[3] = 0;
  51.         indx = 0;
  52.         indirect = 0;
  53.         immediate = 0;
  54.         baserel = 0;
  55.         pcrel = 0;
  56.         absolute = 0;
  57.     } else {
  58.         /* format 3/4  */
  59.         unsigned char byte2 = get_byte_from_memory(LC++);
  60.         unsigned char byte3 = get_byte_from_memory(LC++);
  61.         indirect = (opcode & 0x02)>>1;
  62.         immediate = (opcode & 0x01)>>1;
  63.         indx = (byte2 & 0x80)>>7;
  64.         baserel = (byte2 & 0x40)>>6;
  65.         pcrel = (byte2 & 0x20)>>5;
  66.         absoulte = (byte2 & 0x10)>>4;
  67.         if (absolute) {
  68.             /* format 4 */
  69.             unsigned char byte4 = get_byte_from_memory(LC++);
  70.             unsigned int address = ((byte2 & 0x0F)<<16) +
  71.                            ((byte3 & 0xFF)<< 8) +
  72.                            ((byte4 & 0xFF)<< 0);
  73.             integer_to_string(operand, address);
  74.         } else {
  75.             /* format 3 */
  76.             unsigned int address = ((byte2 & 0x0F)<<8) +
  77.                            ((byte3 & 0xFF)<<0);
  78.             /* handle relativity */
  79.             if (baserel) {
  80.                 address += BASE;
  81.             } else if (pcrel) (
  82.                 /* FIXME: Two's complement issue */
  83.                 address += LC;
  84.             }
  85.             integer_to_string(operand, address);
  86.         }
  87.     }
  88.  
  89.     /* now print the original instruction */
  90.     printf("At %6x : ", instruction_address);
  91.    
  92.     /* format 4?? add + */
  93.     printf("%c", abs ? '+':' ');
  94.  
  95.     /* print the mnemonic */
  96.     printf("%s ", instr->mnemonic);
  97.  
  98.     /* immediate? add # */
  99.     if (immediate)
  100.         printf("#");
  101.  
  102.     /* indirect? add @ */
  103.     if (indirect)
  104.         printf("@");
  105.  
  106.     /* print the operand */
  107.     printf(operand);
  108.  
  109.     /* indexed? add ,X */
  110.     if (indexed)
  111.         printf(",X");
  112.  
  113.     /* done */
  114.     printf("\n");
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement