A SIMPLE LMC SIMULATOR IN C#


A simple version of LMC Simulator inspired from from the program developed by Dr. Magnus Bordewich of Durham University.

This simple program was developed in C#. An introduction to Little Man Computer for non-Computer Science/IT students or professionals.

The Little Man Computer (LMC) is an instructional model of a computer, created by Dr. Stuart Madnick in 1965. The LMC is generally used to teach students, because it models a simple von Neumann architecture computer—which has all of the basic features of a modern computer. It can be programmed in machine code (albeit in decimal rather than binary) or assembly code. Wikipedia





  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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Microsoft.VisualBasic;
using System.IO;
using System.Text.RegularExpressions;

namespace myLMC
{
    public partial class frmMain : Form
    {
        List<TextBox> mailboxes = new List<TextBox>();
        Thread mainThread, fetchExecuteThread;
        int ctr = 0;
        int opCode;
        int address;
        int haltIndex = 0;
        int negNum;
        bool negative;
        bool backspace;
        bool fetchExecute;
        bool suspended;
        bool running;
        bool loadingFile;

        public frmMain()
        {
            InitializeComponent();
        }

        //INITIALIZE
        private void frmMain_Load(object sender, EventArgs e)
        {
            for(int i = 1; i <= 100; i++){
                mailboxes.Add((TextBox)this.Controls.Find("textBox" + Convert.ToString(i), true)[0]); // LAHAT NG TEXTBOX SA LIST
                mailboxes[i-1].KeyDown += mailBoxes_KeyDown;
                mailboxes[i-1].KeyPress += mailBoxes_KeyPress;
                mailboxes[i-1].LostFocus += mailBoxes_LostFocus;
                mailboxes[i-1].BackColor = Color.White;
                mailboxes[i-1].MaxLength = 3;
                mailboxes[i-1].TabIndex = i - 1;
            }

            location("@startup");
            btnRun.Enabled = true;
            btnFetchExecute.Enabled = true;
            btnStop.Enabled = false;
            btnResetAll.Enabled = false;
            fetchExecute = false;
            suspended = false;
            loadingFile = false;
            lblCalculator.TextChanged += new EventHandler(lblCalculator_TextChanged);
            lblMAR.TextChanged += new EventHandler(lblMAR_TextChanged);
            lblMDR.TextChanged += new EventHandler(lblMDR_TextChanged);
            lstConsole.Items.Add("Welcome to my own simple version of Little Man Computer.");
            lstConsole.Items.Add(" ");
        }
        
        //MAIN LOOP
        private void processThread()
        {
            for (int i = 0; i < 100; i++)
            {
                if (mailboxes[i].Text == "000")
                {
                    haltIndex = i + 2;
                    break;
                }
            }

            while (ctr < haltIndex)
            {
                Control.CheckForIllegalCrossThreadCalls = false;      // Para shortcut, hindi na mag-invoke sa lahat ng controls

                //STARTS AT COUNTER
                location("@counter");
                lblOperation.Text = "Fetch";
                lblOperation.Refresh();

                if (suspended)
                {
                    lstConsole.Items.Add("--------------------------------------------------------");
                    lstConsole.Items.Add("...Fetch and execute instruction in mailbox " + format2(ctr.ToString()) + "... ");
                    lstConsole.Items.Add("--------------------------------------------------------");
                    lstConsole.Items.Add("");
                    suspended = false;
                }
                else
                {
                    Thread.Sleep(500);
                }

                lstConsole.Items.Add("Program Counter is at " + format2(ctr.ToString()));
                lstConsole.Items.Add("Incrementing program counter ");
                txtCounter.Text = format2((ctr + 1).ToString());
                txtCounter.Refresh();
                lblMAR.Text = format2(ctr.ToString());
                lblMAR.Refresh();

                //FETCH TO MAILBOXES
                location("@mailboxes");

                lblOperation.Text = "Fetch";
                lblOperation.Refresh();
                textHighlight(mailboxes[int.Parse(lblMAR.Text)]);
                lblMDR.Text = format3(mailboxes[int.Parse(lblMAR.Text)].Text);  // <--- DITO UNG INSTRUCTION CODE ex. 5xx
                lblMDR.Refresh();
                lstConsole.Items.Add("Reading mailbox " + lblMAR.Text);
                opCode = int.Parse(lblMDR.Text.Substring(0, 1));    // <--- DITO UNG OPCODE ex. 5
                address = int.Parse(lblMDR.Text.Substring(1, 2));   // <--- DITO UNG ADDRESS ex. xx
                lstConsole.Items.Add("Instruction: " + lblMDR.Text + " opCode: " + opCode + " Address: " + format2(address.ToString()));
                
                operationCodes(opCode);

                if(!suspended)
                {
                    Thread.Sleep(500);
                } 

                lstConsole.Items.Add(" ");
                ctr++;                      // <--- INCREMENT NG PROGRAM COUNTER (+1)
                lstConsole.SelectedIndex = lstConsole.Items.Count - 1;
                lstConsole.SelectedIndex = -1;
                lblOperation.Text = "done";
                lblOperation.Refresh();
            }
            
        }

