-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory.java
78 lines (69 loc) · 1.63 KB
/
Memory.java
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
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.io.*;
public class Memory {
static ArrayList<Byte> memory = new ArrayList<Byte>();
static Hashtable<String,Integer> varToMem = new Hashtable<String,Integer>();
static public int allocateString(String text)
{
int addr = memory.size();
int size = text.length();
for (int i=0; i<size; i++) {
memory.add(new Byte("", text.charAt(i)));
}
memory.add(new Byte("", 0));
return addr;
}
static public int allocateReal(String varname)
{
if(varToMem.containsKey(varname))
return varToMem.get(varname);
else
{
int mBefore = memory.size();
while(memory.size()%4 != 0)
memory.add(new Byte("", 0));
System.out.println("Memory size = " + memory.size() + ", m%4 = " + memory.size()%4 + ", mBefore = " + mBefore);
varToMem.put(varname, memory.size());
for(int i = 0; i<4; i++)
memory.add(new Byte("", 0));
return memory.size()-4;
}
}
static public void dumpData(PrintStream o)
{
Byte b;
String s;
int c;
int size = memory.size();
for (int i=0; i<size; i++) {
b = memory.get(i);
c = b.getContents();
if (c >= 32) {
s = String.valueOf((char)c);
}
else {
s = ""; // "\\"+String.valueOf(c);
}
o.println("DATA "+c+" ; "+s+" "+b.getName());
}
}
}
class Byte {
String varname;
int contents;
Byte(String n, int c)
{
varname = n;
contents = c;
}
String getName()
{
return varname;
}
int getContents()
{
return contents;
}
}