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

📄 zvm.cpp

📁 自己开发的类汇编语言脚本语言虚拟机
💻 CPP
📖 第 1 页 / 共 5 页
字号:

                    // 跳转到返回地址

                    g_Script.InstrStream.iCurrInstr = ReturnAddr.iInstrIndex;

                    // 打印提示信息

                    printf ( " %d", ReturnAddr.iInstrIndex );

                    break;
                }

                case INSTR_CALLHOST:
                {
                    PrintOpValue ( 0 );

                    break;
                }

                // ---- 其他指令

                case INSTR_PAUSE:
                {
                    // 检测是否脚本正在暂停状态

                    if ( g_Script.iIsPaused == 1 )
                    {
                        break;
                    }

                    // 取得暂停时间间隔

                    int     iPauseDuration      = ResolveOpAsInt ( 0 );

                    // 计算暂停终止时间

                    g_Script.iPauseEndTime      = GetTickCount() + iPauseDuration;

                    // 暂停脚本

                    g_Script.iIsPaused  = true;

                    // 打印暂停间隔

                    break;
                }

                case INSTR_EXIT:
                {
                    // 取得操作码

                    Value   ExitCode    = ResolveOpValue ( 0 );
                    int     iExitCode   = ExitCode.iIntLiteral;

                    // 打破指令循环

                    bExitLoop   = true;

                    // 打印结束码

                    printf ( " " );
                    PrintOpValue ( 0 );
                    break;
                }
            }

            printf ( "\n" );

            // 比较当前指令指针计数的值与循环开始时保存在 iCurrInstr 中的值,以决定是否增加指令计数

            if ( g_Script.InstrStream.iCurrInstr == iCurrInstr )
            {
                // 相等则增加指令指针计数

                ++ g_Script.InstrStream.iCurrInstr;
            }

            // 检测循环结束标志

            if ( bExitLoop == true )
            {
                break;
            }
        }
    }

    /**********************************************************************************************
    *
    *   ResetScript ()
    *
    *   复位脚本
    */

    void ResetScript ()
    {
        // 取得函数_Main的索引

        int iMainFuncIndex = g_Script.iMainFuncIndex;

        // 如果函数表存在就设置入口点

        if ( g_Script.pFuncTable != 0 )
        {
            // 如果_Main()函数存在则寻找入口点

            if ( g_Script.iIsMainFuncPresent )
            {
                g_Script.InstrStream.iCurrInstr = g_Script.pFuncTable [ iMainFuncIndex ].iEntryPoint;
            }
        }

        // 清空堆栈

        g_Script.Stack.iFrameIndex  = 0;
        g_Script.Stack.iTopIndex    = 0;

        // 清空整个堆栈

        for ( int iCurrElmntIndex = 0; iCurrElmntIndex < g_Script.Stack.iSize ; ++ iCurrElmntIndex )
        {
            g_Script.Stack.pElements [ iCurrElmntIndex ].iType = OP_TYPE_NULL;
        }

        // 清空脚本暂停标志

        g_Script.iIsPaused = 0;

        // 为全局变量分配空间

        PushFrame ( g_Script.iGlobalDataSize );

        // 如果_Main()出现,_Main()堆栈框架入栈

        if ( g_Script.iIsMainFuncPresent )
        {
            PushFrame ( g_Script.pFuncTable [ iMainFuncIndex ].iStackFrameSize + 1 );
        }
    }

    /**********************************************************************************************
    *
    *   CoerceValueToInt ()
    *
    *   把 Value 值强制转换成 int
    */

    int CoerceValueToInt ( Value &val )
    {
        // 判定当前值是什么类型

        switch ( val.iType )
        {
            // 整形直接返回

            case OP_TYPE_INT:
                return val.iIntLiteral;

            // 浮点数强制转换成整数返回
            
            case OP_TYPE_FLOAT:
                return static_cast<int> ( val.fFloatLiteral );

            // 字符串转换成整数返回

            case OP_TYPE_STRING:
                return atoi ( val.pstrStringLiteral );

            // 其余情况都是不合法的

            default:
                return 0;
        }
    }
  
    /**********************************************************************************************
    *
    *   CoerceValueToFloat ()
    *
    *   把 Value 值强制转换成 float
    */

    float CoerceValueToFloat ( Value &val )
    {
        // 判定当前值是什么类型

        switch ( val.iType )
        {
            // 整形强制转换成浮点数返回

            case OP_TYPE_INT:
                return static_cast<float> ( val.iIntLiteral );

            // 浮点数直接返回
            
            case OP_TYPE_FLOAT:
                return val.fFloatLiteral;

            // 字符串转换成浮点数返回

            case OP_TYPE_STRING:
                return static_cast<float> ( atof ( val.pstrStringLiteral ) );

            // 其余情况都是不合法的

            default:
                return 0;
        }
    }

    /**********************************************************************************************
    *
    *   CoerceValueToString ()
    *
    *   把 Value 值强制转换成字符串
    */

    char* CoerceValueToString ( Value &val )
    {
        char *pstrCoercion;
        if ( val.iType != OP_TYPE_STRING )
            pstrCoercion = new char [ MAX_COERCION_STRING_SIZE + 1 ];

        // 判定当前值是什么类型

        switch ( val.iType )
        {
            // 整形转换成字符串返回

            case OP_TYPE_INT:
                _itoa_s ( val.iIntLiteral, pstrCoercion, MAX_COERCION_STRING_SIZE + 1, 10 );
                return pstrCoercion;

            // 浮点数转换成字符串返回
            
            case OP_TYPE_FLOAT:
                sprintf_s ( pstrCoercion, MAX_COERCION_STRING_SIZE + 1, "%f", val.fFloatLiteral );
                return pstrCoercion;

            // 字符串直接返回

            case OP_TYPE_STRING:
                return val.pstrStringLiteral;

            // 其余情况都是不合法的

            default:
                return 0;
        }
    }

    /**********************************************************************************************
    *
    *   CopyValue ()
    *
    *   拷贝值
    */

    void CopyValue ( Value* pDest, Value &Source )
    {
        // 如果目的值是字符串类型,先释放原字符串

        if ( pDest->iType == OP_TYPE_STRING )
            delete pDest->pstrStringLiteral;

        // 拷贝值

        *pDest = Source;

        // 如果是字符串类型,拷贝字符串

        if ( pDest->iType == OP_TYPE_STRING )
        {
            size_t iBufSize = strlen ( Source.pstrStringLiteral ) + 1;
            pDest->pstrStringLiteral = new char [ iBufSize ];
            strcpy_s ( pDest->pstrStringLiteral, iBufSize, Source.pstrStringLiteral );
        }
    }

    /**********************************************************************************************
    *
    *   GetOpType ()
    *
    *   取得操作数类型
    */

    int GetOpType ( int iOpIndex )
    {
        // 取得指令索引

        int iCurrInstr = g_Script.InstrStream.iCurrInstr;

        // 返回操作数类型

        return g_Script.InstrStream.pInstrs [ iCurrInstr ].pOpList [ iOpIndex ].iType;
    }

    /**********************************************************************************************
    *
    *   ResolveStackIndex()
    *
    *   把负的堆栈索引转换成指向堆栈上端
    *   把正的堆栈索引转换成指向堆栈低端
    */

    int ResolveStackIndex ( int iIndex ) 
    {
        // 如果索引小于0则将其转换成正的索引

        if ( iIndex < 0 )
            iIndex  += g_Script.Stack.iFrameIndex;

        return iIndex;
    }
        
    /**********************************************************************************************
    *
    *   ResolveOpStackIndex ()
    *
    *   把操作数解析成堆栈索引
    */

    int ResolveOpStackIndex ( int iOpIndex )
    {
        // 取得当前指令索引

        int iCurrInstr = g_Script.InstrStream.iCurrInstr;

        // 取得当前操作数值

        Value val = g_Script.InstrStream.pInstrs [ iCurrInstr ].pOpList [ iOpIndex ];

        // 根据操作数类型解析堆栈索引

        switch ( val.iType )
        {
            // 绝对堆栈索引直接返回

            case OP_TYPE_ABS_STACK_INDEX:
                return val.iStackIndex;

            // 相对堆栈索引,需要转换成绝对堆栈索引

            case OP_TYPE_REL_STACK_INDEX:
            {
                // 取得基址

                int iBaseIndex = val.iStackIndex;

                // 取得变量的堆栈索引

                int iOffsetIndex = val.iOffsetIndex;

                // 取得索引变量的值

                Value StackValue = GetStackValue ( iOffsetIndex );

                // 计算绝对索引

                return iBaseIndex + StackValue.iIntLiteral;
            }

            // 其他情况返回0

            default:
                return 0;
        }
    }

    /**********************************************************************************************
    *
    *   ResolveOpValue ()
    *
    *   把操作数解析成 Value 对象
    */

    Value ResolveOpValue ( int iOpIndex )
    {
        // 取得当前指令索引

        int iCurrInstr = g_Script.InstrStream.iCurrInstr;

        // 取得当前操作数类型

        Value OpValue = g_Script.InstrStream.pInstrs [ iCurrInstr ].pOpList [ iOpIndex ];

        // 根据类型来决定返回什么内容

        switch ( OpValue.iType )
        {
            // 是堆栈索引,解析它

            case OP_TYPE_ABS_STACK_INDEX:
            case OP_TYPE_REL_STACK_INDEX:
            {
                // 解析堆栈索引,并用它来返回响应的堆栈元素

                int iAbsIndex = ResolveOpStackIndex ( iOpIndex );
                return GetStackValue ( iAbsIndex );
            }

            // 是_RetVal

            case OP_TYPE_REG:
      

⌨️ 快捷键说明

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