Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MaxProgSize 100
- #define MaxMemSize 50
- struct Instruction {int OpCode, Address;
- };
- struct Instruction IM[MaxProgSize];
- struct Instruction MDR1;
- struct Instruction IR;
- int DM[MaxMemSize];
- int PC = 0, MAR, MDR2, A;
- int j = 0;
- struct Instruction fetchExe();
- int load();
- int add();
- int store();
- int subt();
- int in();
- int out();
- void end();
- int jmp();
- int skipz();
- int main(int argc, char *argv[]){
- int c;
- int i;
- //Pointing towards the input file Final.txt
- FILE *Final;
- //Instead of using Final = fopen(argv[1], "r"); ,
- //I used this because the command line in DevC++ does not read my file "Final.txt" if it sets on argv[1]
- Final = fopen("Final.txt", "r");
- // if the file does not exist,
- if (Final == NULL)
- printf("Cannot open file!");
- // if it exists and does work.
- else {
- printf("Assembling Program...\n");
- int i = 0;
- while ((c = getc(Final)) != EOF){
- c-='0';
- if(c<0||c>9){
- continue;
- }
- if(i%2==0){
- IM[PC+i/2].OpCode=c;
- }else{
- IM[PC+i/2].Address=c;
- }
- i++;
- }
- printf("Program assembled\n Run.\n");
- printf("PC = %d \t A = %d \t MEM = [",PC,A);
- for(j=0;j<MaxMemSize-1;j++){
- printf("%d, ",DM[j]);
- }
- printf("%d]\n",DM[j]);
- while(MaxProgSize > A ){
- switch(IR.OpCode){
- case 0: fetchExe();
- break;
- case 1: load();{
- printf("PC = %d \t A = %d \t MEM = [",PC,A);
- for(j=0;j<MaxMemSize-1;j++){
- printf("%d, ",DM[j]);
- }
- printf("%d]\n",DM[j]);
- }
- break;
- case 2: add();{
- printf("PC = %d \t A = %d \t MEM = [",PC,A);
- for(j=0;j<MaxMemSize-1;j++){
- printf("%d, ",DM[j]);
- }
- printf("%d]\n",DM[j]);
- }
- break;
- case 3: store();{
- printf("PC = %d \t A = %d \t MEM = [",PC,A);
- for(j=0;j<MaxMemSize-1;j++){
- printf("%d, ",DM[j]);
- }
- printf("%d]\n",DM[j]);
- }
- break;
- case 4: subt();{
- printf("PC = %d \t A = %d \t MEM = [",PC,A);
- for(j=0;j<MaxMemSize-1;j++){
- printf("%d, ",DM[j]);
- }
- printf("%d]\n",DM[j]);
- }
- break;
- case 5: in();{
- printf("PC = %d \t A = %d \t MEM = [",PC,A);
- for(j=0;j<MaxMemSize-1;j++){
- printf("%d, ",DM[j]);
- }
- printf("%d]\n",DM[j]);
- }
- break;
- case 6: out();
- break;
- case 7: end();
- break;
- case 8: jmp();{
- printf("PC = %d \t A = %d \t MEM = [",PC,A);
- for(j=0;j<MaxMemSize-1;j++){
- printf("%d, ",DM[j]);
- }
- printf("%d]\n",DM[j]);
- }
- break;
- case 9: skipz();{
- printf("PC = %d \t A = %d \t MEM = [",PC,A);
- for(j=0;j<MaxMemSize-1;j++){
- printf("%d, ",DM[j]);
- }
- printf("%d]\n",DM[j]);
- }
- break;
- }
- }
- printf("Program Complete\n");
- fclose(Final);
- }
- system("PAUSE");
- return 0;
- }
- //Fetching instructions and address to perform operations
- struct Instruction fetchExe(){
- MAR = PC;
- PC = PC +1;
- MDR1 = IM[MAR];
- IR = MDR1;
- return IR;
- }
- //loading the value from Accumulator from DM
- int load(){
- printf("/*Loading Accumulator to DM %d */ \n", IR.Address);
- MAR = IR.Address;
- MDR2 = DM[MAR];
- A = MDR2;
- IR.OpCode = 0;
- return IR.OpCode;
- }
- //Adding the value of the loaded address to the target address
- int add(){
- MAR = IR.Address;
- MDR2 = DM[MAR];
- printf("/*Adding %d and %d */ \n", A, MDR2);
- A = A + MDR2;
- IR.OpCode = 0;
- return IR.OpCode;
- }
- //Storing the value to the target address
- int store(){
- printf("/* Storing Accumulator to memory %d */ \n", IR.Address);
- MAR = IR.Address;
- MDR2 = A;
- DM[MAR] = MDR2;
- IR.OpCode = 0;
- return IR.OpCode;
- }
- //Subtracting the value of the loaded address to the target address
- int subt(){
- MAR = IR.Address;
- MDR2 = DM[MAR];
- printf("/*Subtracting %d and %d */ \n", A, MDR2);
- A = A - MDR2;
- IR.OpCode = 0;
- return IR.OpCode;
- }
- //Inputting value
- int in(){
- printf("/* Input value */ \n");
- printf("Enter a number: ");
- scanf("%d",&A);
- IR.OpCode = 0;
- return IR.OpCode;
- }
- //showing the value by printing it
- int out(){
- printf("/* Outputting accumulator to screen */ \n");
- printf("PC = %d \t A = %d \t MEM = [",PC,A);
- for(j=0;j<MaxMemSize-1;j++){
- printf("%d, ",DM[j]);
- }
- printf("%d]\n",DM[j]);
- IR.OpCode = 0;
- return IR.OpCode;
- }
- void end(){
- printf("/*Program terminated.*/ \n");
- exit(1);
- }
- int jmp(){
- int i;
- PC = IR.Address;
- printf("/*Jumping to %d */ \n", PC);
- IR.OpCode = 0;
- return IR.OpCode;
- }
- int skipz(){
- int i;
- if(A == 0){
- PC = PC +1;
- IR.OpCode = 0;
- return IR.OpCode;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement