<>

Computer Simulation in C Project



This is my Introduction to computing Course Final Project Check it out friends:

Click on the link given below and then wait for 5 sec then skip the ad.







Source Code:


#include <stdio.h>

#include <conio.h>

#include <windows.h>



#define SIZE 32
#define SENTINEL -99999
#define TRUE 1
#define FALSE 0
#define INP 10
#define PNT 11
#define STP 43
#define LDA 20
#define LDB 21
#define ADD 30
#define MUL 31
#define SUB 32
#define DIV 33
#define STR 40
#define MOD 34
#define CMP 35
#define JGT 50
#define JLT 51
#define JEQ 52
#define JMP 53

void load(int *loadram);
void execute(int *instruction,int *ram,int *registera,int *registerb, int *statusregister,int *counter);
void ramdisp(int *ram,int,int,int,int);
int validWord(long int word );
void menu2();
int help();
void menu();
void start();
void loading();
void logo();
void login();
void repeat();
void sample();
void about();
void samplemenu();
void sample1();
void sample2();
void sample3();
void sample4();
void sample5();

int main()
{
int ram[SIZE]={0};
int instruction[SIZE]={0};
int registera=0,registerb=0,statusregister=0,counter=1;
start();
menu2();
    load( instruction );
    execute(instruction,ram,&registera,&registerb,&statusregister,&counter);
    ramdisp(ram,registera,registerb,statusregister,counter);
    repeat();
    return 0;
}


void load( int *loadram )
{
    long int instruction;
    int i=0,y;
printf("\n\t\t   If you need sample program press 1:  ");
scanf("%d",&y);
if(y==1)
{
sample();
}
printf("Address: Contents:\n");
printf( "\n   00 ?\t  ");
    scanf( "%ld",&instruction );
while ( instruction != SENTINEL )
    {
        if ( !validWord( instruction ) )
            printf( "Number out of range. Please enter again.\n" );
        else
            loadram[i++] = instruction;
        printf( "   %02d ?\t  ",i);
        scanf( "%ld",&instruction );
   }
}

void execute( int *instruction,int *ram,int *registera,int *registerb,int *statusregister, int *counter)
{
int irPtr,opcode,oprend,icPtr=0,temp,fatal= FALSE;

    printf( "\n******************************  EXECUTION START  *******************************\n\n" );

    irPtr = instruction[ icPtr ];
    opcode = irPtr / 100;
    oprend = irPtr % 100;

    while (opcode!=STP)
    {
switch(opcode)
{
case INP:
                printf("Enter integer :");
                scanf("%d",&temp);
                while(!validWord(temp))
                {
                    printf("\nPlease Enter the value again : ");
                    scanf("%d",&temp);
                }
                ram[oprend]=temp;
                icPtr=icPtr+1;
                break;
case LDA: *registera=ram[oprend];
icPtr=icPtr+1;
                break;
case LDB: *registerb=ram[oprend];
icPtr=icPtr+1;
                break;
case ADD: *registerb=*registerb+*registera;
icPtr=icPtr+1;
                break;
case MUL: *registerb=*registerb*(*registera);
icPtr=icPtr+1;
                break;
case DIV: *registerb=*registerb/ *registera;
icPtr=icPtr+1;
                break;
case SUB: *registerb=*registerb-*registera;
icPtr=icPtr+1;
                break;
case STR: ram[oprend]=*registerb;
icPtr=icPtr+1;
                break;
case PNT: printf("\n%d",ram[oprend]);
icPtr=icPtr+1;
                break;
case MOD: *registerb=(*registera)%(*registera);
icPtr=icPtr+1;
                break;
case CMP: if (*registera > *registerb)
*statusregister=1;
else if(*registera < *registerb)
*statusregister=-1;
else
*statusregister=0;
icPtr=icPtr+1;
                break;
case JGT: if (*statusregister==1)
icPtr=oprend;
                break;
case JLT: if(*statusregister==-1)
icPtr=oprend;
                break;
case JEQ: if(*statusregister==0)
icPtr=oprend;
                break;
case JMP: icPtr=oprend;
                break;
default: printf("\nYou Have Entered a wrong Task.");
icPtr=icPtr+1;
                break;
}
*counter=icPtr+1;
irPtr = instruction[ icPtr ];
opcode = irPtr/ 100;
oprend = irPtr % 100;
    }
}