        //OPERATIONS
        private void operationCodes(int code)
        {
            Thread.Sleep(500);

            switch (opCode)
            {
                case 0:
                    halt();
                    break;
                case 1:
                    add();
                    break;
                case 2:
                    subtract();
                    break;
                case 3:
                    store();
                    break;
                case 5:
                    load();
                    break;
                case 6:
                    branch();
                    break;
                case 7:
                    branchzero();
                    break;
                case 8:
                    branchpositive();
                    break;
                case 9:
                    if (address == 01){ input(); }
                    else if (address == 02){ output(); }
                    break;
            }
        }
        
        //HALT :: 000
        private void halt()
        {
            location("@mailboxes");
            lstConsole.Items.Add("Halting");
            lblOperation.Text = "HLT";
            MessageBox.Show("LMC Program has ended.");
            lstConsole.SelectedIndex = lstConsole.Items.Count - 1;

            if (fetchExecute == false)
            {
                mainThread.Abort();
            }
        }

        //ADDITION :: 1XX
        private void add()
        {
            lstConsole.Items.Add("Adding from address " + format2(address.ToString()));
            lblOperation.Text = "ADD";
            lblOperation.Refresh();
            lblMAR.Text = format2(address.ToString());
            lblMAR.Refresh();
            location("@mailboxes");
            textHighlight(mailboxes[address]);
            lblMDR.Text = mailboxes[address].Text;
            lblMDR.Refresh();
            lstConsole.Items.Add("Adding " + lblMDR.Text + " to calculator");
            location("@calculator");
            lblCalculator.Text = format3((int.Parse(lblCalculator.Text) + int.Parse(lblMDR.Text)).ToString());
            lblCalculator.Refresh();
            lstConsole.Items.Add("Calculator: " + lblCalculator.Text);
        }

        //SUBTRACTION :: 2XX
        private void subtract()
        {

            lstConsole.Items.Add("Subtracting from address " + format2(address.ToString()));
            lblOperation.Text = "SUB";
            lblMAR.Text = format2(address.ToString());
            lblMAR.Refresh();
            location("@mailboxes");
            textHighlight(mailboxes[address]);
            lblMDR.Text = mailboxes[address].Text;
            lblMDR.Refresh();
            lstConsole.Items.Add("Subtracting " + lblMDR.Text + " from calculator");

            if (int.Parse(lblMDR.Text) > int.Parse(lblCalculator.Text))
            {
                negative = true;
            }
            else
            {
                negative = false;
            }

            location("@calculator");

            if (negative)
            {
                negNum = (((int.Parse(lblCalculator.Text) - int.Parse(lblMDR.Text)) + 1000) * -1);
                lblCalculator.Text = negNum.ToString();
                lblCalculator.Refresh();
                lstConsole.Items.Add("Calculator: " + int.Parse(lblCalculator.Text) * -1);
            }
            else
            {
                lblCalculator.Text = format3((int.Parse(lblCalculator.Text) - int.Parse(lblMDR.Text)).ToString());
                lblCalculator.Refresh();
                lstConsole.Items.Add("Calculator: " + lblCalculator.Text);
            }
        }

