Thread: My VM project - mysterious instruction

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    My VM project - mysterious instruction

    I've started writing a virtual machine as a pet project (pet project #3 actually) and, without boring you with the details, I'll just say that the instructions are stored in a vector of CInstruction. CInstruction is a very basic class:

    Code:
    class CInstruction {
    public:
        byte opcode;
        byte operands[2];
    };
    In the virtual machine class the Run method iterates through this vector and "does the right thing" based on the instructions opcode. However at the end of each run, I have the instruction 253, with the operands 253, 253, appear out of nowhere. I assume it's a random value in the vector but I'm not sure how it got there.

    Here's the run method:

    Code:
    int CVirtualMachine::Run()
    {
        CInstruction inst = nextInstruction();
    
        while (inst.opcode != 0x05) {
    #ifdef DEBUG
            std::cout << "\nDEBUG: ip : " << (unsigned) inst.opcode << " " << (unsigned) inst.operands[0] <<
                         ", " << (unsigned) inst.operands[1] << endl;
    #endif
            switch (inst.opcode) {
                case 0x00:  // nop
                    break;
                case 0x05:  // end
                    endProgram(inst.operands);
                    break;
                case 0x06:  // movl l, r
                    movl(inst.operands);
                    break;
    
                default:
                    throw BadInstruction();
            }
    
            inst = nextInstruction();
        }
    
        return 0;
    }
    The vector is simply defined in the class as

    Code:
    vector<CInstruction> instructions;
    Then it isn't touched until my main driver program calls LoadDebug:

    Code:
    bool CVirtualMachine::LoadDebug(vector<CInstruction> program)
    {
        for (unsigned i = 0; i < program.size(); i++)
            instructions.push_back(program[i]);
    
        return true;
    }
    LoadDebug is just there so I can feed it "raw instructions" until I start to write an assembler.

    Oh, here's nextInstruction:

    Code:
    CInstruction CVirtualMachine::nextInstruction()
    {
        return instructions[ip++];
    }
    I think that's all that could affect the instructions vector.
    Help!

    Oh yeah in case you were wondering it's all coming along nicely otherwise. Here's a sample output for your pleasure:

    Code:
    DEBUG: ip : 6 10, 1
    
    DEBUG: Registers:
            r0: 10
            r1: 0
            r2: 0
            r3: 0
    
    DEBUG: ip : 6 255, 3
    
    DEBUG: Registers:
            r0: 10
            r1: 0
            r2: 255
            r3: 0
    
    DEBUG: ip : 253 253, 253
    *** ERROR ***
            Bad Instruction!
    Press any key to continue
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Whoop just solved it. I was never giving it the end instruction (0x05) so I guess the while loop went wild.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Have you considered and enum for all your opcodes?

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Yep I've just spent the last few hours working on it and I started using enums when I got to about the 10th instruction.

    It's in a state now where some simple programs can be written but it's a hassle to feed it CInstructions manually so I've just started working on an assembler. This should be fun
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 10:31 PM
  2. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM