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

📄 debugcmd.cpp

📁 这是一个软件水平资格考试中使用的CASL汇编语言的编译器,实现文件中包括一个编译器,一个虚拟机,一个类似于Debug的调试器.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    }
    else
    {
      goto HandleALabelAndNLabel_Failed;
    }

  }
  else
  {
    goto HandleALabelAndNLabel_Failed;
  }
  
  return STATUS_OK;

HandleALabelAndNLabel_Failed:
  m_pVM->m_errorHandler.WriteErrorMessage("Handle EA Failed!");
  return STATUS_FAILED;
}


/////////////////////////////////////////////////////////////////////////
//函数名称:HandleAddress
//函数功能:-----处理A命令中所获得用户输入的指令串中的操作数中的地址偏移部分---
//入口参数:无
//出口参数:S2& argDataOut--------------存放输入的地址偏移数据
//返回值:int-------------STATUS_OK表示操作成功,其他表示操作失败
//开发人员:杨军
//开发日期:2004-4-24
//修改人员:
//修改日期:
/////////////////////////////////////////////////////////////////////////
int CDebugCmd::HandleAddress(S2& argDataOut)
{
  argDataOut = 0;

  //-----------------------变量定义部分 begins---------------------------
  char chCmd;
  //-----------------------变量定义部分 ends-----------------------------
  
  //---------------掠过空格符-------------------------------------------
  PassBySpace();
  //---------------先处理ALabelAddress----------------------------------------
  //注意:这里与Debug下一样,将用户输入的数字默认为十六进制格式!!!
  //     以便于统一处理!!
  chCmd = GetNextChar();
  if (chCmd >= '0' && chCmd <= '9' || chCmd >= 'A' && chCmd <= 'F')
  {
    chCmd = chCmd - '0';
    if (chCmd > 9)
    {
      chCmd -= ('A' - '0');
    }
    argDataOut = (argDataOut << 4) + chCmd;
    chCmd = GetNextChar();
    while (chCmd >= '0' && chCmd <= '9' || chCmd >= 'A' && chCmd <= 'F')
    {
       chCmd = chCmd - '0';
       if (chCmd > 9)
       {
        chCmd -= ('A' - '0');
       }
       argDataOut = (argDataOut << 4) + chCmd;
       chCmd = GetNextChar();
    } 
    if (chCmd != EOI)
    {
      m_iPos--;
    }
    
  }
  else
  {
    goto HandleAddress_Failed; 
  }
  return STATUS_OK;

HandleAddress_Failed:
  m_pVM->m_errorHandler.WriteErrorMessage("Handle Address Failed");
  return STATUS_FAILED;
}


/////////////////////////////////////////////////////////////////////////
//函数名称:HandleGR
//函数功能:-----处理A命令中所获得用户输入的指令串中的操作数中的GR部分--------
//入口参数:无
//出口参数:无
//返回值:int-------------STATUS_OK表示操作成功,其他表示操作失败
//开发人员:杨军
//开发日期:2004-4-24
//修改人员:
//修改日期:
/////////////////////////////////////////////////////////////////////////
int CDebugCmd::HandleGR()
{
  PassBySpace();

  if (GetNextChar() == 'G')
  {
    if (GetNextChar() == 'R')
    {
      m_asmCode.bGRUse = true;
      switch (GetNextChar())
      {
      case '0':       //GR0
        m_asmCode.iGRNumber = 0;
        break;
      case '1':       //GR1
        m_asmCode.iGRNumber = 1;
        break;
      case '2':       //GR2
        m_asmCode.iGRNumber = 2;
        break;
      case '3':       //GR3
        m_asmCode.iGRNumber = 3;
        break;
      case '4':       //GR4
        m_asmCode.iGRNumber = 4;
        break;
      default:        //unknown GR
        m_asmCode.bGRUse = false;
        goto HandleGR_Failed;
        break;
      }
    }
    else
    {
      goto HandleGR_Failed;
    }
  }
  else
  {
    goto HandleGR_Failed;
  }

  return STATUS_OK;

HandleGR_Failed:
  m_pVM->m_errorHandler.WriteErrorMessage("Handle GR Failed!");
  return STATUS_FAILED;
}

