Skip to content

Commit

Permalink
Initial commmit to build the project
Browse files Browse the repository at this point in the history
Signed-off-by: Sarita Singh <saritasingh.0425@gmail.com>
  • Loading branch information
sritasngh committed Sep 20, 2021
1 parent df6ec8a commit c9a523a
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
*.exe
*.out
*.app

# Build
build
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ project(xeus-asm LANGUAGES CXX
DESCRIPTION "Jupyter Kernel for ASM"
HOMEPAGE_URL "https://github.com/itssingh/xeus-asm")

include_directories(include/xasm)
include_directories(include)

add_executable(main src/main.cc src/instruction.cc src/interpreter.cc src/utils.cc)
4 changes: 4 additions & 0 deletions add_two_numbers.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MVI A, 05
MVI B, 08
ADD B
HLT
10 changes: 6 additions & 4 deletions include/xasm/instruction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
#include <string>
#include <vector>

#include "utils.hpp";
#include "utils.hpp"

using namespace std;

void ADD(string arg1, string registers[], bool flag[],
map<string, string> &memory);

void MOV (string argument1,string argument2,string registers[],bool flag[],map<string,string> &memory);
map<string, string> &memory);
void MOV(string argument1, string argument2, string registers[], bool flag[],
map<string, string> &memory);
void MVI(string arg1, string arg2, string registers[], bool flags[],
map<string, string> &memory);
#endif // INSTRUCTION_HPP_
5 changes: 2 additions & 3 deletions include/xasm/interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
using namespace std;

