-
Notifications
You must be signed in to change notification settings - Fork 1
/
sml.c
110 lines (107 loc) · 2.48 KB
/
sml.c
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
/*
SML(simple machine language) simulator Simpletron DEMO.
Language Format: xxbb
The Lower 2 digits bb is memory address.
The Higher 2 digits xx is the operater.
Operators: READ, WRITE, LOAD, STORE, ADD, SUB, MUL, DIV, MOD, JMP, JMPN, JMPE, HALT
winxos 2012-11-04
*/
#include <stdio.h>
#include <string.h>
#include <windows.h>
#define READ 10
#define WRITE 11
#define LOAD 20
#define STORE 21
#define ADD 30
#define SUB 31
#define MUL 32
#define DIV 33
#define MOD 34
#define JMP 40
#define JMPN 41
#define JMPZ 42
#define HALT 43
#define MAX_MEM 100
int mem[MAX_MEM]={0},ADDER=0; //ONLY ONE REGISTER ADDER
void showMemory() //Pretty Show Memory For Debugging
{
int i;
printf("\nMEMORY:\n ");
for(i=0;i<10;i++)printf("%6d",i);
printf("\n 0");
for(i=0;i<MAX_MEM;i++)
{
printf("%6d",mem[i]);
if(i%10==9 && i!=MAX_MEM-1)printf("\n%3d",1+i/10);
}
printf("\n");
}
void loadCode(int c[])
{
int len=0;
while(c[len++]!=-1);
memset(mem,0,sizeof(int)*MAX_MEM);
memcpy(mem,c,len*sizeof(int));
}
void runSML(int c[]) //
{
int op=0,addr=0,*pt=mem;
while(1)
{
//printf("%d\n",*pt);
op=*pt/100; //xxbb
addr=*pt%100;
pt++;
switch(op)
{
case READ:printf("input:");scanf("%d",&mem[addr]);break;
case WRITE:printf("%d ",mem[addr]);break;
case LOAD:ADDER=mem[addr];break;
case STORE:mem[addr]=ADDER;break;
case ADD:ADDER+=mem[addr];break;
case SUB:ADDER-=mem[addr];break;
case MUL:ADDER*=mem[addr];break;
case DIV:ADDER/=mem[addr];break;
case JMP:pt=mem+addr;break;
case JMPN:ADDER<0?pt=mem+addr:0;break;
case JMPZ:ADDER==0?pt=mem+addr:0;break;
case HALT:printf("\nhalt");return;
default:break;
}
//Sleep(10);
}
}
int* inputCode()
{
int buf[100];
printf("Enter -1 to end input.\n\n");
int ct=0,li=0;
while(ct!=-1)
{
printf("%02d ? ",li);
scanf("%d",&ct);
buf[li++]=ct;
}
return buf;
}
int main()
{
int c[]={
//1007,1008,2007,3008,2109,1109,4300,-1 /*ADD TWO NUM*/
//1009,1010,2009,3110,4107,1109,4300,1110,4300,-1 /*SHOW BIG NUM*/
/*5 num sum*/
//2011,4209,3112,2111,1013,2013,3014,2114,4000,1114,4300,5,1,0,0,-1
//2036,2137,0000,2035,3137,4133,2037,3036,2137,3136,2138,0000,2038,3136,3136,4129,2037,3338,3238,3137,4222,4024,0000,4031,0000,2038,3136,2138,4011,0000,1137,0000,4002,0000,4300,99,1,0,0,-1
};
printf("\t\tSimpletron simulator.\n\n");
printf("\t\t\tVersion 1.0\n\n");
printf("\t\t\t\twinxos 2012/11/05\n\n\n");
while(1)
{
loadCode(inputCode()); //calculate the code length
runSML(c);
showMemory();
}
return 0;
}