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

📄 assembler.cpp

📁 Ollydbg环境下的一款插件源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    ASSERT(strString);

    TOKENTYPE tt;

    tt = NextToken(fii, strString);
    if (TOK_DQUOTE == tt)
        return 1;

    Error(ERR_NEWLINEINCONST, NULL, fii->FileName, fii->LineNum);

    return 0;
}

int CAssembler::GetEnd(
    /* [in][out] */     FILEINFO *fii,
    /* [in] */          const MNEMONICTYPE MneType
)
{
    ASSERT(fii);

    int nRetResult = 0;

    int ch;
    TOKENTYPE state = TOK_UNKNOWN;

    for (;;)
    {
        switch (state)
        {
        case TOK_UNKNOWN:
            ch = getc(fii->fp);
            if ((' ' == ch) || ('\t' == ch))
                state = TOK_UNKNOWN;
            else if (';' == ch)
                state = TOK_LINECOMMENT;
            else if ('/' == ch)
                state = TOK_COMMENT0;
            else if ('\n' == ch)
            {
                ++fii->LineNum;
                goto Exit1;
            }
            else if (EOF == ch)
                goto Exit1;
            else
            {
                ungetc(ch, fii->fp);
                goto Exit0;
            }
            break;
        case TOK_LINECOMMENT:
            ch = getc(fii->fp);
            if (('\n' == ch) || (EOF == ch))
            {
                ++fii->LineNum;
                goto Exit1;
            }
            break;
        case TOK_COMMENT0:
            ch = getc(fii->fp);
            if ('/' == ch)
                state = TOK_LINECOMMENT;
            else if ('*' == ch)
                state = TOK_BLOCKCOMMENT;
            else
            {
                ungetc(ch, fii->fp);
                Error(ERR_BADBLOCKCMT, NULL, fii->FileName, fii->LineNum);
                goto Exit0;
            }
            break;
        case TOK_BLOCKCOMMENT:
            ch = getc(fii->fp);
            if ('\n' == ch)
                ++fii->LineNum;
            else if ('*' == ch)
            {
                ch = getc(fii->fp);
                if ('/' == ch)
                    state = TOK_UNKNOWN;
                else
                    ungetc(ch, fii->fp);
            }
            break;
       }
    }

Exit1:
    nRetResult = 1;
Exit0:
    if (0 == nRetResult)
    {
        Error(
            ERR_NOLINEEND,
            m_VM->GetMnemonicName(MneType),
            fii->FileName,
            fii->LineNum
        );
    }
    return nRetResult;
}

int CAssembler::AddLabel(
    /* [in] */          const FILEINFO *fii,
    /* [in] */          const UINT unLabelNameSize,
    /* [in] */          const char *szLabelName
)
{
    ASSERT(fii);
    ASSERT(szLabelName);

    LABELINFO li;

    if ((strlen(szLabelName) + 1) != unLabelNameSize)
        return 0;

    li.Addr = m_unCodeLen;
    li.Name = szLabelName;
    li.Refed = 0;
    strcpy(li.FileName, fii->FileName);
    li.LineNum = fii->LineNum;
    m_pLabelList->AddTail(li);

    return 1;
}

int CAssembler::FindLabelName(
    /* [size_is][in] */ const UINT unLabelNameSize,
    /* [in] */          const char *szLabelName,
    /* [out] */         LABELINFO *LabelFound,
    /* [out] */         int *nLblIdx
) const
{
    ASSERT(szLabelName);

    int i;
    int nCount;
    LABELINFO li;

    if ((strlen(szLabelName) + 1) != unLabelNameSize)
        return 0;

    nCount = m_pLabelList->GetCount();
    for (i = 0; i < nCount; ++i)
    {
        li = m_pLabelList->GetAt(m_pLabelList->FindIndex(i));
        if (0 == strcomp(li.Name, szLabelName))
        {
            if (LabelFound)
                *LabelFound = li;
            if (nLblIdx)
                *nLblIdx = i;
            return 1;
        }
    }

    return 0;
}

