This project was created as an assignment for Languages and tools for programming I
The goal of this project was to create simulate simple Computer, with its own memory, running assembly-like language. Simulation had to be implemented using metaprogramming and C++ templates.
Computer is parameterized by
- size of memory
- type of basic word (had to be integer type)
Computer class has core method boot, which loads and executes programme written in TMPAsm (Template Metha programming Assembly) in compile time.
Programme written in TMPAsm language is effectively a sequence of instructions.
Id(const char*)- creates a label or variable identificator.Num<word_type>- pvalue, represents numerical value
Examples:Num<10>,Num<-1>Mem<Addr>- lvalue (and also pvalue), provides access to memory addressAddr, which has to be valid pvalue.
Examples of correct memory references:Mem<Num<0>>,Mem<Lea<Id("a")>>Lea<Id>- pvalue, loads effective address of variable labaled withIdlabel.
Example:Lea<Id("A")>D<Id, Value>- declares variable with idIdand numberical valueValue.
Example:D<Id("A"), Num<5>>Mov<Dst, Src>- copies value of Src into Dst, Dst must be a valid l-value, and Src must be a valid r-value
Examples:Mov<Mem<Num<0>>,Num<13>>,Mov<Mem<Lea<Id("abc")>>,Mem<Num<0>>>
Add<Arg1, Arg2>- addsArg2toArg1and saves result toArg1(Arg1has to be lvalue,Arg2has to be pvalue).Sub<Arg1, Arg2>- substractsArg2fromArg1and saves result toArg1(Arg1has to be lvalue,Arg2has to be pvalue).Inc<Arg>- IncrementsArg(it has to be lvalue).Dec<Arg>- DecrementsArg(it has to be lvalue).
Examples:Add<Mem<Num<0>>,Num<1>>, Inc<Mem<Lea<Id("a")>>>
Arithmetic operations set
- ZF flag (zero flag) in processor to
1if result is equal to0, flag is set to0otherwise. - SF flag (sign flag) in processor to
1if result is negative, to0otherwise.
- Logical operators:
And<Arg1, Arg2>
Or<Arg1, Arg2>
Result is stored inArg1(it has to be lvalue).
Not<Arg>(result is stored inArg, it has to be lvalue)
These operations can change zero flag.
Examples:And<Mem<Num<0>>,Num<1>>,Not<Mem<Lea<Id("a")>>>. Cmp<Arg1, Arg2>works likeSub<Arg1,Arg2>but doesn't change Arg1, only sets flags.
Example:Cmp<Mem<Num<0>>,Num<1>>.Label<Id("label")>- creates a Label of certain Id, that can be accesed later (like in assembly).- Conditional jumps
Jmp<Id>- jumps to a label.
Jz<Id>- jumps if Label ZF is set to1.
Js<Label>- jumps if Label SF is set to1.
Examples:Jmp<Id("label")>,Jz<Id("stop")>
You can find example programs in computer_example.cc file or tests directory. Also, task description in Polish can be found in desc directory.