Skip to content

Subset of assembler simulation written in C++ templates.

Notifications You must be signed in to change notification settings

matiutd888/cpp-tasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Assembly simulation using Metaprogramming in C++

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.

Instruction types

Fundamental 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 address Addr, 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 with Id label.
    Example: Lea<Id("A")>
  • D<Id, Value> - declares variable with id Id and numberical value Value.
    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>>>

Arithmetic operations

  • Add<Arg1, Arg2> - adds Arg2 to Arg1 and saves result to Arg1 (Arg1 has to be lvalue, Arg2 has to be pvalue).
  • Sub<Arg1, Arg2> - substracts Arg2 from Arg1 and saves result to Arg1 (Arg1 has to be lvalue, Arg2 has to be pvalue).
  • Inc<Arg> - Increments Arg (it has to be lvalue).
  • Dec<Arg> - Decrements Arg (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 1 if result is equal to 0, flag is set to 0 otherwise.
  • SF flag (sign flag) in processor to 1 if result is negative, to 0 otherwise.

Additional instructions

  • Logical operators:
    And<Arg1, Arg2>
    Or<Arg1, Arg2>
    Result is stored in Arg1 (it has to be lvalue).
    Not<Arg> (result is stored in Arg, 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 like Sub<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 to 1.
    Js<Label> - jumps if Label SF is set to 1.
    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.

About

Subset of assembler simulation written in C++ templates.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published