int CAssembler::DeclareLabel(
    /* [in] */          const FILEINFO *fii,
    /* [in] */          const UINT unLabelNameSize,
    /* [in] */          const char *szLabelName
)
{
    ASSERT(fii);
    ASSERT(szLabelName);

    int nRetResult = 0;
    int nRetCode;

    LABELINFO li;

    OM_PROCESS_ERROR((strlen(szLabelName) + 1) == unLabelNameSize);

    nRetCode = FindLabelName(unLabelNameSize, szLabelName, &li);
    if (nRetCode)   // found, redefined label
    {
        Error(
            ERR_REDEFLABEL,
            szLabelName,
            fii->FileName,
            fii->LineNum
        );
    }
    AddLabel(fii, unLabelNameSize, szLabelName);

    nRetResult = 1;
Exit0:
    return nRetResult;
}

int CAssembler::SetLabelRef(
    /* [size_is][in] */ const UINT unLabelNameSize,
    /* [in] */          const char *szLabelName
)
{
    ASSERT(szLabelName);

    int nRetResult = 0;
    int nRetCode;

    int nLblIdx;
    LABELINFO li;

    OM_PROCESS_ERROR((strlen(szLabelName) + 1) == unLabelNameSize);

    nRetCode = FindLabelName(unLabelNameSize, szLabelName, &li, &nLblIdx);
    if (0 == nRetCode)
        goto Exit0;

    if (0 == li.Refed)
    {
        li.Refed = 1;
        m_pLabelList->SetAt(m_pLabelList->FindIndex(nLblIdx), li);
    }

    nRetResult = 1;
Exit0:
    return nRetResult;
}

int CAssembler::ProcessInclude(
    /* [in][out] */     FILEINFO *fii,
    /* [in] */          const PASSTYPE pt
)
{
    ASSERT(fii);

    int nRetResult = 0;
    int nRetCode;

    CString strString;
    FILE *fp_include = NULL;

    nRetCode = GetString(fii, &strString);
    OM_PROCESS_ERROR(nRetCode);

    nRetCode = GetEnd(fii, MC_INCLUDE);
    OM_PROCESS_ERROR(nRetCode);

    fp_include = fopen((LPCTSTR)strString, "rt");
    if (NULL == fp_include)
    {
        Error(ERR_NOINCLUDEFILE, strString, fii->FileName, fii->LineNum);
        FatalError();
        goto Exit0;
    }
    if (fp_include)
    {
        fclose(fp_include);
        fp_include = NULL;
    }

    switch (pt)
    {
    case PT_PASS1:
        MakeSymbolTable(strString.GetLength() + 1, strString);
        break;
    case PT_PASS2:
        GenerateCodes(strString.GetLength() + 1, strString);
        break;
    default:
        goto Exit0;
    }

    nRetResult = 1;
Exit0:
    if (0 == nRetCode)
    {
        Error(
            ERR_ERRORONID,
            m_VM->GetMnemonicName(MC_INCLUDE),
            fii->FileName,
            fii->LineNum
        );
    }
    return nRetResult;
}

void CAssembler::CheckLabelRef()
{
    int i;
    int nCount;
    LABELINFO li;

    nCount = m_pLabelList->GetCount();
    for (i = nCount - 1; i >= 0; --i)
    {
        li = m_pLabelList->GetAt(m_pLabelList->FindIndex(i));
        if (0 == li.Refed)
        {
            Warning(WARN_UNREFLABEL, li.Name, li.FileName, li.LineNum);
        }
    }
}

int CAssembler::CheckCodeCapacity(
    /* [in] */          const int nExpectedLen
)
{
    int nRetResult = 0;

    if (m_unCodeLen + nExpectedLen >= m_unCodeSize)
    {
        m_Code = (unsigned char *)realloc(
            m_Code,
            m_unCodeSize + CODE_INC_SIZE
        );
        OM_PROCESS_ERROR(m_Code);
        m_unCodeSize += CODE_INC_SIZE;
    }

    nRetResult = 1;
Exit0:
    return nRetResult;
}