void ramdisp(int *ram,int registera,int registerb,int statusregister, int counter)
{
   printf("\nREGISTERS:\n\nRegister A: %d \nRegister B: %d \nStatus Register : %d \nCounter Register : %d", registera,registerb,statusregister,counter);
   printf("\n\nRAM:\n\n");

   for (int i=0;i<=7;i++)
      printf("%5d ",i);
   for (int j=0;j<SIZE;j++)
   {
      if (j%8==0)
         printf("\n%2d ",j);
      printf("%+05d ",ram[j]);
   }
   printf( "\n" );
}

int validWord( long int word )
{
   return (word >= -9999 && word <= 9999);
}

void menu2()
{
    system("COLOR 1F");
    int x,choice=0;
    x:
    system("cls");
    printf("\n\n\n\n\n\n\n\n\n\t\t\t");
    printf("Press 1 for Program Startup\n\t\t\tPress 2 for Sample Program\n\t\t\tPress 3 for Help\n\t\t\tPress 4 for About\n\n\t\t\tEnter Choice: ");
    scanf("%d",&x);
    if(x==1)
        menu();
    else if(x==2)
        samplemenu();
    else if(x==3)
    {
        choice=help();
        if(choice)
        {
            menu2();
        }
    }
else if(x==4)
{
about();
}
    else
    {
        printf("\t\t\tInvalid Selection");
        Sleep(1000);
        goto x;
    }
}

int help()
{
    system("COLOR 1F");
char c;
menu();
sample();
printf("\nPress any key to go back to Menu: ");\
    c=getch();
    c=int(c);
    return c;
}

void menu()
{
    system("cls");
printf("\n***********************************  MENU  ************************************\n");
printf("*\t\t\t\t\t\t\t\t\t      *\n");
printf("*\t       INP = 10     PNT = 11     STP = 43    LDA = 20\t\t      *\n");
printf("*\t\t\t\t\t\t\t\t\t      *\n");
printf("*\t       LDB = 21     ADD = 30     MOD = 34    CMP = 35\t\t      *\n");
printf("*\t\t\t\t\t\t\t\t\t      *\n");
printf("*\t       MUL = 31     SUB = 32     DIV = 33    STR = 40\t\t      *\n");
printf("*\t\t\t\t\t\t\t\t\t      *\n");
printf("*\t       JGT = 50     JLT = 51     JEQ = 52    JMP = 53\t\t      *\n");
printf("*\t\t\t\t\t\t\t\t\t      *\n");
printf("*******************************************************************************\n\n");
}

void loading()
{
    system("COLOR 70");
    char z[32];
    system("cls");
    printf("\n\n\n\n\n\n\n\n\n\t\t\t   Please Wait while Loading");
    printf("\n\n\t\t\t");
    for(int p=0; p<30; p++)
    {
        if(p%3==0)
        printf("%c",7);
        z[p]='.';
        printf("%c",z[p]);
        Sleep(200);
    }
    Sleep(500);
}

void logo()
{
    system("COLOR 1F");
    char y[24]={'B','L','A','C','K','R','I','B','B','O','N','C','O','M','P','U','T','E','R','M','O','D','E','L'};
    system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t");
    for(int q=0;q<24;q++)
    {
        if(q%3==0)
        printf("%c",7);
        if(q==5 || q==11|| q== 19)
        printf(" ");
        printf("%c",y[q]);
        Sleep(100);
    }
}

