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 ...