/////////////////////////////////////////////////////////////////////////
//函数名称:DbgCmd_Dump
//函数功能:---------------------Dump    功能的执行函数------------------------
//入口参数:U2 argStartAddress-------Dump区域的起始地址
//          U2 argEndAddress---------Dump区域的终止地址
//出口参数:无
//返回值:int-------------STATUS_OK表示操作成功,其他表示操作失败
//开发人员:杨军
//开发日期:2004-4-27
//修改人员:
//修改日期:
/////////////////////////////////////////////////////////////////////////
int CDebugCmd::DbgCmd_Dump(U2 argStartAddress, U2 argEndAddress) 
{
  //需要编写Dump功能的执行函数了!!!
  //显示数据的格式如下;
  //       Ascii码                                              对应可显示字符 
  //地址1: xx xx xx xx xx xx xx xx--xx xx xx xx xx xx xx xx    cccccccccccccccc
  //地址2: xx xx xx xx xx xx xx xx--xx xx xx xx xx xx xx xx    cccccccccccccccc
  //......
  //地址8: xx xx xx xx xx xx xx xx--xx xx xx xx xx xx xx xx    cccccccccccccccc
  //设置输出数字的显示格式是十六进制
  long lFlag = 0;
  int i = 0;
  S1  s1Temp = 0;

  U2 u2Addr = argStartAddress;
  cout << hex;
  
  while (u2Addr + 15 <= argEndAddress)
  {
    cout << u2Addr << ":" <<  '\t';


    lFlag = cout.flags();
    //设置新的数据输出格式

    for (i = 0; i < 16; i++)
    {
      //如果要显示的字节单元的数据小于0x0f,则补足一个'0'以保证显示的数据的格式的一致
      /*
      if (!((*(m_pVM->m_pVMMemoryBase + u2Addr + i)) & 0xf0))
      {
        cout << '0';
      }
      */
      cout << uppercase;
      cout.width(2);
      cout.fill('0');
      cout << (int)(unsigned char)(*(m_pVM->m_pVMMemoryBase + u2Addr + i)) << " ";
      if (7 == i)
      {
        cout << "--" << " ";
      }
    }

    //恢复以前的数据输出格式
    cout.flags(lFlag);
    
    cout << '\t';
    //输出对应的可显示字符
    for (i = 0; i < 16; i++)
    {
      s1Temp = *(m_pVM->m_pVMMemoryBase + u2Addr + i);
      if (s1Temp >= '0' && s1Temp <='9' ||
          s1Temp >= 'a' && s1Temp <= 'z' ||
          s1Temp >= 'A' && s1Temp <= 'Z' ||
          ispunct(s1Temp))
      {
        cout << s1Temp;
      }
      else
      {
        cout << '.';
      }
    }

    u2Addr += 16;
    cout << endl;
  }
  
  lFlag = cout.flags();
  //输出剩下的不足一行的数据
  if (u2Addr < argEndAddress)
  {
    cout << u2Addr << ":" <<  '\t';
    i = 0;
    while (u2Addr + i <= argEndAddress)
    {
      /*
      if ((*(m_pVM->m_pVMMemoryBase + u2Addr + i)) < 0x0f)
      {
        cout << '0';
      }
      */
      cout << uppercase;
      cout.width(2);
      cout.fill('0');
      cout << (int)(unsigned char)*(m_pVM->m_pVMMemoryBase + u2Addr + i) << " ";
      if (7 == i)
      {
        cout << "--" << " ";
      }
      i++;
    }
    cout.flags(lFlag);
    //将后面的空格补齐, 以保证格式的一致
    do 
    {
      cout << "  " << " ";
      if (7 == i)
      {
        cout << "  " << " ";
      }

    } while (++i < 16);

    cout << '\t';

    i = 0;
    while (u2Addr + i <= argEndAddress)
    {
      s1Temp = *(m_pVM->m_pVMMemoryBase + u2Addr + i);
      if (s1Temp >= '0' && s1Temp <='9' ||
          s1Temp >= 'a' && s1Temp <= 'z' ||
          s1Temp >= 'A' && s1Temp <= 'Z' ||
          ispunct(s1Temp))
      {
        cout << s1Temp;
      }
      else
      {
        cout << '.';
      }
      
      i++;
    }
    cout << endl;
  }
  //恢复输出数字的显示格式是十进制
  cout << dec;
  return STATUS_OK;
}

/////////////////////////////////////////////////////////////////////////
//DbgCmd_Go
//函数功能:---------------------Go    功能的执行函数------------------------
//入口参数:U2 argStartAddress-------Go功能将要执行的指令的起始地址
//          U2 argEndAddress---------Go功能要执行的指令的终止地址
//出口参数:无
//返回值:int-------------STATUS_OK表示操作成功,其他表示操作失败
//开发人员:杨军
//开发日期:2004-4-27
//修改人员:
//修改日期:
/////////////////////////////////////////////////////////////////////////
int CDebugCmd::DbgCmd_Go(U2 argStartAddress, U2 argEndAddress) 
{
  m_pVM->m_PC = argStartAddress;
  while (m_pVM->m_PC <= argEndAddress)
  {
    //从虚拟机的工作内存中读出当前PC指向的指令内容
    m_pVM->ReadInstruction();
    //执行刚刚读出的指令
    //如果当前指令是Exit,则Go功能执行可以结束
    if (CASL_EXIT == m_pVM->m_objInstruction.instructionType)
    {
      m_pVM->IncrementPC();
      break;
    }
    if (STATUS_OK != m_pVM->ExecuteInstruction())
    {
      goto DbgCmd_Go_Failed;
    }
  }

  return STATUS_OK;

DbgCmd_Go_Failed:
  return STATUS_FAILED;
}