void login()
{
    system("COLOR 70");
    char a[6],x[5];
    f: e:
system("cls");
    printf("\n\n\n\tLOGIN REQUIRED:");
    printf("\n\n\n\t\tUsername: haseeb\tPassword: 12345");
    printf("\n\n\n\n\t\t\tEnter Username: ");
    gets(a);

        if(a[0]=='H' || a[0]=='h')
        {
            if((a[1]=='A' || a[1]=='a') && (a[2]=='S' || a[2]=='s' )&& (a[3]=='E' || a[3]=='e' )&& (a[4]=='E' || a[4]=='e' )&& (a[5]=='B'|| a[5]=='b'))
            {
                c:
                printf("\n\t\t\tEnter Password: ");
                for(int j=0; j<5; j++)
                {
                    x[j]=getch();
                    printf("*");
                }
                if(x[0]=='1' && x[1]=='2' && x[2]=='3' && x[3]=='4' && x[4]=='5')
                    printf("\n\n\t\t\t!! Password Matched !!");
                else
                {
                    printf("\n\t\t\tInvalid Password enter");
                    goto c;
                }
            }
            else
            {
                printf("\n\t\t\tInvalid Username enter");
                goto e;
            }
        }
        else
        {
            if(a[0]=='S' || a[0]=='s' && a[1]=='T' || a[1]== 't' && a[2]=='R' || a[2]=='r' && a[3]=='O' || a[3]=='o' && a[4]=='A' || a[4]=='a' && a[5]=='M' || a[5]=='m')
            {
                d:
                printf("\n\t\t\tEnter Password: ");
                for(int j=0; j<5; j++)
                {
                    x[j]=getch();
                    printf("*");
                }
                if(x[0]=='7' && x[1]=='2' && x[2]=='6' && x[3]=='7' && x[4]=='2')
                    printf("\n\n\t\t\t!! Password Matched !!");
                else
                {
                    printf("\n\t\t\tInvalid Password enter");
                    goto d;
                }
            }
            else
            {
                printf("\n\t\t\tInvalid Username enter");
                Sleep(2000);
                system("cls");
                goto f;
            }
        }
}

void start()
{
    loading();
logo();
    Sleep(2000);
    login();
Sleep(2000);
system("cls");
}

void repeat()
{
    char x;
    printf("\n\nDo you want to continue press Y otherwisr N: ");
    x=getche();
    if(x=='y'||x=='Y')
        main();
}

void sample()
{
printf("\n\t\t\t\tSAMPLE PROGRAM");
printf("\n\n*******************************************************************************\n\n");
printf("Address: Contents:\n");
    printf("  00 ? \t 1020\t It will take input at 20 location.\n");
    printf("  01 ? \t 1021\t It will also take input at 21 location.\n");
    printf("  02 ? \t 2020\t It will load 20 location value to Register A\n");
    printf("  03 ? \t 2121\t It will load 21 location value to Register B\n");
    printf("  04 ? \t 3000\t It will add the both values of registers\n");
    printf("  05 ? \t 4023\t It will store the add result to 23 location\n");
    printf("  06 ? \t 4300\t It will stop the execution\n");
    printf("  07 ? \t -99999\t It is the sentinel value\n");
printf("\n\t\t  \t\t Example : 1020");
printf("\n\t\t 10 is Instruction and 20 is the Ram Location  \t\t\n");
printf("\n*******************************************************************************\n\n");
}

