Main loop of the Finite State Machine

This is a discussion on Main loop of the Finite State Machine within the C Programming forums, part of the General Programming Boards category; hey folks, i got an issue wtih this code I coded main loop of the finite state machine,I know the ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Main loop of the Finite State Machine

    hey folks, i got an issue wtih this code

    I coded main loop of the finite state machine,I know the AND INSTRUCTION part but i could not figure out ADD INSTRUCTİON PART.

    I hope you will help me out. Here is the code:

    Code:
    // Main loop of the Finite State Machine
    while (1)
    {
    // Fetch
    instruction = memory[pc];
    ++pc;
    
    // Decode
    opcode = (instruction & 0xF000) >> 12;
    printf("Opcode is %x\n", opcode);
    
    switch (opcode)
    {
    case 0x0:
    printf("Branch instruction.\n");
    break;
    
    case 0x1:
    printf("ADD instruction\n");
    // HERE. I COULD NOT FIGURE OUT 
    break;
    
    case 0x5:
    printf("AND instruction\n");
    if (instruction & (1 << 5))
    {
    // Immediate mode
    dr = (instruction >> 8) & 0x7;
    sr1 = (instruction >> 5) & 0x7;
    store(dr, registers[sr1] & sign_extend(5, instruction & 0x1F));
    }
    else
    {
    // Immediate mode
    dr = (instruction >> 8) & 0x7;
    sr1 = (instruction >> 5) & 0x7;
    sr2 = (instruction >> 0) & 0x7;
    store(dr, registers[sr1] & registers[sr2]);
    }
    break;
    
    case 0x9:
    printf("NOT instruction\n");
    break;
    
    
    case 0xF:
    /* Right now, quit any time we see a trap. */
    exit(0);
    }
    }
    
    }
    Last edited by Salem; 11-18-2010 at 10:51 PM. Reason: restored original edit

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    I'm assuming then, that the ADD instruction is supposed to add the values in 2 registers (or a register and immediate value) and store them somewhere. The code should look exactly like your AND code, except that you're using the addition operator '+' instead of the and operator, '&'.

    Can you describe exactly what's wrong with ADD mode? What is the type of registers? Perhaps this is a signed vs unsigned issue or an overflow issue.

    Code:
    	  else
    	    {
    	      // Immediate mode
    	      dr = (instruction >> 8) & 0x7;
    	      sr1 = (instruction >> 5) & 0x7;
    	      sr2 = (instruction >> 0) & 0x7;
    	      store(dr, registers[sr1] & registers[sr2]);
    	    }
    I think the else clause is "direct mode" (using 2 register), not "immediate mode" (using a registers and literal/immediate value).

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    nhnty
    Nice...
    Last edited by User Name:; 11-18-2010 at 09:21 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    @ makonikor
    Please don't edit your posts down to nothing (or garbage) as soon as you've got your answer.
    No one else can learn from the discussion if you do that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. State Machine Controller
    By scott_ill in forum C# Programming
    Replies: 0
    Last Post: 06-25-2009, 02:57 AM
  2. Break from loop to main();
    By Djanvk in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2008, 03:20 AM
  3. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  4. input/switch statement issues
    By peanut in forum C Programming
    Replies: 5
    Last Post: 10-27-2006, 02:58 PM
  5. Function pointer question
    By sbayeta in forum C Programming
    Replies: 9
    Last Post: 08-06-2004, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21