/////////////////////////////////////////////////////////////////////////
//函数名称:DbgCmd_Proceed
//函数功能:---------------------Proceed 功能的执行函数------------------------
//入口参数:U2 argStartAddress-------Proceed功能将要执行的指令的起始地址
//          U2 argInstNumber---------Proceed功能要执行的指令的条数
//出口参数:无
//返回值:int-------------STATUS_OK表示操作成功,其他表示操作失败
//开发人员:杨军
//开发日期:2004-4-28
//修改人员:
//修改日期:
/////////////////////////////////////////////////////////////////////////
int CDebugCmd::DbgCmd_Proceed(U2 argStartAddress, U2 argInstNumber) 
{
  //-------------变量定义部分 begins------------------------------------
  int i = 0;
  //-------------变量定义部分 ends  ------------------------------------

  m_pVM->m_PC = argStartAddress;

  for (i = 0; i < argInstNumber; i++)
  {
    m_pVM->ReadInstruction();
    //遇到了Exit指令,可以退出Proceed功能了
    if (CASL_EXIT == m_pVM->m_objInstruction.instructionType)
    {
      m_pVM->IncrementPC();
      goto DbgCmd_Proceed_Ok;
    }
    else
    {
      //对于Call指令,连续执行直到遇到Ret指令后才将Call到Ret之间的指令序列
      //计为一条批蛉 
      if (CASL_CALL == m_pVM->m_objInstruction.instructionType)
      {
        //执行Call指令
        if (STATUS_OK != m_pVM->ExecuteInstruction())
        {
          goto DbgCmd_Proceed_Failed;
        }
        //指行Call指令指向的过程体
        do 
        {
          m_pVM->ReadInstruction();
          if (CASL_EXIT == m_pVM->m_objInstruction.instructionType)
          {
            m_pVM->IncrementPC();
            goto DbgCmd_Proceed_Ok;
          }
          if (STATUS_OK != m_pVM->ExecuteInstruction())
          {
            goto DbgCmd_Proceed_Failed;
          }
          //遇到了Ret指令了,可以退出内循环了
          if (CASL_RET == m_pVM->m_objInstruction.instructionType)
          {
            break;
          }
        } while (1);
      }
      else
      {
        if (STATUS_OK != m_pVM->ExecuteInstruction())
        {
          m_pVM->m_errorHandler.WriteErrorMessage("执行指令失败");
          goto DbgCmd_Proceed_Failed;
        }
      }
    }
  }

  //显示当前运行上下文状态
  ShowRunContext();
  //显示下一条指令
  if (STATUS_OK != ShowNextInstruction())
  {
    goto DbgCmd_Proceed_Failed;
  }
DbgCmd_Proceed_Ok:
  return STATUS_OK;

DbgCmd_Proceed_Failed:
  return STATUS_FAILED;
}

/////////////////////////////////////////////////////////////////////////
//函数名称:DbgCmd_Trace
//函数功能:---------------------Trace 功能的执行函数------------------------
//入口参数:U2 argStartAddress-------Trace功能将要执行的指令的起始地址
//          U2 argInstNumber---------Trace功能要执行的指令的条数
//出口参数:无
//返回值:int-------------STATUS_OK表示操作成功,其他表示操作失败
//开发人员:杨军
//开发日期:2004-4-28
//修改人员:
//修改日期:
/////////////////////////////////////////////////////////////////////////
int CDebugCmd::DbgCmd_Trace(U2 argStartAddress, U2 argInstNumber) 
{
  //-------------变量定义部分 begins------------------------------------
  int i = 0;
  //-------------变量定义部分 ends  ------------------------------------
  
  m_pVM->m_PC = argStartAddress;
  for (i = 0; i < argInstNumber; i++)
  {
    m_pVM->ReadInstruction();
    //遇到了Exit指令,可以停止Trace功能了!
    if (CASL_EXIT == m_pVM->m_objInstruction.instructionType)
    {
      goto DbgCmd_Trace_Ok;
    }
    else
    {
      //如果执行当前指令失败的话
      if (STATUS_OK != m_pVM->ExecuteInstruction())
      {
        goto DbgCmd_Trace_Failed;
      }
    }
  }

  //显示当前运行上下文状态
  ShowRunContext();
  //显示下一条指令
  if (STATUS_OK != ShowNextInstruction())
  {
    goto DbgCmd_Trace_Failed;
  }
DbgCmd_Trace_Ok:
  return STATUS_OK;

DbgCmd_Trace_Failed:
  return STATUS_FAILED;
}


/////////////////////////////////////////////////////////////////////////
//函数名称:DbgCmd_Unassemble
//函数功能:---------------------Unassemble 功能的执行函数------------------------
//入口参数:U2 argStartAddress-------Unassemble区域的起始地址
//          U2 argEndAddress---------Unassemble区域的终止地址
//出口参数:无
//返回值:int-------------STATUS_OK表示操作成功,其他表示操作失败
//开发人员:杨军

⌨️ 快捷键说明

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