        //STORE :: 3XX
        private void store()
        {
            lblMAR.Text = format2(address.ToString());
            lblMAR.Refresh();
            location("@calculator");
            lblOperation.Text = "Fetch";
            lblMDR.Text = lblCalculator.Text;
            lblMDR.Refresh();
            location("@mailboxes");
            lblOperation.Text = "STO";
            mailboxes[address].Text = lblMDR.Text;
            textHighlight(mailboxes[address]);
            lstConsole.Items.Add("Storing calculator value " + lblMDR.Text + " in address " + lblMAR.Text);
            lblOperation.Refresh();
        }

        //LOAD :: 5XX
        private void load()
        {
            lstConsole.Items.Add("Loading calculator with data from address from " + format2(address.ToString()));
            lblMAR.Text = format2(address.ToString());
            lblMAR.Refresh();
            location("@mailboxes");
            lblOperation.Text = "LDA";
            lblOperation.Refresh();
            textHighlight(mailboxes[address]);
            lblMDR.Text = format3(mailboxes[address].Text);
            lblMDR.Refresh();
            location("@calculator");
            lblCalculator.Text = format3(lblMDR.Text);
            lblCalculator.Refresh();
            lstConsole.Items.Add("Calculator: " + lblCalculator.Text);
        }
        
        //BRANCH ALWAYS :: 6XX
        private void branch()
        {
            lblMAR.Text = format2(address.ToString());
            lblMAR.Refresh();
            textHighlight(mailboxes[address]);
            lblMDR.Text = format2(mailboxes[address].Text);
            lblMDR.Refresh();
            lstConsole.Items.Add("Branching to " + lblMAR.Text);
            ctr = int.Parse(lblMAR.Text) - 1;
        }

        //BRANCH IF ZERO :: 7XX
        private void branchzero()
        {
            lstConsole.Items.Add("Branch on zero.");

            if (lblCalculator.Text == "000")
            {

                lblMAR.Text = format2(address.ToString());
                lblMAR.Refresh();
                textHighlight(mailboxes[address]);
                lblMDR.Text = format2(mailboxes[address].Text);
                lblMDR.Refresh();
                lstConsole.Items.Add("Calculator: " + lblCalculator.Text);
                lstConsole.Items.Add("Branching to " + lblMAR.Text);
                ctr = int.Parse(lblMAR.Text) - 1;
            }
            else if (lblCalculator.Text != "000")
            {
                lstConsole.Items.Add("Calculator: " + lblCalculator.Text);
                lstConsole.Items.Add("Not zero - no branching");
            }
        }

        //BRANCH POSITIVE :: 8XX
        private void branchpositive()
        {
            lstConsole.Items.Add("Branch on positive.");

            if (int.Parse(lblCalculator.Text) >= 0)
            {
                lblMAR.Text = format2(address.ToString());
                lblMAR.Refresh();
                textHighlight(mailboxes[address]);
                lblMDR.Text = format2(mailboxes[address].Text);
                lblMDR.Refresh();
                lstConsole.Items.Add("Calculator: " + lblCalculator.Text);
                lstConsole.Items.Add("Branching to " + lblMAR.Text);
                ctr = int.Parse(lblMAR.Text) - 1;
            }
            else if (int.Parse(lblCalculator.Text) < 0)
            {
                lstConsole.Items.Add("Negative flag is set - no branching");
            }
        }

