Thread: Design Issue, Your Thoughts Wanted

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    Design Issue, Your Thoughts Wanted

    I'm creating a virtual computer that can process its own pseudo-assembly instruction set.
    Basically I have a structure like this:
    Code:
    struct cpu
    {  /* structure for registers of virtual cpu */
        int   ax;  /* address register */
        int   bx;  /* address register */
        int   cx;  /* numerical register */
        int   dx;  /* numerical register */
        char  fl;  /* flag */
        char  sp;  /* stack pointer */
        int   st[10];  /* stack */
        int   ip;  /* instruction pointer */
    };
    Then a group of instructions:
    Code:
    struct command
    {
        int opcode, operand1, operand2;
    };
    std::vector<command> algo; //Not actually here, but you get the idea
    The actual opcodes are defined in a switch case like:
    Code:
    switch(hotcommand.opcode)
    {
         case MOV:
              //Code to for MOV
    The problem is here with the code for MOV. For this to work like I had imagined the operands for an address like ax would need to be pointers to the actual register, or something.

    If this isn't clear, then please ask, and I'll try to clarify.

    Thanks,
    David

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Make sure you ask an actual question.

    It doesn't quite sound like a design issue, but more like a "how can I do this?" problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do they need to point to registers? Since you're emulating the machine, the registers might as well be stored in memory, which is what you have now.
    Btw, the stack is pretty small too. It should at least be around a MB.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Sorry it isn't very clear. Say you have the command:
    mov ax,cx

    Well I have a switch statement that switches depending on the command, but in the instruction, "ax" and "cx" are just, mmm references? So would I have to say if "cx" then we are actually talken about CPU.cx. Is that a necessary step, or is there a better way I could do this?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Obviously, part of your decoding of your instruction will involve figuring out what the arguments for this particular instruction is, in your example ax and cx.

    One way to achieve that would be to, when you decode the instruction, you create a pointer to the source and destination register, e.g.:
    Code:
        switch(srcRegNo)
        {
           case RegNoAX:
              src = &CPU.ax;
              break;
         .... 
        }
        switch(dstRegNo)
        {
           ... Same as above
        }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    OK, I see. Thanks for the reassurance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Software Design issue
    By zacs7 in forum Tech Board
    Replies: 1
    Last Post: 04-22-2009, 05:34 AM
  2. Design of Large C Projects/Program
    By amrishpurohit in forum Tech Board
    Replies: 5
    Last Post: 09-03-2008, 01:29 AM
  3. design problem
    By s-men in forum C++ Programming
    Replies: 5
    Last Post: 06-09-2008, 05:59 AM
  4. which design is better to wrap another class instance
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2008, 12:27 AM
  5. Need Help with 2D Garden Design Program
    By frgmstr in forum C++ Programming
    Replies: 7
    Last Post: 02-04-2002, 04:58 PM