Simpletron Simulator (II.)

Source: Internet
Author: User
Tags abs function prototype
After the previous article, here is the source code of the Simpletron simulator I implemented. I think many places in the code look uncomfortable, but now it will be available.
The first is Simpletron.h:
#ifndef _simpletron_h_
#define _simpletron_h_

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#define READ 10
#define WRITE 11
#define LOAD 20
#define STORE 21
#define ADD 30
#define SUBTRACT 31
#define DIVIDE 32
#define MULTIPLY 33
#define BRANCH 40
#define BRANCHNEG 41
#define Branchzero 42
#define HALT 43

/*global Variables and function prototype*/
int accumulator;
Short Instructioncounter;
int instructionregister;
Short Operationcode;
short operand;
int memory[100];

static void Init ();
void instruction ()
{
printf ("* * * Welcome to Simpletron ***/n");
printf ("* * * Please enter your program one instruction ***/n");
printf ("* * * (or data word) at a time, I'll type the ***/n");
printf ("* * * location number and a question mark (?)." /n ");
printf ("* * * then type the word for that location. /n ");
printf ("* * * Type the sentinel-99999 to stop entering ***/n");
printf ("* * * your program. /n ");
}
void Dump ()
{
Char sign = ' + ';
int start = 9;
int i = 0;
int j = 0;
int skip = 7;
printf ("/n/n");
printf ("registers:/n");
printf ("accumulator/t/t/t%+.4d/n", accumulator);
printf ("instructioncounter/t/t%.2d/n", instructioncounter);
printf ("instructionregister/t/t%+.4d/n", Instructionregister);
printf ("operationcode/t/t/t%.2d/n", Operationcode);
printf ("operand/t/t/t/t%.2d/n", operand);
printf ("n");
printf ("memory:/n");
for (i=0; i<10; i++)
{
if (i > 0)
printf ("%7d", I);
Else
printf ("%9d", I);
}
printf ("n");
for (i=0; i<10; i++)
{
printf ("%.2d", i*10);
for (j=0; j<10; j + +)
{
printf ("%+.4d", Memory[i*10 + j]);
}
printf ("n");
}
}
void Halt ()
{
printf ("/n*** simpletron execution terminated ***/n");
Dump ();
Exit (1);
}
void error (int errno)/*errno indicates error number*/
{
Switch (errno)
{
Case 1:
printf ("[instruction is negative.] /n ");
Case 2:
printf ("[Attempt to devide by zero.] /n ");
Case 3:
printf ("[Result beyond-9999 ~ +9999.] /n ");

}
printf ("* * * Simpletron execution abnormally terminated. /n ");
Dump ();
Exit (-1);
}
int read ();
int write ();
int load ();
int store ();
int add ();
int subtract ();
int divide ();
int multiiply ();
int branch ();
int Branchneg ();
int Branchzero ();
void Start ();
void exec ();
void Exechelper ();

#ifdef __cplusplus
}
#endif

#endif/* _simpletron_h_ * *

And then the SIMPLETRON.C:
#include <stdio.h>
#include <math.h>
#include "Simpletron.h"

int main (int argc, char *argv[])
{
Init ();
instruction ();
Start ();
exec ();

return 0;
}
static void Init ()
{
accumulator = 0;
Instructioncounter = 0;
Instructionregister = 0;
Operationcode = 0;
operand = 0;
}
void Start ()
{
int resultofscanf = 0;/*indicate whether scanf () succeed. 0 for false*/
int scanfed =-1;
while (scanfed!=-99999)
{
printf ("%.2d?", Instructioncounter);
RESULTOFSCANF = scanf ("%d", &scanfed);
if (0 = resultofscanf | | (scanfed!=-99999 && ABS (scanfed) >9999))
{
printf ("[A wrong instruction or data word. -9999 ~ +9999)]/n ");
Continue
}
if (scanfed = = 99999)
{
instructioncounter--;
Break
}
memory[instructioncounter++] = scanfed;
}
printf ("* * * * program. Loading completed ***/n ");
}
void exec ()
{
Init ();
printf ("* * * * program. Execution begins ***/n ");
while (instructioncounter<100)
{
Instructionregister = memory[instructioncounter++];
if (instructionregister <= 0)
{
instructioncounter--;
Error (1);
}
Operationcode = instructionregister/100;
operand = instructionregister%100;
Exechelper ();
}
}
void Exechelper ()
{
Switch (Operationcode)
{
Case READ:
Read ();
Case WRITE:
Write (); break;
Case LOAD:
Load ();
Case STORE:
Store (); break;
Case ADD:
Add (); break;
Case Subtract:
Subtract ();
Case DIVIDE:
Divide ();
Case MULTIPLY:
Multiply ();
Case BRANCH:
Branch ();
Case BRANCHNEG:
Branchneg ();
Case Branchzero:
Branchzero ();
Case HALT:
Halt ();
}
}
int read ()
{
int resultofscanf = 0;/*indicate whether scanf () succeed. 0 for false*/
int scanfed =-1;
while (1)
{
printf ("?");
RESULTOFSCANF = scanf ("%d", &scanfed);
if (0 = resultofscanf | | ABS (SCANFED) >9999)
printf ("[Please input a integer (-9999 ~ +9999).] /n ");
memory[operand] = scanfed;
Break
}
return scanfed;
}
int write ()
{
printf ("%d", memory[operand]);
return 1;
}
int load ()
{
Accumulator = memory[operand];
return 1;
}
int Store ()
{
memory[operand] = accumulator;
return 1;
}
int Add ()
{
Accumulator + + memory[operand];
return 1;
}
int subtract ()
{
Accumulator-= Memory[operand];
return 1;
}
int divide ()
{
int div = memory[operand];
if (div = = 0)
{
Error (2);
}
Else
{
Accumulator/= Div;
}
return 1;
}
int multiply ()
{
Accumulator *= memory[operand];
if (ABS (accumulator) > 9999)
Error (3);
return 1;
}
int branch ()
{
Instructioncounter = operand;
return 1;
}
int Branchneg ()
{
if (Accumulator < 0)
Instructioncounter = operand;
return 1;
}
int Branchzero ()
{
if (accumulator = 0)
Instructioncounter = operand;
return 1;
}

There is no comment in the code, a simple, and lazy. But less than 300 lines of code are believed to be not baffled. Please correct me.

PS: Suddenly found in the code memory 100 words, but did not determine whether the program entered more than 100 lines, this is a very large bug, but assume that there is no such a long program now. I do not want to change this now (very good change), you are interested in their own hands.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.