        //INPUT :: 901
        private void input()
        {
            try
            {
                location("@input");
                lstConsole.Items.Add("Input/Output - Address " + format2(address.ToString()) + ": input");
                lblOperation.Text = "IN";
                lblOperation.Refresh();
                String str = int.Parse(format3(Microsoft.VisualBasic.Interaction.InputBox("Enter value: "))).ToString();
                if (str.Length > 3){str = str.Substring(0, 3);}
                lblCalculator.Text = format3(str);
                lblCalculator.Refresh();
                lstConsole.Items.Add("Entering input " + lblCalculator.Text + " in calculator");
            }
            catch 
            {
                MessageBox.Show("Enter numbers only within three-digit format.");
                input();
            }
            
        }
       
        //OUTPUT :: 902
        private void output()
        {
            location("@calculator");
            lstConsole.Items.Add("Input/Output - Address " + format2(address.ToString()) + ": output");
            lblMDR.Text = format3(lblCalculator.Text);
            lblMDR.Refresh();
            lblMAR.Text = format2(address.ToString());
            lblMAR.Refresh();
            location("@output");
            lblOperation.Text = "OUT";
            lblOperation.Refresh();

            if (int.Parse(lblCalculator.Text) < 0)
            {
                lstOutput.Items.Add(format3((negNum * -1).ToString()));
            }
            else
            {
                lstOutput.Items.Add(lblCalculator.Text);
            }

            lstConsole.Items.Add("Send calculator value to output: " + lblCalculator.Text);
            lstOutput.Refresh();
            lstOutput.SelectedIndex = lstOutput.Items.Count - 1;
            lstOutput.SelectedIndex = -1;
        }

        //HIGLIGHT THE TEXTBOX - FOR UI PURPOSES
        private void textHighlight(TextBox tb)
        {
            int msec;

            if (loadingFile)
            {
                msec = 50;
            }
            else
            {
                msec = 250;
            }
                tb.BackColor = Color.Red;
                tb.ForeColor = Color.White;
                tb.Refresh();
                Thread.Sleep(msec);
                tb.BackColor = Color.White;
                tb.ForeColor = Color.Black;
                tb.Refresh();
        }

        // LITTLE MAN LOCATION (GROUPBOX)
        private void location(string loc)
        {

            Thread.Sleep(500);

            switch (loc)
            {
                case "@startup":
                    gbLMC.Location = new Point(453, 228);
                    break;
                case "@counter":
                    gbLMC.Location = new Point(455, 288);
                    break;
                case "@mailboxes":
                    if (int.Parse(lblMAR.Text) < 20) { gbLMC.Location = new Point(633, 54); }
                    else if (int.Parse(lblMAR.Text) >= 20 && int.Parse(lblMAR.Text) < 40) { gbLMC.Location = new Point(633, 146); }
                    else if (int.Parse(lblMAR.Text) >= 40 && int.Parse(lblMAR.Text) < 60) { gbLMC.Location = new Point(633, 238); }
                    else if (int.Parse(lblMAR.Text) >= 60 && int.Parse(lblMAR.Text) < 80) { gbLMC.Location = new Point(633, 330); }
                    else if (int.Parse(lblMAR.Text) >= 80 && int.Parse(lblMAR.Text) < 100) { gbLMC.Location = new Point(633, 422); }
                    Thread.Sleep(1000);
                    break;
                case "@output":
                    gbLMC.Location = new Point(102, 248);
                    break;
                case "@input":
                    gbLMC.Location = new Point(102, 127);
                    break;
                case "@calculator":
                    gbLMC.Location = new Point(316, 103);
                    break;
            }

            
        }

        // FORMATTING STRINGS

        public static String format2(String str)
        {
            str = String.Format("{0}", str.ToString().PadLeft(2, '0'));
            return str;
        }

        public static String format3(String str)
        {
            str = String.Format("{0}", str.ToString().PadLeft(3, '0'));
            return str;
        }


        // BUTTON & EVENTHANDLERS


