-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
123 lines (91 loc) · 2.3 KB
/
Copy pathmemory.cpp
File metadata and controls
123 lines (91 loc) · 2.3 KB
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
#include "memory.h"
#include "word.h"
#include "data.h"
#include "registers.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
Memory::Memory()
{
head = NULL;
}// Memory()
Memory::~Memory()
{
delete head;
}//~Memory()
void Memory::insert(Word *w)
{
cout<< "hi" <<endl;
ListNode *ptr, *prev = NULL;
for(ptr = head; ptr && *ptr->word < *w; ptr = ptr -> next)
prev = ptr;
if(prev)
prev->next = new ListNode(w, ptr);
else // if prev is NULL
head = new ListNode(w, ptr);
} //insert()
Word& Memory::operator[] (int addr)
{
Word w(addr);
ListNode *ptr;
ptr = head;
for(ptr = head; ptr && *ptr->word < w; ptr = ptr->next);
if ((ptr) && !(*ptr->word < w) && !(w < *ptr->word))
return *ptr->word;
Data *data = new Data(addr);
insert(data);
return *data;
} // operator[]
Word& Memory::operator[](int addr)const
{
Word w(addr);
ListNode *ptr;
ptr = head;
for(ptr = head; ptr && *ptr->word < w; ptr = ptr->next);
if ((ptr) && !(*ptr->word < w) && !(w < *ptr->word))
return *ptr->word;
if(ptr == NULL)
{
cout << "Seg fault at address: <" << addr << ">";
exit(1);
} // if ptr is NULL
return *ptr->word;
} // operator[] const
ListNode::ListNode(Word *w, ListNode *listN)
{
word = w;
next = listN;
} // ListNode()
ListNode::~ListNode()
{
delete word;
} // ~ListNode()
const Instruction& Memory::fetch(Registers *registers) const
{
const Instruction &instruction =
dynamic_cast<const Instruction&> ((*this)[registers->get(Registers::eip)]);
registers->set(Registers::eip, registers->get(Registers::eip) + 4);
return instruction;
} // fetch()
istream& operator>> (istream &is, Memory &memory)
{
char line[256], *ptr;
int address = 100;
Instruction *instruction;
while(is.getline(line, 256))
{
for(ptr = strchr(line, '\t'); ptr; ptr = strchr(line, '\t'))
*ptr = ' '; // replace all tabs with space characters
for(ptr = line; *ptr == ' '; ptr++); // get past leading spaces
if(!(*(ptr + strlen(ptr) - 1) == ':') && !(*ptr == '.'))
{
instruction = new Instruction(address);
address += 4;
*instruction = ptr;
memory.insert(instruction);
} // if not directive, nor main:
} // while more in file
return is;
} // operator>>