Thread: Machine language simulation

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Machine language simulation

    Hi,

    This is an exercise from the deitel book. First i got it to work. Then later it has a bug. There is something wrong with the looping. But i don't know how to explain, sorry. By looking at the code is there something wrong with it?

    thnx in advance.

    Code:
    /* Simpletron - Computer simulation */
    
    #include <stdio.h>
    
    int main()
    {
       int memory[ 100 ] = { 0 };
       int accumulator = 0;
       int operationCode;
       int operand;
       int instructionCounter = 0;
       int instructionRegister;
       int temp;
    
       printf( "***            Welcome to Simpletron!         ***\n" );
       printf( "*** Please enter your program one instruction ***\n" );
       printf( "*** (or data word) at a time. I will type the ***\n" );
       printf( "*** location number and a question mark (?).  ***\n" );
       printf( "*** You then type the word for that location. ***\n" );
       printf( "*** Type the sentinel -99999 to stop entering ***\n" );
       printf( "*** your program.                             ***\n\n" );
    
       while ( memory[ instructionCounter ]  != -99999 ) {
          printf( "%2d ? ", instructionCounter );
          scanf( "%d", &temp );
          if ( temp >= -9999 && temp <= 9999 )
             memory[ instructionCounter ] = temp;
          else if ( temp == -99999 ) {
             memory[ instructionCounter ] = temp;
             break;
          }
          instructionCounter++;
       }
       printf( "\n" );
    
       printf( "*** Program loading completed ***\n" );
       printf( "*** Progam execution begins ***\n\n" );
    
       instructionCounter = 0;
    
       while ( instructionRegister != -99999 ) {
          instructionRegister = memory[ instructionCounter ];
          operationCode = instructionRegister / 100;
          operand = instructionRegister % 100;
    
          printf( "\ninstructionRegister = %d, operationCode = %d, operand = %d\n", instructionRegister, operationCode, operand );
    
          switch ( operationCode ) {
             case 10:
                scanf( "%d\n", &memory[ operand ] );
                printf( "scaned for the first time\n" );
                break;
             case 11:
                printf( "%d\n", memory[ operand ] );
                break;
             case 20:
                accumulator = memory[ operand ];
                break;
             case 21:
                memory[ operand ] = accumulator;
                break;
             case 30:
                accumulator += memory[ operand ];
                break;
             case 31:
                accumulator -= memory[ operand ];
                break;
             case 32:
                accumulator /= memory[ operand ];
                break;
             case 33:
                accumulator *= memory [operand ];
                break;
             case 40:
                instructionCounter = operand;
                continue;
             case 41:
                if ( accumulator < 0 ) {
                   instructionCounter = operand;
                   continue;
                }
                break;
             case 42:
                if ( accumulator == 0 ) {
                   instructionCounter = operand;
                   continue;
                }
                break;
             case 43:
                printf( "\n\n*** Simpletron execution terminated ***\n\n" );
                instructionRegister = -99999;
                continue;
             default: /* invalid operation code encountered */
                printf( "\nSimpletron execution abnormally terminated ***\n" );
                instructionRegister = -99999;
                continue;
          }
          instructionCounter++;
       }
    
       printf( "\n\n*** END *** \n" );
    
       getch();
       return 0;
    A sample program which adds two numbers is:

    1007
    1008
    2007
    3008
    2109
    1109
    4300
    0000
    0000
    0000
    -99999

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    16
    hey, instead of having a continue after the branchnegative, and branchZero statements, put a break in it, and add increment the instructionCounter in an else statement. That way, if it gets to those statements, it will increment to the next instruction if it doesn't have to brach the trasnfer of controll. That should do it, let me know if it works
    always learning, always frustrated

    http://www.snakshak.net

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    16
    dude, are you in my class or something ???
    always learning, always frustrated

    http://www.snakshak.net

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    The only thing I noticed is this:
    Code:
    >         case 10:
    >            scanf( "%d\n", &memory[ operand ] );
    Leave off the newline here:
                scanf( "%d", &memory[ operand ] );
    >            printf( "scaned for the first time\n" );
    >            break;
    I ran it, and after that change, it seemed to work good. Using your data, it did the addition.

    The only other thing I noticed is instructionRegister isn't initialized the first time thru the loop. But of course what are the odds of it being -99999?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But of course what are the odds of it being -99999?
    And what are the odds of that one odd time being the most critical time the program is run? This program also fails to include conio.h when it uses getch at the end.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >And what are the odds of that one odd time being the most critical time the program is run?

    Using Murphy's Law, pretty good ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. d programming language - a better c++?
    By kypronite in forum Tech Board
    Replies: 12
    Last Post: 02-28-2011, 02:55 AM
  2. Role of c++ in simulation software
    By CChakra in forum C++ Programming
    Replies: 9
    Last Post: 11-18-2008, 02:36 AM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  5. Enough language discussions.
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-13-2004, 09:59 AM