int CAssembler::CheckDataCapacity(
    /* [in] */          const int nExpectedLen
)
{
    int nRetResult = 0;

    if (m_unDataLen + nExpectedLen >= m_unDataSize)
    {
        m_Data = (char *)realloc(
            m_Data,
            m_unDataSize + DATA_INC_SIZE
        );
        OM_PROCESS_ERROR(m_Data);
        m_unDataSize += DATA_INC_SIZE;
    }

    nRetResult = 1;
Exit0:
    return nRetResult;
}

UINT CAssembler::GetCodeSize() const
{
    return m_unCodeLen;
}

int CAssembler::GetCodeContents(
    /* [out] */           unsigned char *ucCode
) const
{
    ASSERT(ucCode);

    if ((0 == m_unCodeLen) || (NULL == m_Code))
        return 0;

    memcpy(ucCode, m_Code, m_unCodeLen);

    return 1;
}

UINT CAssembler::GetDataSize() const
{
    return m_unDataLen;
}

int CAssembler::GetDataContents(
    /* [out] */         char *ucData
) const
{
    ASSERT(ucData);

    if ((0 == m_unDataLen) || (NULL == m_Data))
        return 0;

    memcpy(ucData, m_Data, m_unDataLen);

    return 1;
}

int CAssembler::Match(
    /* [in][out] */     FILEINFO *fii,
    /* [in] */          const TOKENTYPE TokType,
    /* [out] */         CString *strTokenName
)
{
    ASSERT(fii);

    TOKENTYPE tt;
    CString strTokenName0;
    CString strExpected;

    tt = NextToken(fii, &strTokenName0);
    if (TokType != tt)
    {
        switch (TokType)
        {
        case TOK_TOKEN:
            strExpected = "token";
            break;
        case TOK_REGISTER:
            strExpected = "register";
            break;
        case TOK_LABEL:
            strExpected = "label";
            break;
        case TOK_DIGIT_DEC:
        case TOK_DIGIT_HEX:
            strExpected = "digit";
            break;
        case TOK_LINECOMMENT:
            strExpected = "line comment";
            break;
        case TOK_BLOCKCOMMENT:
            strExpected = "block comment";
            break;
        case TOK_COMMA:
            strExpected = ",";
            break;
        case TOK_CRLF:
            strExpected = "\\r\\n";
            break;
        case TOK_DQUOTE:
            strExpected = "\"\"";
            break;
        }
        Error(ERR_EXPECTED, strExpected, fii->FileName, fii->LineNum);
        return 0;
    }

    if (strTokenName)
        *strTokenName = strTokenName0;

    return 1;
}

int CAssembler::I(
    /* [in][out] */     FILEINFO *fii,
    /* [in] */          const MNEMONICTYPE MneType
)
{
    ASSERT(fii);

    int nRetResult = 0;
    int nRetCode;

    AddOpcode(MneType);

    nRetCode = GetEnd(fii, MneType);
    OM_PROCESS_ERROR(nRetCode);

    nRetResult = 1;
Exit0:
    return nRetResult;
}

int CAssembler::IL(
    /* [in][out] */     FILEINFO *fii,
    /* [in] */          const MNEMONICTYPE MneType
)
{
    ASSERT(fii);

    int nRetResult = 0;
    int nRetCode;

    CString strTokenName;
    TOKENTYPE tt;
    LABELINFO li;
    int nLblIdx;
    int unTokenNameSize;

    AddOpcode(MneType);

    tt = NextToken(fii, &strTokenName);
    if (TOK_TOKEN != tt)
    {
        Error(
            ERR_ERRORONID,
            m_VM->GetMnemonicName(MneType),
            fii->FileName,
            fii->LineNum

⌨️ 快捷键说明

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