        private void btnRun_Click(object sender, EventArgs e)
        {
            try
            {
                running = true;
                fetchExecute = false;
                btnFetchExecute.Enabled = false;
                btnStop.Enabled = true;
                mainThread = new Thread(processThread);
                mainThread.IsBackground = true;
                mainThread.Start();
                btnRun.Enabled = false;
            }
            catch
            {
                MessageBox.Show("Error Threads");
                mainThread.Abort();
            }
        }

        private void btnFetchExecute_Click(object sender, EventArgs e)
        {
            try
            {
                running = true;
                fetchExecute = true;
                fetchExecuteThread = new Thread(processThread);
                fetchExecuteThread.IsBackground = true;
                fetchExecuteThread.Start();
            }
            catch
            {
                MessageBox.Show("Error Threads");
                fetchExecuteThread.Abort();
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            try
            {
                running = false;
                fetchExecute = false;
                btnFetchExecute.Enabled = true;
                btnRun.Enabled = true;
                btnResetAll.Enabled = true;
                suspended = true;
                mainThread.Suspend();
            }
            catch
            {
                mainThread.Abort();
            }
        }

        private void btnResetAll_Click(object sender, EventArgs e)
        {
            if (fetchExecute)
            {
                if (fetchExecuteThread.ThreadState == ThreadState.Suspended)
                {
                    fetchExecuteThread.Start();
                    fetchExecuteThread.Abort();
                }
            }
            else
            {
                if (!suspended)
                {
                    mainThread.Abort();
                }
                else if (suspended && mainThread.ThreadState == ThreadState.Suspended)
                {
                    mainThread.Start();
                    mainThread.Abort();
                }
            }

            lblCalculator.Text = "000";
            lblMAR.Text = "00";
            lblMDR.Text = "000";
            lblOperation.Text = "- - - -";
            txtCounter.Text = "00";
            lstOutput.Items.Clear();
            lstConsole.Items.Clear();
            tsProgramName.Clear();
            ctr = 0;
            opCode = 0;
            address = 0;
            haltIndex = 0;
            location("startup");
            frmMain_Load(this, new EventArgs());
        }

        private void btnClearMBoxes_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                mailboxes[i].Text = "000";
                mailboxes[i].ForeColor = Color.Black;
                mailboxes[i].BackColor = Color.White;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtCounter.Text = "00";
        }

