Simpletron Complete Program in C [Solved]

The Simpletron runs programs written in the only language it directly understands that is, Simpletron Machine Language, or SML for short. All information in the Simpletron is handled in terms of words. A word is a signed four-digit decimal number such as +1009 +1010 +2009 +3110 and so on. The Simpletron is equipped with a 100- word memory and these words are referenced by their location numbers 00, 01, …, 99. Before running an SML program, we must load or place the program into memory or the array of the program considered it as the RAM of the computer. The first instruction of every SML program is always placed in location 00. 

Each instruction written in SML occupies one word of the Simpletron’s memory, so instructions are four-digit decimal numbers. Each location in the Simpletron’s memory may contain either an instruction, a data value used by a program or an unused (and hence undefined) area of memory.

For more details, you can read the section 7.27 Building your own computer of Deitel-C how to Program 7th edition.

We have to provide an SML file to this program to execute as a command-line argument. As shown in the screenshot in the output section.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<stdio.h>

#include<stdlib.h>

void displayMemory(int memory[],int size){

    int i;

    printf("\n\n****SML file Loaded in Memory****\n\n");

    for(i=0;i<size;i++){

        printf(" %+05d ",memory[i]);

        if((i+1)%10==0){

            printf("\n");

        }

    }

    printf("\n");

}

int main(int argCount, char *argArr[]){

    int memory[100]={0},i=0,opcode=0,address=0,accumulator=0,counter=0;

    FILE *file;


    if(argCount==2){

        if((file=fopen(argArr[1],"r"))==NULL){

            printf("\nProvided SML File is Not Opening\n");

        }else{

            while(!feof(file)){

                fscanf(file,"%d",&memory[i]);

                i++;

            }

            fclose(file);

        }

        i=0;

    }else{

        printf("\nNo SML file provided to RUN\n");

        exit(0);

    }

    displayMemory(memory,100);

    while(memory[i]!=4300){

        opcode = memory[i]/100;

        address = memory[i]%100;

        counter++;

        printf("\nAccumulator : %d",accumulator);

        printf("\nOpcode      : %d",opcode);

        printf("\nOperand     : %d",address);

        printf("\nCounter     : %d\n",counter);

        switch(opcode){

            case 10 :

                printf("Opcode is 10, So Enter a number : ");

                scanf("%d",&memory[address]);

            break;

            case 11 : printf("%d",memory[address]);

            break;

            case 20 : accumulator = memory[address];

            break;

            case 21 : memory[address] = accumulator;

            break;

            case 30 : accumulator += memory[address];

            break;

            case 31 : accumulator -= memory[address];

            break;

            case 32 : accumulator /= memory[address];

            break;

            case 33 : accumulator *= memory[address];

            break;

            case 40 : i = address;

                        continue;

            break;

            case 41 :

                if(accumulator<0){

                        i = address;

                        continue;

                }

            break;

            case 42 :

                if(accumulator==0){

                        i = address;

                        continue;

                }

            break;

        }

        i++;

    }


    return 0;

}

Output:

1 comment: