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

📄 xasm.cpp

📁 < Game Script Mastery>> source code
💻 CPP
📖 第 1 页 / 共 5 页
字号:

            if ( ! ( g_ppstrSourceCode [ iCurrLineIndex ] = ( char * ) malloc ( MAX_SOURCE_LINE_SIZE + 1 ) ) )
                ExitOnError ( "Could not allocate space for source line" );

            // Read in the current line

            fgets ( g_ppstrSourceCode [ iCurrLineIndex ], MAX_SOURCE_LINE_SIZE, g_pSourceFile );

            // Strip comments and trim whitespace

            StripComments ( g_ppstrSourceCode [ iCurrLineIndex ] );
            TrimWhitespace ( g_ppstrSourceCode [ iCurrLineIndex ] );

            // Make sure to add a new newline if it was removed by the stripping of the
            // comments and whitespace. We do this by checking the character right before
            // the null terminator to see if it's \n. If not, we move the terminator over
            // by one and add it. We use strlen () to find the position of the newline
            // easily.

            int iNewLineIndex = strlen ( g_ppstrSourceCode [ iCurrLineIndex ] ) - 1;
            if ( g_ppstrSourceCode [ iCurrLineIndex ] [ iNewLineIndex ] != '\n' )
            {
                g_ppstrSourceCode [ iCurrLineIndex ] [ iNewLineIndex + 1 ] = '\n';
                g_ppstrSourceCode [ iCurrLineIndex ] [ iNewLineIndex + 2 ] = '\0';
            }
        }

        // Close the source file

        fclose ( g_pSourceFile );
    }

    /******************************************************************************************
    *
    *   InitInstrTable ()
    *
    *   Initializes the master instruction lookup table.
    */

    void InitInstrTable ()
    {
        // Create a temporary index to use with each instruction

        int iInstrIndex;

        // The following code makes repeated calls to AddInstrLookup () to add a hardcoded
        // instruction set to the assembler's vocabulary. Each AddInstrLookup () call is
        // followed by zero or more calls to SetOpType (), whcih set the supported types of
        // a specific operand. The instructions are grouped by family.

        // ---- Main

        // Mov          Destination, Source

        iInstrIndex = AddInstrLookup ( "Mov", INSTR_MOV, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // ---- Arithmetic

         // Add         Destination, Source

        iInstrIndex = AddInstrLookup ( "Add", INSTR_ADD, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // Sub          Destination, Source

        iInstrIndex = AddInstrLookup ( "Sub", INSTR_SUB, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // Mul          Destination, Source

        iInstrIndex = AddInstrLookup ( "Mul", INSTR_MUL, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // Div          Destination, Source

        iInstrIndex = AddInstrLookup ( "Div", INSTR_DIV, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // Mod          Destination, Source

        iInstrIndex = AddInstrLookup ( "Mod", INSTR_MOD, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // Exp          Destination, Source

        iInstrIndex = AddInstrLookup ( "Exp", INSTR_EXP, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // Neg          Destination

        iInstrIndex = AddInstrLookup ( "Neg", INSTR_NEG, 1 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // Inc          Destination

        iInstrIndex = AddInstrLookup ( "Inc", INSTR_INC, 1 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // Dec          Destination

        iInstrIndex = AddInstrLookup ( "Dec", INSTR_DEC, 1 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // ---- Bitwise

        // And          Destination, Source

        iInstrIndex = AddInstrLookup ( "And", INSTR_AND, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // Or           Destination, Source

        iInstrIndex = AddInstrLookup ( "Or", INSTR_OR, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // XOr          Destination, Source

        iInstrIndex = AddInstrLookup ( "XOr", INSTR_XOR, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // Not          Destination

        iInstrIndex = AddInstrLookup ( "Not", INSTR_NOT, 1 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // ShL          Destination, Source

        iInstrIndex = AddInstrLookup ( "ShL", INSTR_SHL, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // ShR          Destination, Source

        iInstrIndex = AddInstrLookup ( "ShR", INSTR_SHR, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );

        // ---- String Manipulation

        // Concat       String0, String1

        iInstrIndex = AddInstrLookup ( "Concat", INSTR_CONCAT, 2 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG |
                                    OP_FLAG_TYPE_STRING );

       // GetChar      Destination, Source, Index

        iInstrIndex = AddInstrLookup ( "GetChar", INSTR_GETCHAR, 3 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG |
                                    OP_FLAG_TYPE_STRING );
        SetOpType ( iInstrIndex, 2, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG |
                                    OP_FLAG_TYPE_INT );

        // SetChar      Destination, Index, Source

        iInstrIndex = AddInstrLookup ( "SetChar", INSTR_SETCHAR, 3 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG |
                                    OP_FLAG_TYPE_INT );
        SetOpType ( iInstrIndex, 2, OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG |
                                    OP_FLAG_TYPE_STRING );

        // ---- Conditional Branching

        // Jmp          Label

        iInstrIndex = AddInstrLookup ( "Jmp", INSTR_JMP, 1 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_LINE_LABEL );

        // JE           Op0, Op1, Label

        iInstrIndex = AddInstrLookup ( "JE", INSTR_JE, 3 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 2, OP_FLAG_TYPE_LINE_LABEL );

        // JNE          Op0, Op1, Label

        iInstrIndex = AddInstrLookup ( "JNE", INSTR_JNE, 3 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 2, OP_FLAG_TYPE_LINE_LABEL );

        // JG           Op0, Op1, Label

        iInstrIndex = AddInstrLookup ( "JG", INSTR_JG, 3 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 2, OP_FLAG_TYPE_LINE_LABEL );

        // JL           Op0, Op1, Label

        iInstrIndex = AddInstrLookup ( "JL", INSTR_JL, 3 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 2, OP_FLAG_TYPE_LINE_LABEL );

        // JGE          Op0, Op1, Label

        iInstrIndex = AddInstrLookup ( "JGE", INSTR_JGE, 3 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 2, OP_FLAG_TYPE_LINE_LABEL );

        // JLE           Op0, Op1, Label

        iInstrIndex = AddInstrLookup ( "JLE", INSTR_JLE, 3 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 1, OP_FLAG_TYPE_INT |
                                    OP_FLAG_TYPE_FLOAT |
                                    OP_FLAG_TYPE_STRING |
                                    OP_FLAG_TYPE_MEM_REF |
                                    OP_FLAG_TYPE_REG );
        SetOpType ( iInstrIndex, 2, OP_FLAG_TYPE_LINE_LABEL );

        // ---- The Stack Interface

        // Push          Source

        iInstrIndex = AddInstrLookup ( "Push", INSTR_PUSH, 1 );
        SetOpType ( iInstrIndex, 0, OP_FLAG_TYPE_INT |

⌨️ 快捷键说明

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