void about()
{
    system("COLOR 1F");
    system("cls");
    printf("\n\tThis program takes assembly language instruction as input and\n");
    Sleep(500);
    printf("\n\tbreaks it opcode and operand, opcode is the instruction given\n");
    Sleep(500);
    printf("\n\tby the user to the computer to do some various tasks ,and the\n");
    Sleep(500);
    printf("\n\toperand are the instruction given to the ram to tell the ram\n");
    Sleep(500);
    printf("\n\tthat you have to apply this opcode instruction to the data.This\n");
    Sleep(500);
    printf("\n\tprogram is written to understand the machine language codes and\n");
    Sleep(500);
    printf("\n\tit make us easy to understand what was the problem for the user\n");
    Sleep(500);
    printf("\n\tand the programmers. Some years before the problem face by the\n");
    Sleep(500);
    printf("\n\tprogrammer are the programmer makes codes in the assembly lang.\n");
    Sleep(500);
    printf("\n\tif one of the instruction gets wrong all of the program gives\n");
    Sleep(500);
    printf("\n\twrong output and stops its execution and then the programmer have\n");
    Sleep(500);
    printf("\n\tto recheck all the program for the valid output.\n\n");
    Sleep(500);
    printf("\n\n\n\tProgram designed and coded by:\n");
    Sleep(1000);
    printf("\n\t\t\t\tHASEEB MUGHAL & MUHAMMAD ALI AKBAR\n");
    Sleep(500);
    printf("\n\tIn the supervision of:");
    Sleep(1000);
    printf("\n\t\t\t\tProf. NABEEL SABIR & Sir. BILAL HASSAN.\n");
    Sleep(1000);
    printf("\n\n\n\n\t\t\tUNIVERSITY OF CENTRAL PUNJAB (F12)");
    printf("\n\n\n\nPress any key to go back to Menu.");
    getch();
    menu2();
}

void samplemenu()
{
    int x=0;
    system("cls");
    printf("\n\n\n\n\n\n\n\n\n\t\t\tPress 1 for Sample Program 1");
    printf("\n\t\t\tPress 2 for Sample Program 2");
    printf("\n\t\t\tPress 3 for Sample Program 3");
    printf("\n\t\t\tPress 4 for Sample Program 4");
    printf("\n\t\t\tPress 5 for Sample Program 5");
    printf("\n\n\t\t\tEnter Choice: ");
    scanf("%d",&x);
    if(x==1)
        sample1();
    else if(x==2)
        sample2();
    else if(x==3)
        sample3();
    else if(x==4)
        sample4();
    else if(x==5)
        sample5();
    else
    {
        printf("\n\t\t\tInvalid Selection");
        Sleep(1000);
        samplemenu();
    }
}

void sample1()
{
    system("cls");
    printf("\n\n\n\t\t\t\tSAMPLE PROGRAM 1");
printf("\n\n*******************************************************************************\n\n");
printf("Address: Contents:\n\n");
    printf("  00 ? \t 1020\t It will take input at 20 location.\n\n");
    printf("  01 ? \t 1021\t It will also take input at 21 location.\n\n");
    printf("  02 ? \t 2020\t It will load 20 location value to Register A\n\n");
    printf("  03 ? \t 2121\t It will load 21 location value to Register B\n\n");
    printf("  04 ? \t 3000\t It will add the both values of registers\n\n");
    printf("  05 ? \t 4023\t It will store the add result to 23 location\n\n");
    printf("  06 ? \t 4300\t It will stop the execution\n\n");
    printf("  07 ? \t -99999\t It is the sentinel value\n\n");
printf("\n*******************************************************************************\n\n");
printf("\n\n\nEnter any key to go to Menu.");
getch();
menu2();
}

void sample2()
{
    system("cls");
    printf("\n\n\n\t\t\t\tSAMPLE PROGRAM 2\n");
printf("\n\n*******************************************************************************\n\n");
printf("Address: Contents:\n\n");
    printf("  00 ? \t 1015\t It will take input at 15 location.\n\n");
    printf("  01 ? \t 1016\t It will also take input at 16 location.\n\n");
    printf("  02 ? \t 2015\t It will load 15 location value to Register A\n\n");
    printf("  03 ? \t 2116\t It will load 16 location value to Register B\n\n");
    printf("  04 ? \t 3200\t It will Subtract the both values of registers\n\n");
    printf("  05 ? \t 4017\t It will store the add result to 17 location\n\n");
    printf("  06 ? \t 4300\t It will stop the execution\n\n");
    printf("  07 ? \t -99999\t It is the sentinel value\n\n");
printf("\n*******************************************************************************\n\n");
printf("\n\n\nEnter any key to go to Menu.");
getch();
menu2();
}