struct Register {
string A, B, C, D, E, G, H, PC;
bool S, Z, AC, P, C;
string A, B, C, D, E, H, L, PC;
bool S, Z, AC, P, CA;
friend ostream &operator<<(ostream &out, const Register &r);
};
class Interpreter {
Expand All @@ -34,7 +34,6 @@ class Interpreter {
public:
Interpreter();
Register getRegister();
void printRegisters();
void input();
void multiLine();
void execution(string pc, map<string, string> &Memory,
Expand Down
11 changes: 7 additions & 4 deletions include/xasm/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ bool validityRegisterPair(string reg);
int registerNumber(string str);
string hexAdd(string arg1, string arg2, bool flag[], bool carry);

map<string, function<void(string, string[], bool[], map<string, string> &)>>
instruction1{{"add", ADD}};
// map<string, function<void(string, string[], bool[], map<string, string> &)>>
// instruction1{{"add", ADD}};

map<string, function<void(string, string, string[], bool[], map<string, string> &)>>
instruction2{{"mov", MOV}};
// map<string, function<void(string, string, string[], bool[], map<string, string> &)>>
// instruction2{{"mov", MOV}};

bool isHexadecimal(char a);
bool validityData(string a);

#endif // UTILS_HPP_
Binary file added resources/ASM Kernel.pdf
Binary file not shown.
Empty file removed src/CMakeLists.txt
Empty file.
27 changes: 27 additions & 0 deletions src/instruction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,31 @@ void MOV(string argument1, string argument2, string registers[], bool flag[],
exit(0);
}
}
}

void MVI (string arg1,string arg2,string registers[],bool flags[],map<string,string> &memory){
int l1=arg1.length();
int l2=arg2.length();
if(l1==1&&l2==2){
if(arg1=="M"){
string address=registers[5]+registers[6];
if(validityData(arg2))
memory[address]=arg2;
else{
cout<<"Error: "<<"Invalid content\nThe program will quit\n";
exit(0);
}
}
else if(validityRegisters(arg1)&&validityData(arg2)){
cout << "Do Nothing\n";
}
else{
cout<<"Error: "<<"Invalid content\nThe program will quit\n";
exit(0);
}
}
else{
cout<<"Error: "<<"Invalid content\nThe program will quit\n";
exit(0);
}
}
17 changes: 3 additions & 14 deletions src/interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ostream &operator<<(ostream &out, const Register &r) {
out << "Registers\nA\t" << r.A << "\nBC\t" << r.B << "\t" << r.C << "\nDE\t"
<< r.D << "\t" << r.E << "\nHL\t" << r.H << "\t" << r.L << "\nPC\t"
<< r.PC << "\n";
out << "Flags\nS\tZ\tAC\tP\tC\n"
out << "Flags\nS\tZ\tAC\tP\tAC\n"
<< r.S << "\t" << r.Z << "\t" << r.AC << "\t" << r.P << "\t" << r.C
<< "\n";
return out;
Expand All @@ -29,11 +29,11 @@ Interpreter::Interpreter() {
}

Register Interpreter::getRegister() {
Register register = {registers[0], registers[1], registers[2], registers[3],
Register resister = {registers[0], registers[1], registers[2], registers[3],
registers[4], registers[5], registers[6], pc,
flag[7], flag[6], flag[4], flag[2],
flag[0]};
return register;
return resister;
}

void Interpreter::input() {
Expand All @@ -50,17 +50,6 @@ void Interpreter::input() {
sequence.push_back(start);
}

void Interpreter::printRegisters() {
int i = 0;
for (const auto &registerName : registers) {
std::cout << "Name: " << registerNames[i]
<< "\tRegister Value: " << (ui)registerName.registerValue
<< "\tBits:" << std::bitset<8>(registerName.registerValue)
<< '\n';
i++;
}
}

void Interpreter::multiLine() {
cin.ignore();
cout << "\nEnter the code:\n";
Expand Down
6 changes: 2 additions & 4 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
int main(int argc, char *argv[]) {
Interpreter instance;
instance.input();
if (argc == 2) {
instance.multiLine();
cout<<instance.getRegister();
}
instance.multiLine();
cout<<instance.getRegister();
}
35 changes: 27 additions & 8 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ string executionCode(string command, string Registers[], bool flag[],
commandPart.push_back(inst);
part = strtok(NULL, delimiter);
}
instruction1[commandPart[0]](commandPart[1], Registers, flag, memory);
instruction2[commandPart[0]](commandPart[1], commandPart[2], Registers, flag,
memory);
// instruction1[commandPart[0]](commandPart[1], Registers, flag, memory);
// instruction2[commandPart[0]](commandPart[1], commandPart[2], Registers, flag,
// memory);
if (commandPart[0] == "MOV") {
MOV(commandPart[1], commandPart[2], Registers, flag, memory);
commandSize = operationSize(commandPart[0]);
Expand All @@ -56,6 +56,10 @@ string executionCode(string command, string Registers[], bool flag[],
ADD(commandPart[1], Registers, flag, memory);
commandSize = operationSize(commandPart[0]);
return nextAddress(programCounter, commandSize);
} else if (commandPart[0] == "MVI") {
MVI(commandPart[1], commandPart[2], Registers, flag, memory);
commandSize = operationSize(commandPart[0]);
return nextAddress(programCounter, commandSize);
}
return "";
}
Expand All @@ -76,11 +80,9 @@ bool validityAddress(string data) {
}

int operationSize(string str) {
string one[] = {"HLT", "MOV", "STAX", "XCHG", "ADD", "SUB", "INR",
"DCR", "INX", "DCX", "DAD", "CMA", "CMP"};
string two[] = {"MVI", "ADI", "SUI"};
string three[] = {"LXI", "LDA", "STA", "SHLD", "LHLD", "JMP",
"JC", "JNZ", "JNC", "JZ", "SET"};
string one[] = {"HLT", "MOV"};
string two[] = {"MVI"};
string three[] = {"SET"};
const char *ch = str.c_str();
char *var = (char *)ch;
const char *delimiter = " ,";
Expand Down Expand Up @@ -217,3 +219,20 @@ string hexAdd(string arg1, string arg2, bool flag[], bool carry) {

return resultant;
}

bool isHexadecimal(char a){

if((a>='0' && a<='9') || (a>='A' && a<='F'))
return true;
else
return false;
}

bool validityData(string a){

int l=a.length();
if(l==2 && isHexadecimal(a[0]) && isHexadecimal(a[1]))
return true;
else
return false;
}

0 comments on commit c9a523a

Please sign in to comment.