⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asm.c

📁 稀疏矩阵、链表、图、队列、二叉树、多叉树、排序、遗传算法等的实现
💻 C
📖 第 1 页 / 共 2 页
字号:

   case ADDB:
      RegB += *Operand;
      PC += IMMEDIATE_LENGTH;
      break;

   case COMPAI:
      SR =  RegA - *Operand;
      PC += IMMEDIATE_LENGTH;
      break;

   case COMPBI:
      SR =  RegB - *Operand;
      PC += IMMEDIATE_LENGTH;
      break;

   case BLT:   
      PC += ADDRESS_LENGTH;
      /* only branch if comparison less than is true */
      if (SR < 0)
      {
         Address = LoadAddress(Operand);
         PC = Address;
      }    
      break;

   case BGT:
      PC += ADDRESS_LENGTH;
      /* only branch if comparison greater than is true */
      if (SR > 0)
      {
         Address = LoadAddress(Operand);
         PC = Address;
      }
      break;

   case JUMP:
      Address = LoadAddress(Operand);
      PC = Address;
      break;

   case PRINTA:
      printf("%x\n", RegA);
      break;

   case BREAK:
      Stop = TRUE;
      break;

   default:
      /* didn't recognise the Instruction just decoded */
      Stop = TRUE;
   }

   Reg->RegA = RegA;
   Reg->RegB = RegB;
   Reg->SR   = SR;
   Reg->PC   = PC;

   return Stop;
}


/*******************************************************************************
 * Function       :  Run
 * Description    :  Runs the machine code at given location, till 
 *                   stop encountered, or error
 ******************************************************************************/

void Run (REGISTERS* Registers, unsigned char* Memory, int Start)
{
   printf ("Beginning execution at address %04X\n", Start);

   Registers->PC = Start;

   while (Execute (Memory, Registers) == FALSE)
      ;

   printf ("Break encountered at address %04X\n", Registers->PC);
}

/*******************************************************************************
 * Function       :  Step
 * Description    :  Executes one machine code instruction
 ******************************************************************************/
void Step (REGISTERS* Registers, unsigned char* Memory)
{
   if (Execute (Memory, Registers) == FALSE)
      printf ("Break encountered at address %04X\n", Registers->PC);
}


/*******************************************************************************
 * Function       :  SetStep
 * Description    :  Sets execution address
 *                   and executes one machine code instruction
 ******************************************************************************/
void SetStep (REGISTERS* Registers, unsigned char* Memory, int Start)
{
   Registers->PC = Start;
   if (Execute (Memory, Registers) == FALSE)
      printf ("Break encountered at address %04X\n", Registers->PC);
}


/*******************************************************************************
 * Function       :  DumpRegisters
 * Description    :  
 ******************************************************************************/

void DumpRegisters (REGISTERS* Registers)
{
   printf (" Reg A  : %02X\n", Registers->RegA);
   printf (" Reg B  : %02X\n", Registers->RegB);
   printf (" PC     : %02X\n", Registers->PC);
   printf (" SR     : %02X\n", Registers->SR);
}


/*******************************************************************************
 * Function       :  DumpMemory
 * Description    :  
 ******************************************************************************/

void DumpMemory (unsigned char* Memory, int start, int end)
{
   int i = 0;
   int j = 0;


   for (i=start; i < end; i+= 8)
   {
      printf ("%04X     ", i);

      for (j = i; j < i+8; j++)
      {
         printf ("%02X  ", Memory[j]);
      }
      printf("\n");
   }
}

/*******************************************************************************
 * Function       :  Disassemble
 * Description    :  Disassembles from start address to end address
 ******************************************************************************/

void Disassemble (int Start, int End, unsigned char* Memory)
{
   
   int   addr  = 0;

   unsigned char InsCode;

   INSTRUCTION*   Instruction = NULL;


   for (addr = Start; addr < End; addr++)
   {
      InsCode = Memory[addr];

      /* print the address of the instruction */

      printf("%04X %02X ", addr, InsCode);

      /* make sure the instruction code is within bounds */

      if (InsCode < sizeof InstructionTable / sizeof InstructionTable[0])
      {
         Instruction = &InstructionTable[InsCode]; 

         switch(Instruction->Operand)
         {
         case NONE:
            printf("        %-8s\n", Instruction->InstructionName);
            break;
            
         case IMMEDIATE:
            addr++;
            printf("%02X      %-8s #%02X\n", Memory[addr], 
               Instruction->InstructionName, 
               Memory[addr]);
            break;
            
         case ADDRESS:
            addr++;
            printf("%02X %02X   %-8s %04X\n", Memory[addr],
               Memory[addr+1],
               Instruction->InstructionName,
               LoadAddress(&Memory[addr]) );
            addr++;
            break;
         }
      }
      else
      {
         printf("        ???\n");
      }
   }
}

