Thread: Oh noes, backwards she goes!

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

    Oh noes, backwards she goes!

    Ok so while I'm debugging I'm just going to feed the VM a vector of CInstruction's instead of writing an assembler right now. This function takes one such vector and writes the elements into the memory allocated by AllocateBlock (I took major_small & everyone elses advice about the memory allocation BTW).

    Code:
    void CVM::LoadProgramVec(vector<CInstruction>& program)
    {
        vector<CInstruction>::iterator iter = program.begin();
        word* write = AllocateBlock(program.size());
    
        if (! write) throw VMPLoadFailNoMem();
    
        for (; iter != program.end(); iter++)
            ++*write = *iter->opcode;
    
        DebugBlock(write, program.size(), false);
    }
    DebugBlock just dumps the memory to the screen so I can, you know, see it. This all works fine except the opcodes are getting written in backwards. Here's my main which gives it two instructions:

    Code:
    // Eww, but buggered if I'm writing this out once per instruction while debugging
    #define INST(i, op1, op2) *inst.opcode = i; inst.operands[0] = op1; inst.operands[1] = op2; program.push_back(inst)
    
    void Error(const char* msg);
    
    int main(int argc, char* argv[])
    {
        CVM* vm = new CVM();
        vector<CInstruction> program;
        CInstruction inst;
    
        try {
            vm->Initialise();
        } catch (VMInitFail) {
            Error("vm->Initialise() threw VMInitFail");
            return -1;
        }
    
        INST(0, 0, 0);
        INST(1, 0, 2);
    
        try {
            vm->LoadProgramVec(program);
        } catch (VMPLoadFailNoMem) {
            Error("Program load failed - memory could not be allocated"); 
        }
    
        vm->Shutdown();
        delete vm;
        return 0;
    }
    Output:

    Code:
     1 0
    Press any key to continue
    Unless I'm misunderstanding something, shouldn't that be "0 1" ?
    Oh, not sure how relevant this is, but:

    Code:
    class CInstruction {
    public:
        CInstruction(void);
    
        word* opcode;
        word* operands;
    };
    opcode and operands are new'd in the constructor.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    try chaning
    ++*write = *iter->opcode;
    to
    *write++ = *iter->opcode;
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

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

    Output:

    Code:
     65021 65021
    Press any key to continue
    What the hell?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ok, I dont exactly know why that happened but I know that your
    ++*write = *iter->opcode;
    is very suspicious. Doing ++*write wont take you to the next location it points to, it will only increment whatever is stored in that memory by 1.

    What you probably wanted was this:
    *++write
    so you might want to try with that too, but then my *write++ should have worked too (at least to some degree since the only difference is the pre/post increment.).
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ok I've now got this:

    Code:
    void CVM::LoadProgramCode(vector<CInstruction>& program)
    {
        vector<CInstruction>::iterator iter = program.begin();
        word* write = AllocateBlock(program.size());
    
        if (! write) throw VMPLoadFailNoMem();
    
        for (; iter != program.end(); iter++)
            *++write = *iter->opcode;
    
        DebugBlock(write, program.size(), false);
    }
    I forgot that ++*write does what it does. However I'm now getting this output:

    Code:
     2 65021
    In main I have

    Code:
    INST(1, 0, 0);
    INST(2, 0, 0);
    So it still seems to be going in backwards, unless this is just outputting them bent:

    Code:
    void CVM::DebugBlock(word* b, size_t sz, bool fileDump)
    {
        if (! fileDump) {
            for (size_t i = 0; i < sz; i++)
                std::cout << " " << b[i];
    
            std::cout << std::endl;
        } else {
            FILE* f = fopen("DebugBlock.txt", "a");
    
            for (size_t i = 0; i < sz; i++)
                fputc(b[i], f);
    
            fclose(f);
        }
    }
    Last edited by cboard_member; 03-11-2006 at 09:29 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    ahhh now I remember this: I think it should be this:
    *write++ = *iter->opcode;

    but at the end of the loop you must restore write to point to the beginning of the memoryblock. See you modify where the pointer points to, and then in write you try to print out the 2 blocks after the ones you allocated.

    Try something like this:
    Code:
    word *temp = write;
    for (; iter != program.end(); iter++)
            *write++ = *iter->opcode;
    write = temp;
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Code:
    void CVM::LoadProgramCode(vector<CInstruction>& program)
    {
        vector<CInstruction>::iterator iter = program.begin();
        word* write = AllocateBlock(program.size());
        word* temp = write;
    
        if (! write) throw VMPLoadFailNoMem();
    
        for (; iter != program.end(); iter++)
            *write++ = *iter->opcode;
        write = temp;
    
        DebugBlock(write, program.size(), false);
    }
    Output:

    Code:
     2 2
    Didn't work, just going to fiddle with it more.
    Keep possible solutions coming if you got 'em.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    And why can't I watch expressions like

    Code:
    *iter->opcode
    OR
    (*iter).opcode
    With VS .NET 2003's debugger? I tried the (*iter).opcode in case it was being picky with ->.

    ?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You can add a watch, just rightclick on iter and select add watch (at least thats how I do it in VS.NET 2002).
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Backwards uint16_t?
    By DavidDobson in forum C Programming
    Replies: 7
    Last Post: 05-11-2009, 09:45 AM
  2. Backwards Up-Down
    By Cactus_Hugger in forum Windows Programming
    Replies: 1
    Last Post: 11-08-2003, 05:56 PM
  3. Taking input and Outputting it backwards
    By whtpirate in forum C Programming
    Replies: 9
    Last Post: 06-08-2003, 10:59 AM
  4. Reading in a number backwards!
    By paranormal in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2002, 04:09 AM
  5. how to print a string backwards
    By Unregistered in forum C# Programming
    Replies: 2
    Last Post: 01-27-2002, 01:04 PM