void sample3()
{
    system("cls");
    printf("\n\n\n\t\t\t\tSAMPLE PROGRAM 3");
printf("\n\n*******************************************************************************\n\n");
printf("Address: Contents:\n\n");
    printf("  00 ? \t 1007\t It will take input at 7 location.\n\n");
    printf("  01 ? \t 2007\t It will load 7 location value to Register A.\n\n");
    printf("  02 ? \t 2107\t It will load 7 location value to Register B.\n\n");
    printf("  03 ? \t 3100\t It will Multiply the both values of registers.\n\n");
    printf("  04 ? \t 4008\t It will store the Multiply result to the 8 location.\n\n");
    printf("  05 ? \t 1108\t It will Output the result from the 8 location.\n\n");
    printf("  06 ? \t 4300\t It will stop the execution\n\n");
    printf("  07 ? \t -99999\t It is the sentinel value\n\n");
printf("\n*******************************************************************************\n\n");
printf("\n\n\nEnter any key to go to Menu.");
getch();
menu2();
}

void sample4()
{
    system("cls");
    printf("\n\n\n\t\t\t\tSAMPLE PROGRAM 4");
printf("\n\n*******************************************************************************\n\n");
printf("Address: Contents:\n\n");
    printf("  00 ? \t 1014\t It will take input at 14 location.\n\n");
    printf("  01 ? \t 2014\t It will load 14 location value to register A.\n\n");
    printf("  02 ? \t 2113\t It will load 13 location value to register B.\n\n");
    printf("  03 ? \t 3400\t It will take Mod of register A with register B.\n\n");
    printf("  04 ? \t 2011\t It will load 11 location value to register A.\n\n");
    printf("  05 ? \t 3500\t It will compare the register A value with register B.\n\n");
    printf("  06 ? \t 5209\t If condition code 0 (values equal), jump to instruction 09.\n\n");
    printf("  07 ? \t 1111\t It will output the value from 11 location.\n\n");
    printf("  08 ? \t 5310\t It will jump to instruction 10.\n\n");
    printf("  09 ? \t 1112\t It will output the value from 12 location.\n\n");
    printf("  10 ? \t 4300\t It will stop the execution.\n\n");
    printf("  11 ? \t -99999\t It is the sentinel value\n\n");
printf("\n*******************************************************************************\n\n");
printf("\n\n\nEnter any key to go to Menu.");
getch();
menu2();
}

void sample5()
{
    system("cls");
    printf("\n\n\n\t\t\t\tSAMPLE PROGRAM 5");
printf("\n\n*******************************************************************************\n\n");
printf("Address: Contents:\n\n");
    printf("  00 ? \t 2113\t It will load the value from 13 location to register B.\n\n");
    printf("  01 ? \t 4015\t It will store the value of register B to 15 location.\n\n");
    printf("  02 ? \t 1014\t It will take the input at 14 location.\n\n");
    printf("  03 ? \t 2014\t It will load the value from 14 location to the register A.\n\n");
    printf("  04 ? \t 2113\t It will load the value from 13 location to the register B.\n\n");
    printf("  05 ? \t 3500\t It will compare the register A value with register B.\n\n");
    printf("  06 ? \t 5111\t It will jump from loop if the value of register A < register B.\n");
    printf("  07 ? \t 2115\t It will load the value from 15 location to the register B.\n\n");
    printf("  08 ? \t 3000\t It will add the values of both of the registers.\n\n");
printf("  09 ? \t 4015\t It will store the value to the 15 location.\n\n");
printf("  10 ? \t 5302\t It will jump back to the loop for the next input.\n\n");
printf("  11 ? \t 1115\t It will print the value of 15 location.\n\n");
printf("  12 ? \t 4300\t It will stop the execution.\n\n");
    printf("  13 ? \t -99999\t It is the sentinel value\n\n");
printf("\n*******************************************************************************\n\n");
printf("\n\n\nEnter any key to go to Menu.");
getch();
menu2();
}
Rate this posting:
{[['']]}

0 comments:

Post a Comment

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

 
TOP