/
frmMain.cs
383 lines (337 loc) · 13.3 KB
/
frmMain.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Started 11-03-2016, Basile Van Hoorick
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using StackFish.Instructions;
using StackFish.StackItems;
namespace StackFish
{
public partial class frmMain : Form
{
private readonly static Keys[] FKeys =
{ Keys.F1, Keys.F2, Keys.F3, Keys.F4, Keys.F5, Keys.F6,
Keys.F7, Keys.F8, Keys.F9, Keys.F10, Keys.F11, Keys.F12 };
private readonly TextBox[] txtMarked; // set bold on change
private readonly Font defaultFont, changedFont;
public frmMain()
{
DpiAwareness.Set(DpiAwareness.Mode.SystemDpiAware);
InitializeComponent();
#if DEBUG
openFile("..\\..\\test.asm");
#else
openToolStripMenuItem_Click(null, null);
#endif
BringToFront();
defaultFont = new Font(txtEAX.Font, FontStyle.Regular);
changedFont = new Font(txtEAX.Font, FontStyle.Bold);
txtMarked = new TextBox[] { txtEAX, txtEBX, txtECX, txtEDX, txtESP, txtEBP, txtCarry, txtOverflow, txtParity, txtSign, txtZero };
foreach (TextBox txt in txtMarked)
txt.TextChanged += ((sender, e) =>
{
(sender as TextBox).Font = changedFont;
});
}
#region Control Events
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
openFile(dialog.FileName);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void initialStateToolStripMenuItem_Click(object sender, EventArgs e)
{
new frmInitState(this).ShowDialog(this);
}
private void stepForwardToolStripMenuItem_Click(object sender, EventArgs e)
{
if (simulator != null)
{
clearChangedMarks();
bool result = false;
bool retry = true;
while (retry)
{
try
{
result = simulator.StepForward();
retry = false;
}
catch (Exception ex)
{
// The retry functionality assumes line index was NOT changed
// TODO restore index because this isn't always true
DialogResult dr = MessageBox.Show(ex.Message, "Error", MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Error, MessageBoxDefaultButton.Button3);
if (dr == DialogResult.Abort)
{
stop();
return;
}
else if (dr == DialogResult.Retry)
retry = true;
else if (dr == DialogResult.Ignore)
simulator.Ignore();
}
}
if (result)
{
executed++; // TODO don't count labels and comments
updateExecuted();
}
}
}
private void stepBackwardToolStripMenuItem_Click(object sender, EventArgs e)
{
if (simulator != null)
{
clearChangedMarks();
if (simulator.StepBackward())
{
setStatus("Stepped backwards");
executed--;
updateExecuted();
}
else
setStatus("Could not step backwards! Program state history limit reached");
}
}
private void stopToolStripMenuItem_Click(object sender, EventArgs e)
{
stop();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
new frmAbout().Show();
}
private void clearChangedMarks()
{
foreach (TextBox txt in txtMarked)
txt.Font = defaultFont;
}
#endregion
private AssemblyFile file;
private int[] lineTextIndices; // for efficient RTB formatting..
internal List<StackItem> InitStack = null;
private Simulator simulator;
private int executed;
private void setStatus(string msg)
{
tlblStatus.Text = msg;
}
private bool run(LabelInst label)
{
if (InitStack == null)
{
MessageBox.Show("Initial stack has not been set yet!", "StackFish", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
// Recreate return address to rename callee to current label & set return address -1
StackItem lastItem = InitStack[InitStack.Count - 1];
if (lastItem is ReturnAddress)
{
ReturnAddress ra = lastItem as ReturnAddress;
InitStack[InitStack.Count - 1] = new ReturnAddress(ra.CallerName, label.Name, -1);
}
// Create simulator
this.simulator = new Simulator(file, InitStack); // TODO initial registers
simulator.OnInstructionReady += Simulator_OnInstructionReady;
simulator.Registers.OnRegisterChanged += Registers_OnRegisterChanged;
simulator.Registers.OnPointerChanged += Registers_OnPointerChanged;
simulator.Registers.OnFlagChanged += Registers_OnFlagChanged;
simulator.Stack.OnStackChanged += Stack_OnStackChanged;
simulator.Start(label.Index);
// Update controls
startToolStripMenuItem.Visible = false;
runningToolStripMenuItem.Visible = true;
updateRegisters();
updateFlags();
drawStack();
executed = 0;
updateExecuted();
setStatus("Started at " + label.Name + "! Press F5 to step forward, Shift+F5 to step backward");
return true;
}
private void stop()
{
simulator = null;
highlightLine(-1);
startToolStripMenuItem.Visible = true;
runningToolStripMenuItem.Visible = false;
setStatus("Stopped!");
}
private bool openFile(string path)
{
if (!File.Exists(path))
{
MessageBox.Show(path + " does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
// Parse instructions
file = new AssemblyFile(path);
// Initialize code textbox
loadCodeText();
// Initialize 'Start' menu
loadStartMenu();
// Update status
setStatus("Successfully loaded " + path + " (" + file.Instructions.Count + " instructions)");
return true;
}
private void loadCodeText()
{
lineTextIndices = new int[file.Instructions.Count];
Font defaultFont = new Font(rtbFile.Font, FontStyle.Regular);
Font unrecogFont = new Font(rtbFile.Font, FontStyle.Strikeout);
rtbFile.Clear();
for (int i = 0; i < file.Instructions.Count; i++)
{
Instruction inst = file.Instructions[i];
rtbFile.SelectionFont = defaultFont;
if (inst is LabelInst)
rtbFile.SelectionColor = Color.Red;
else if (inst is PushPopInst)
rtbFile.SelectionColor = Color.Green;
else if (inst is JumpInst)
rtbFile.SelectionColor = Color.Blue;
else if (inst is CallReturnInst)
rtbFile.SelectionColor = Color.Olive;
else if (inst is OtherInst) // not recognized -> strikeout
rtbFile.SelectionFont = unrecogFont;
else if (inst is Comment)
rtbFile.SelectionColor = Color.Gray;
else
rtbFile.SelectionColor = Color.Black;
string toAppend = (inst is LabelInst || inst is Comment ? "" : "\t");
toAppend += inst.Line + "\r\n";
if (i != 0)
lineTextIndices[i] = rtbFile.TextLength;
rtbFile.AppendText(toAppend);
rtbFile.ScrollToCaret();
}
rtbFile.Select(0, 0);
rtbFile.ScrollToCaret();
}
private void loadStartMenu()
{
startToolStripMenuItem.DropDownItems.Clear();
for (int i = 0; i < file.Labels.Count; i++)
{
LabelInst label = file.Labels[i];
ToolStripMenuItem menuItem = new ToolStripMenuItem(label.Op);
menuItem.Click += (sender, e) => run(label); // yay for syntactic sugar
if (i < FKeys.Length)
menuItem.ShortcutKeys = Keys.Control | FKeys[i];
startToolStripMenuItem.DropDownItems.Add(menuItem);
}
}
private int hlStart, hlLength;
private void highlightLine(int index)
{
rtbFile.Select(hlStart, hlLength);
rtbFile.SelectionBackColor = Color.White;
if (0 <= index && index < lineTextIndices.Length)
{
hlStart = lineTextIndices[index];
hlLength = rtbFile.Lines[index].Length;
rtbFile.Select(hlStart, hlLength);
rtbFile.SelectionBackColor = Color.Lime;
}
rtbFile.SelectionLength = 0;
}
#region Updating
private void Simulator_OnInstructionReady(int index, bool lastSuccess)
{
highlightLine(index);
if (index == -1)
setStatus("End of program reached! Press Shift+F5 to step back, F8 to stop");
else
setStatus((lastSuccess ? "Success!" : "Fail!") + " Waiting at line " + (index + 1) + "...");
}
private void Registers_OnRegisterChanged(Registers.Regs reg, uint value)
{
updateRegisters();
}
private void Registers_OnFlagChanged(Registers.Flags flag, bool value)
{
updateFlags();
}
private void Registers_OnPointerChanged(Registers.Regs reg, uint value)
{
drawStack();
}
private void Stack_OnStackChanged(uint address)
{
drawStack();
}
private void updateRegisters()
{
txtEAX.Text = simulator.Registers.EAX.ToString();
txtEBX.Text = simulator.Registers.EBX.ToString();
txtECX.Text = simulator.Registers.ECX.ToString();
txtEDX.Text = simulator.Registers.EDX.ToString();
txtESP.Text = simulator.Registers.ESP.ToString();
txtEBP.Text = simulator.Registers.EBP.ToString();
}
private void updateFlags()
{
txtCarry.Text = (simulator.Registers.Carry ? "1" : "0");
txtOverflow.Text = (simulator.Registers.Overflow ? "1" : "0");
txtParity.Text = (simulator.Registers.Parity ? "1" : "0");
txtSign.Text = (simulator.Registers.Sign ? "1" : "0");
txtZero.Text = (simulator.Registers.Zero ? "1" : "0");
}
private void drawStack()
{
// TODO what if not mod 4?
uint lowestAddr = simulator.Stack.Items.Keys.Min();
uint highestAddr = simulator.Stack.Items.Keys.Max();
// lowestAddr = Math.Min(lowestAddr, simulator.Registers.ESP);
lowestAddr = simulator.Registers.ESP;
highestAddr = Math.Max(highestAddr, simulator.Registers.ESP);
string toSet = "";
for (uint addr = lowestAddr; addr <= highestAddr; addr += 4)
{
StackItem item = simulator.Stack.MemGet(addr);
string toInsert = fillSpaces(addr + ": ", 8);
if (item != null)
{
toInsert += fillSpaces(item.Description, 16);
if (item is ReturnAddress)
{
if ((int)item.Value != -1) // not root
toInsert += fillSpaces("(line " + (item.Value + 1) + ")", 16);
}
else
toInsert += fillSpaces(item.Value + "", 16);
}
toInsert = fillSpaces(toInsert, 8 + 16 + 16);
if (addr == simulator.Registers.ESP)
toInsert += " <- ESP";
if (addr == simulator.Registers.EBP)
toInsert += " <- EBP";
toSet += toInsert + "\r\n";
}
txtStack.Text = toSet;
txtStack.Select(0, 0);
txtStack.ScrollToCaret();
}
private void updateExecuted()
{
tslblExec.Text = "Instructions executed: " + executed;
}
private static string fillSpaces(string input, int length)
{
while (input.Length < length)
input += " ";
return input;
}
#endregion
}
}