#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
string codes[65537] = {
"inp 0",
"inp 1",
"add 0 1",
"out 0",
"end"
}, op, s;
ll mem[65537] = {}, stk[17] = {};
int ip, sp;
stringstream ss;
/*
stp (stack top) 1
set %num %mem
mov %src %dest 1
swp %src %dest 1
psh %src 1
pop 1
cmp %src %dest lss/gtr/leq/geq/eql/neq %mem 1
inp %mem 1
out %mem 1
add %dest %src 1
sub %dest %src 1
mul %dest %src 1
div %dest %src 1
mod %dest %src 1
gcd %dest %src 1
lcm %dest %src 1
pow %dest %src 1
asc %mem 1
jmp %mem
ifj %con %mem
end
*/
void read_codes()
{
ifstream fin("codes.in");
for (int i = 0; i < 65536 && getline(fin, codes[i]); i++);
}
int main()
{
// read_codes();
ip = 0;
sp = -1;
while (true)
{
op = codes[ip++];
ss.clear();
ss << op;
ss >> s;
if (s == "" || s == "end") break;
else if (s == "set")
{
ss >> s;
int num = stoi(s);
ss >> s;
mem[stoi(s)] = num;
}
else if (s == "mov")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
*tep = *tsp;
}
else if (s == "swp")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
int temp = *tsp;
*tsp = *tep;
*tep = temp;
}
else if (s == "add")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
*tsp += *tep;
}
else if (s == "sub")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
*tsp -= *tep;
}
else if (s == "mul")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
*tsp *= *tep;
}
else if (s == "div")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
*tsp /= *tep;
}
else if (s == "mod")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
*tsp %= *tep;
}
else if (s == "gcd")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
*tsp = __gcd(*tsp, *tep);
}
else if (s == "lcm")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
*tsp = (*tsp) / __gcd(*tsp, *tep) * (*tep);
}
else if (s == "pow")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
*tsp = pow(*tsp, *tep);
}
else if (s == "psh")
{
string s2;
ss >> s2;
ll *tsp = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
stk[++sp] = *tsp;
}
else if (s == "pop")
sp--;
else if (s == "inp")
{
string s2;
ss >> s2;
ll *tsp = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
cin >> *tsp;
}
else if (s == "out")
{
string s2;
ss >> s2;
ll *tsp = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
cout << *tsp;
}
else if (s == "asc")
{
string s2;
ss >> s2;
ll *tsp = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
cout << (char)(*tsp);
}
else if (s == "cmp")
{
string s2;
ss >> s2;
ll *tsp = nullptr, *tep = nullptr, *trp = nullptr;
if (s2 == "stp")
tsp = &stk[sp];
else
tsp = &mem[stoi(s2)];
ss >> s2;
if (s2 == "stp")
tep = &stk[sp];
else
tep = &mem[stoi(s2)];
ss >> s2;
bool res;
if (s2 == "lss")
res = *tsp < *tep;
else if (s2 == "gtr")
res = *tsp > *tep;
else if (s2 == "leq")
res = *tsp <= *tep;
else if (s2 == "geq")
res = *tsp >= *tep;
else if (s2 == "equ")
res = *tsp == *tep;
else if (s2 == "neq")
res = *tsp != *tep;
else
throw ("Unexpected Comparison");
ss >> s2;
if (s2 == "stp")
trp = &stk[sp];
else
trp = &mem[stoi(s2)];
*trp = !res;
}
else if (s == "jmp")
ss >> ip;
}
return 0;
}