        private void mailBoxes_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!backspace)
            {
                if (!char.IsDigit(e.KeyChar))
                {
                    e.Handled = true;
                }
            }
        }

        private void mailBoxes_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back)
            {
                backspace = true;
            }
            else
            {
                backspace = false;
            }
        }

        private void mailBoxes_LostFocus(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (String.IsNullOrEmpty(tb.Text))
            {
                tb.Text = "000";
            }
        }

        private void lblCalculator_TextChanged(object sender, EventArgs e)
        {
            if (!negative)
            {
                if (int.Parse(lblCalculator.Text) > 0)
                {
                    lblCalculator.ForeColor = Color.LawnGreen;
                    lblCalculator.Refresh();
                    Thread.Sleep(250);
                    lblCalculator.ForeColor = Color.White;
                    lblCalculator.Refresh();
                }
                else if (int.Parse(lblCalculator.Text) == 0)
                {
                    lblCalculator.ForeColor = Color.White;
                }
            }
            else if (negative)
            {
                lblCalculator.ForeColor = Color.Red;
                lblCalculator.Refresh();
                Thread.Sleep(250);
                lblCalculator.ForeColor = Color.White;
                lblCalculator.Refresh();
            }
        }

        private void lblMAR_TextChanged(object sender, EventArgs e)
        {
            lblMAR.ForeColor = Color.Red;
            Thread.Sleep(250);
            lblMAR.ForeColor = Color.Black;
        }

        private void lblMDR_TextChanged(object sender, EventArgs e)
        {
            lblMDR.ForeColor = Color.Red;
            Thread.Sleep(250);
            lblMDR.ForeColor = Color.Black;
        }

        private void lblOperation_TextChanged(object sender, EventArgs e)
        {
            if (lblOperation.Text == "done" && fetchExecute == true)
            {
                suspended = true;
                running = false;
                if (suspended)
                {
                    btnResetAll.Enabled = true;
                    fetchExecuteThread.Suspend();
                }
            }
        }

        private void instructionSetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new frmInstructionSet();
            frm.Show();
        }


        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("Are you sure to exit program?", "QUIT PROGRAM", MessageBoxButtons.YesNo);
          
            if (dr == DialogResult.Yes)
            {
                if (running)
                {
                  mainThread.Abort();
                }
                this.Dispose();
            }
            else if (dr == DialogResult.No)
            {
                e.Cancel = (dr == DialogResult.No);
            }      
        }

        private void smSave_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.FileName = tsProgramName.Text;
                sfd.Filter = "LMC File|*.lmc";
                sfd.Title = "Save LMC File";

                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    lstConsole.Items.Add("");
                    lstConsole.Items.Add("> Saving file...");
                    using (StreamWriter file = new StreamWriter(sfd.FileName))
                    {
                        file.WriteLine("# Simple Little Man Computer Simulator - Zilvercodes © 2017 #");
                        file.Write(format2(haltIndex.ToString()) + "%");
                        file.Write(format3(lblCalculator.Text) + "%");
                        for (int i = 0; i < 100; i++)
                        {
                            file.Write(mailboxes[i].Text + ",");
                        }
                        file.Close();
                    }
                    MessageBox.Show("LMC file was saved successfully.");
                    lstConsole.Items.Add("> LMC file was saved successfully.");
                    lstConsole.Items.Add("");
                }
                else if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                {
                    lstConsole.Items.Add("> File saving was canceled.");
                }
            }
            catch
            {
                MessageBox.Show("Error on saving file. File cannot be overwritten.");
                lstConsole.Items.Add("Error on saving file. File cannot be overwritten.");
            }

        }

        private void smOpen_Click(object sender, EventArgs e)
        {
            try
            {
                Stream lmcStream;
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "LMC files(*.lmc)|*.lmc|All files(*.*)|*.*";
                ofd.Title = "Load LMC file";

                lstConsole.Items.Add("> Loading file...");
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if ((lmcStream = ofd.OpenFile()) != null)
                    {
                        string strPath = ofd.FileName;
                        string strFilename = Path.GetFileName(strPath);
                        string strDirectory = Path.GetDirectoryName(strPath);
                        string strLine = File.ReadAllText(strPath);
                        string[] parts = strLine.Split(',');
                        int index = strLine.IndexOf("%", strLine.IndexOf('%') + 1);
                        parts[0] = strLine.Substring(index + 1, 3);                 //change the first line of text array
                        lstConsole.Items.Add("> " + strFilename.ToUpper() + "...");
                        lstConsole.Items.Add("> from " + strDirectory + "...");

                        for (int i = 0; i < 100; i++)
                        {
                            mailboxes[i].Text = parts[i].ToString();
                            loadingFile = true;
                            textHighlight(mailboxes[i]);
                        }
                        loadingFile = false;
                        lstConsole.Items.Add("> LMC file was successfully loaded");
                        lstConsole.Items.Add("");
                        tsProgramName.Text = " " + Path.GetFileNameWithoutExtension(strPath).ToUpper();
                        MessageBox.Show("LMC file was successfully loaded.");
                    }
                }
                else if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                {
                    lstConsole.Items.Add("Loading was canceled.");
                }
            }
            catch
            {
                MessageBox.Show("Error on loading file.");
            }
        }

        private void mnuAbout_Click(object sender, EventArgs e)
        {
            Form frm = new frmAbout();
            frm.Show();
        }

        private void btnAssemblyEditor_Click(object sender, EventArgs e)
        {
            Form frm = new frmAssemblyEditor();
            frm.Show();
        }




    }
}

Comments