/*******************************************************************************
 * Function       :  HelpMenu
 * Description    :  
 ******************************************************************************/                  

void HelpMenu()
{
   printf ("h or help                        this help menu\n");
   printf ("load <filename> <memory_addr>    assemble file starting at address\n");
   printf ("go <memory addr>                 run program starting at address\n");
   printf ("s <memory addr>                  step one instruction, starting at address\n");
   printf ("s                                step one instruction, from current PC\n");
   printf ("d <start> <end>                  disassemble memory from start to end\n");
   printf ("r                                print contents of registers\n");
   printf ("m <start> <end>                  print memory from start to end\n");
   printf ("q or quit                        exits\n");
}


/*******************************************************************************
 * Function       :  Main
 * Description    :  
 ******************************************************************************/                  

void main ( int argc, char **argv)
{
   int   Exit     = FALSE;
   char  Seps[]   = " ,\t\n";
   char* Token    = NULL;

   char	Line1[MAX_LINE] = {0};
   char	Line2[MAX_LINE] = {0};

   char* LoadLocation = NULL;
   char* FileName     = NULL;
   char* Start        = NULL;
   char* End          = NULL;

   REGISTERS   Registers = {0};
   char        Memory[ADDRESS_SPACE] = {0};


   HelpMenu();

   /* loop until user asks to quit */
   while (!Exit)
   {
	   printf (">");
	   fgets (Line1, MAX_LINE, stdin);

      Token = strtok (Line1, Seps);

      /* parse the command line */
      if (NULL == Token)
      {
         continue;
      }

      if (0 == strcmp (Token, "load"))
      {
         /* see if there are any arguments specified */
         FileName = strtok (NULL, Seps);

         while (NULL == FileName)
         {
            printf ("File name :");
            fgets (Line1, MAX_LINE, stdin);
            FileName = strtok (Line1, Seps);
         }

         LoadLocation = strtok (NULL, Seps);

         while (NULL == LoadLocation)
         {
            printf ("Load to Memory Address :");
            fgets (Line2, MAX_LINE, stdin);
            LoadLocation = strtok (Line2, Seps);
         }

         ReadAssembly (Memory, strtol(LoadLocation, NULL, 16), FileName);
      }
      else if (0 == strcmp (Token, "go"))
      {
         /* see if there are any arguments specified */
         Token = strtok (NULL, Seps);

         while (NULL == Token)
         {
            printf ("address :");
            fgets (Line1, MAX_LINE, stdin);
            Token = strtok (Line1, Seps);
         }
         Run (&Registers, Memory, strtol(Token, NULL, 16));
      }
      else if (0 == strcmp (Token, "s"))
      {
         /* Step option specified */
         /* see if there are any arguments specified */
         Token = strtok (NULL, Seps);

         if (NULL != Token)
         {
            SetStep (&Registers, Memory, strtol(Token, NULL, 16));
         }
         else
         {
            Step (&Registers, Memory);
         }
      }
      else if (0 == strcmp (Token, "d"))
      {
         /* disassemble option specified */
         Start = strtok (NULL, Seps);

         while (NULL == Start)
         {
            printf ("Start Address :");
            fgets (Line1, MAX_LINE, stdin);
            Start = strtok (Line1, Seps);
         }

         End = strtok (NULL, Seps);
         while (NULL == End)
         {
            printf ("End Address :");
            fgets (Line2, MAX_LINE, stdin);
            End = strtok (Line2, Seps);
         }
         Disassemble (strtol(Start, NULL, 16), strtol(End, NULL, 16), Memory);
      }
      else if (0 == strcmp (Token, "r"))
      {
         DumpRegisters (&Registers);
      }
      else if (0 == strcmp (Token, "m"))
      {
         /* see if there are any arguments specified */
         Start = strtok (NULL, Seps);

         while (NULL == Start)
         {
            printf ("Start Address :");
            fgets (Line1, MAX_LINE, stdin);
            Start = strtok (Line1, Seps);
         }

         End = strtok (NULL, Seps);

         while (NULL == End)
         {
            printf ("End Address :");
            fgets (Line2, MAX_LINE, stdin);
            End = strtok (Line2, Seps);
         }

         DumpMemory (Memory, strtol(Start, NULL, 16), strtol(End, NULL, 16));
      }
      else if (0 == strcmp (Token, "quit") || 0 == strcmp (Token, "q"))
      {
         Exit = TRUE;
      }
      else if (0 == strcmp (Token, "help") || 0 == strcmp (Token, "h"))
      {
         HelpMenu();
      }
      else
      {
         printf ("Unknown command\n");
      }   
   }
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -