简单的ASM虚拟机相关问题


#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef pairint> verb;

typedef pair com;

//end the program
int Halt(int num)
{
    return num;
}

/*put the top two elements out of the stack
add them, and put the results into the top of the stack*/
void Add(Stack<int>& st)
{
    int a = st.pop();
    int b = st.pop();
    int add = a + b;
    st.push(add);
}

/*put the top two elements out of the stack
the second stack element minus the first stack element
and put the result into the top of the stack*/
void Sub(Stack<int>& st)
{
    int a = st.pop();
    int b = st.pop();
    int sub = b - a;
    st.push(sub);
}

/*put the top two elements out of the stack
mul them, and put the results into the top of the stack*/
void Mul(Stack<int>& st)
{
    int a = st.pop();
    int b = st.pop();
    int mul = a * b;
    st.push(mul);
}

/*put the top two elements out of the stack
the second stack element div the first stack element
and put the result into the top of the stack*/
void Div(Stack<int>& st, int num)
{
    string err = "Error";
    int a = st.pop();
    int b = st.pop();
    if (a == 0){
        cout << err << endl;
        Halt(num);
    } else{
        int div = b / a;
        st.push(div);
    }
}

/*Put the stack top element a out of the stack
and update the state to map the variable x to a*/
void SetVar(string x, Vector& state, Stack<int>& st)
{
    int a = st.pop();
    verb xst(x, a);
    state.add(xst);
}

//Place the value of the variable x in the state at the top of the stack
void Var(string x, Vector& state, Stack<int>& st, int num)
{
    int j = 0;
    string err = "Error";
    for (int i = 0; i < state.size(); i++){
        if (state[i].first == x){
            st.push(state[i].second);
            break;
        }
        if (state[i].first != x) j++;
        if (j == state.size() - 1)
        {
            cout <on to pc = n
void Jmp(int n, int num)
{
    string err = "Error";
    if (n >= num){
        cout << err << endl;
        Halt(num);
    }
}

/*Take the top two elements out of the stack,
if the two are equal, jump to pc = n*/
void JmpEq(Stack<int>& st, int n, int num)
{
    int a = st.pop();
    int b = st.pop();
    if (a == b) Jmp(n, num);
}

/*Take the top two elements out of the stack.
If the second stack element is greater than the first stack element,
jump to pc = n*/
void JmpGt(Stack<int>& st, int n, int num)
{
    int a = st.pop();
    int b = st.pop();
    if (b > a) Jmp(n, num);
}

/*Take the top two elements out of the stack.
If the second stack element is less than the first stack element,
jump to pc = n*/
void JmpLt(Stack<int>& st, int n, int num)
{
    int a = st.pop();
    int b = st.pop();
    if (b < a) Jmp(n, num);
}

//Place the integer n into the top of the stack
void Const(Stack<int>& st, int n)
{
    st.push(n);
}

//Print the value of variable x and change
void Print(string x, Vector& state, int num)
{
    int j = 0;
    string err = "Error";
    for (int i = 0; i < state.size(); i++){
        if (state[i].first == x){
            cout << state[i].second << endl;
            break;
        }
        if (state[i].first != x) j++;
        if (j == state.size() - 1)
        {
            cout << err << endl;
            Halt(num);
        }
    }
}

//use the orders before to output results
int main()
{
    int pc = 0;
    int num ;
    cin >> num;
    string comm;
    com mid("x", "10");
    Stack<int> st;
    Vector state;
    Vector command(num, mid);
    Vector<int> numb(num, 0);

    for (int i = 0; i < num; i++){
        getline(cin, comm);
        for (int j = 0; j < comm.size(); j++){
            if (comm[j] == 32){
                command[i].first = comm.substr(0, j);
                if (comm.size() == 3) break;
                else {
                    for (int m = j + 1; m < comm.size(); m++){
                        command[i].second += comm[m];
                    }
                }
            }
        }
    }
    for (int i = 0; i < num; i++){
        if (command[i].first == "Jmp" ||
            command[i].first == "JmpEq" ||
            command[i].first == "JmpGt" ||
            command[i].first == "JmpLt" ||
            command[i].first == "Const"){
                string str = command[i].second;
                numb[i] = stoi(str);
            }
    }
    for (int i = 0; i < num; i++){
        if (command[i].first == "Add"){
            Add(st);
            pc += 1;
            continue;
        }
        if (command[i].first == "Sub"){
            Sub(st);
            pc += 1;
            continue;
        }
        if (command[i].first == "Mul"){
            Mul(st);
            pc += 1;
            continue;
        }
        if (command[i].first == "Div"){
            Div(st, num);
            pc += 1;
            continue;
        }
        if (command[i].first == "SetVar"){
            SetVar(command[i].second, state, st);
            pc += 1;
            continue;
        }
        if(command[i].first == "Var"){
            Var(command[i].second, state, st, num);
            pc += 1;
            continue;
        }
        if(command[i].first == "Jmp"){
            Jmp(numb[i], num);
            pc = numb[i];
            i = pc;
            continue;
        }
        if(command[i].first == "JmpEq"){
            JmpEq(st, numb[i], num);
            pc = numb[i];
            continue;
        }
        if(command[i].first == "JmpGt"){
            JmpGt(st, numb[i], num);
            pc = numb[i];
        }
        if(command[i].first == "JmpLt"){
            JmpLt(st, numb[i], num);
            pc = numb[i];
            continue;
        }
        if(command[i].first == "Const"){
            Const(st, numb[i]);
            pc += 1;
            continue;
        }
        if(command[i].first == "Print"){
            Print(command[i].first, state, num);
            pc += 1;
            continue;
        }
        if(command[i].first == "Halt"){
            i = Halt(num);
            pc += 1;
        }
    }
    //output error when the number of command losed
    return 0;
}

以上代码目的是实现一个简单的"ASM"语言(Assembly)的虚拟机。其中的.h头文件定义了一些如add的函数。在输入命令时,会出现异常输出结果,如输入
18
Const 10
SetVar z
Const 1
SetVar y
Var z
Const 0
JmpEq 16
Var y
Var z
Mul
SetVar y
Var z
Const 1
Sub
SetVar z
Jmp 4
Print y
Halt
后会错误输出打印如下内容:
Error
Error

Process returned 0 (0x0) execution time : 2.545 s
Press any key to continue.
希望可以帮忙解答(此系本人第一次发帖,如有不妥之处敬请海涵)

代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。