0

I'm working on a small custom Assembler

I have a vector of struc to storing OPCODE informations (Mnemonic, number and type of argument, parsing function,...)

typedef char args_type_t; typedef struct op_s { std::string opcode; char nbr_args; args_type_t type[4]; int code; Tryte (*fct)(std::vector<std::string>); } op_t; 

the parsing function is pointer on a static member function :

{"MOV", 2, {T_REGISTER | T_ADDRESS, T_REGISTER | T_ADDRESS | T_CONSTANT}, 1, &Opcodes::MOV}, 

and the function :

class Opcodes { public: static Tryte Opcodes::MOV(std::vector<std::string> _opMap) { return Tryte(0); } }; 

I try this, but I get SEGFAULT (str() is a member function of Tryte) :

for (int i = 0; i < opMap.size(); i++) { for (int j = 0; j < op_tab.size(); j++) { if (!op_tab[j].opcode.compare(opMap[i][2])) { std::cout << "OPBYTE : " << op_tab[j].fct(opMap[i]).str() << std::endl; } } } 

I want to call my function without instanciate Opcodes object it's posible ?

EDIT :

my error was here : if (!op_tab[j].opcode.compare(opMap[i][2])) my mnemonic is the 1st item n opMap

2
  • 2
    Try (op_tab[j].fct)(opMap[i]).str(). The braces are important for grouping, since fct is a pointer to a function, not a member function. Commented Apr 5, 2017 at 10:00
  • thx, but I found my problem : !op_tab[j].opcode.compare(opMap[i][2]) in my opMap the mnemonic is at place 1 not 2 >< Commented Apr 5, 2017 at 10:03

1 Answer 1

1

Your code seems right, so perhaps a debugger information could help a bit. But we can try to improve the friendlyness of the code by using a std::function:

typedef char args_type_t; #include <functional> typedef struct op_s { std::string opcode; char nbr_args; args_type_t type[4]; int code; std::function<Tryte(std::vector<std::string>>)> fct; } op_t; 

As for the sefgault, send us the backtrace. Also, try to use range-based-for as it doesn't needs to tranverse the map to get the element again (as you are doing inside of the inner loop)

for (auto op : opMap) { for (auto tab : op_tab) { if (!tab.opcode.compare(op[1])) { std::cout << "OPBYTE : " << tab.fct(op).str() << std::endl; } } } 

One common fix that you can do to not miss the indexes anymore is to use an Enum holding the possibilities.

enum Columns { FuncPointer, UserData } for (auto op : opMap) { for (auto tab : op_tab) { if (!tab.opcode.compare(op[FuncPointer])) { std::cout << "OPBYTE : " << tab.fct(op).str() << std::endl; } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.