Thread: Machine simulator

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Machine simulator

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*Input/Output operations*/
    #define READ 10
    #define WRITE 11
    
    /* Load/store operations */
    #define LOAD 20
    #define STORE 21
    
    /*Arthmetic operations */
    #define ADD 30
    #define SUBTRACT 31
    #define DIVIDE 32
    #define MULTIPLY 33
    
    /* Transfer of control operations */
    #define BRANCH 40
    #define BRANCHNEG 41
    #define BRANCHZERO 42
    #define HALT 43
    
    /* My own need Macros */
    #define MEMSIZE 100
    
    /*-----------------11/5/98 0:50:M-------------------
     * Functions it will help me keep the programm clean
     * and easy to understand.Function are very important.
     * --------------------------------------------------*/
    
    void memorydump(int [], int , int, int , int, int );
    void simulate(int [], int* , int *, int*, int*, int );
    
    int main (void)
    {
    	int memory[MEMSIZE]={+0000};
    	int accumulator, instructionCounter, instructionRegister,
    		operationCode, operand;
    	int cnt=0;
    	/*-----------------11/5/98 1:21:M-------------------
    	 * Set the varibles to initial value...
    	 * --------------------------------------------------*/
    	accumulator = +0000;
    	instructionRegister = +0000;
    	operationCode = 00;
    	operand = 00;
    
    
    	printf( "\t\t*** Welcome to Simpletron! ***\n"
    		    "\t\t*** Please enter your program one instruction ***\n"
    		    "\t\t*** (or data word) at a time. I will type the ***\n"
    			"\t\t*** location number and a question mark (?). ***\n"
    			"\t\t*** You then type the word for that location. ***\n"
    			"\t\t*** Type the sentinel -99999 to stop entering. ***\n"
    			"\t\t*** your program. Enjoy %c ***\n\n\n", 2 );
    
    	printf( "0%d ? ",cnt );
    	scanf( "%d", &memory[cnt] );
    	while ( memory[cnt] != -99999 )
    	{
    		if ( memory[ cnt ] < -9999 || memory[ cnt ] > 9999 ){
    			printf("Between -9999 && +9999\n");
    			--cnt;
    		}
    
    		++cnt;
    		printf( "%c%d ? ", (cnt < 10) ? '0' : '\b' , cnt );
    		scanf( "%d", &memory[cnt] );
    
    
    	}
        memory[cnt] = +0000;
    
    
    	printf( "\n*** Program loading completed ***\n"
    			"*** Program execution begins ***\n" );
    
    	for ( instructionCounter = 0; instructionCounter <= cnt-1; instructionCounter++  )
    	{
    		instructionRegister = memory[ instructionCounter ];
    
    		operationCode = instructionRegister / 100;
    		operand = instructionRegister % 100;
    
    		simulate(memory, &accumulator, &instructionCounter,
    	 		     &operationCode, &operand, cnt);
    
    	}
    	printf("\n\n");
    	system( "pause" );
    	system( "cls" );
    
    	memorydump( memory, accumulator, instructionCounter, instructionRegister,
    				operationCode, operand );
    
    	system( "pause" );
    	return 0;
    }
    
    void simulate ( int memory[MEMSIZE], int *accumulator, int *instructionCounter,
    				int *operationCode, int *operand, int cnt_fun )
    {
    
    	switch(*operationCode){
    
    		case READ :
    			printf("? ");
    			scanf( "%d", &memory[ *operand ] );
    			break;
    		case WRITE :
    			printf("? %d", memory[ *operand ] );
    			break;
    
    		case LOAD :
    			*accumulator = memory[ *operand ];
    			break;
    		case STORE :
    			memory[ *operand ] = *accumulator;
    			break;
    
    	   case ADD :
    			if ( *accumulator > 9999 ){
    				printf(" *** Accumulator overflow ( + Positive )*** \n"
    					   " *** Simpletron execution abnormaly terminated ***\n" );
    				system( "pause" );
    				exit(EXIT_FAILURE);
    			}
    			*accumulator += memory[ *operand ];
    			break;
    	   case SUBTRACT :
    			if ( *accumulator < -9999 ){
    				printf( "*** Accumulator overflow ( - Negative )*** \n"
    						"*** Simpletron execution abnormaly terminated ***\n" );
    				system( "pause" );
    				exit(EXIT_FAILURE);
    			}
    			*accumulator -= memory[ *operand ];
    			break;
    	   case DIVIDE :
    			if ( *accumulator == 0 ){               /* Checking for divide by zero */
    				printf( "*** Attempt to divide by zero ***\n"
    						"*** Simpletron execution abnormaly terminated ***\n" );
    				system( "pause" );
    				exit(EXIT_FAILURE);
    			}
    			*accumulator /= memory[ *operand ];
    			break;
    	   case MULTIPLY :
    			if ( *accumulator < -9999 || *accumulator > 9999 ){
    				printf(" *** Accumulator overflow due to Multiplication( + Positive / - Negative )*** \n"
    					   " *** Simpletron execution abnormaly terminated ***\n" );
    				system( "pause" );
    				exit(EXIT_FAILURE);
    			}
    
    			*accumulator *= memory[ *operand ];
    			break;
    
    	  case BRANCH :
    			*instructionCounter = *operand;
    			break;
    	  case BRANCHNEG :
    		if ( *accumulator < 0 )
    			*instructionCounter = *operand;
    			break;
    	  case BRANCHZERO :
    		if ( *accumulator == 0 )
    			*instructionCounter = *operand;
    			break;
    	  case HALT :
    			*instructionCounter =  cnt_fun;
    			printf( "\n*** Simpletron execution terminated ***\n" );
    			break;
    	  default :
    			printf( "\n*** Invalid Operation Code ***\n"
    					" *** Simpletron execution abnormally terminated ***\n");
    			system( "pause" );
    			exit(EXIT_FAILURE);
    	}
    }
    
    void memorydump( int memory[MEMSIZE], int accumulator, int instructionCounter,
    				 int instructionRegister, int operationCode, int operand )
    {
    	int cnt_main, cnt_sub, cnt, i;
        i = 0;
    
    	printf( "\nREGISTERS:\n"
    		  "accumulator %+20.4d\n"
    		  "instructionCounter %13.2d\n"
    		  "instructionRegister %+12.4d\n"
    		  "operationCode %18.2d\n"
    		  "operand %24.2d\n", accumulator, instructionCounter, instructionRegister,
    		  operationCode, operand );
    
       printf("\nMEMORY:\n");
    
       printf("%9d" ,0);
       for ( cnt_main = 1; cnt_main <= 9; cnt_main++ )
    		printf( "%7d", cnt_main );
                     printf("\n");
    
       for ( cnt_main = 0, cnt = 0; cnt_main <= 9; cnt_main++ ){
           printf( "%2d", cnt );
           cnt += 10;
       for (cnt_sub =0; cnt_sub <= 9; cnt_sub++ )
             printf("%+7.4d", memory[i++] );
    
                            printf("\n");
       }
    
    
    
    }
    this simulator executed your codes written in SML ..which is simple machine langauge ...

    For a try use these codes :-

    00 ? 1007
    01 ? 1008
    02 ? 2007
    03 ? 3008
    04 ? 2109
    05 ? 1109
    06 ? 4300
    07 ? 0000
    08 ? 0000
    09 ? 0000
    10 ? -99999 (to end the data entry).

    The above code add 2 numbers and prints te sum out..

    Here is another code that finds the largest number :-

    00 ? 1009
    01 ? 1010
    02 ? 2009
    03 ? 3110
    04 ? 4107
    05 ? 1109
    06 ? 4300
    07 ? 1110
    08 ? 4300
    09 ? 0000
    10 ? 0000

    ----------------------------------------------------------------------------------

    I wanted to make some modifications to it ..

    1) Modify the simulator to use hexadecimal values rather than integer values to represent Simpletrom machine language ?? Okay would i have to use a %x ..is that what they are saying ...

    2) Modify simulator to allow output of a newline ...

    3) Modify the simulator to process floating point values in addition to integer ...

    Tnaks alot
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    thanks alot salem
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finite State Machine
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 11:59 AM
  2. Replies: 4
    Last Post: 01-18-2008, 07:05 PM
  3. destroywindow() problem
    By algi in forum Windows Programming
    Replies: 6
    Last Post: 03-27-2005, 11:40 PM
  4. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  5. Creating a Virtual Machine
    By Chronom1 in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2003, 11:11 AM