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 SML file to this program to execute as command line argument. As shown in the screenshot in output section.

#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:

Comments

Post a Comment

Popular posts from this blog

Change your Application background in Scene Builder || JavaFX FXML Tutorial

5 Programming Languages To Learn in 2019

c/c++ Programming questions || Practice Problems for Beginners || C Programs List