#include <iostream>
#include <sstream>
#include <deque>
#include <vector>
#include <string>
#include <map>
#include <cctype>
#include <cstdlib>
#include <cassert>
using namespace std;

#define VERBOSE 1 // verboseness level: 0 silent, 1 normal (report result only), 2 verbose
#define MAXSTEPS 100000

#define IFVERB(x) if (VERBOSE>=1) { x ; }
#define IFVERB2(x) if (VERBOSE>=2) { x ; }
#define ERROR(x) { IFVERB(cerr << x << endl;); exit(1); }
#define FOREACH(it,s) for (typeof(s.begin()) it=s.begin(); it!=s.end(); ++it)

vector<string> SPLIT( const string& s ) {
  vector<string> res;
  string t="", delim = ";";
  for ( unsigned i = 0 ; i != s.size() ; i++ ) { 
    if ( delim.find( s[i] ) != string::npos ) { 
      if ( !t.empty() ) { res.push_back( t ); t = ""; } 
    } else { 
      t += s[i]; 
    } 
  } 
  if ( !t.empty() ) { res.push_back(t); } 
  return res; 
}

typedef unsigned int number; // note that we assume that int has at least 32 bits

deque<number> Q; // queue
vector<string> Prog; // program
map<string,int> Labels; // maps labels to lines
map<string,int>::iterator label; // we are gonna look for labels 
vector<number> Vars; // variables a-z
int IP; // instruction pointer
int SC; // step count

int QGET() { if (Q.empty()) ERROR("Queue empty."); int t=Q.front(); Q.pop_front(); return t; }

int main(void) {
  assert(sizeof(number) >= 4);
  string line;
  number x,y;

  while (getline(cin,line)) {
    vector<string> cmds = SPLIT(line);
    FOREACH(it,cmds) {
      Prog.push_back(*it);
      stringstream ss(*it); string w; ss >> w; 
      if (w=="label") { 
        if (!(ss >> w)) ERROR("Label without a name.");
        Labels[w]=Prog.size()-1; 
      }
    }
  }
  Prog.push_back("stop");

  IFVERB2(cerr << "Program:" << endl;);
  IFVERB2(FOREACH(it,Prog) cerr << "  " << *it << endl;);
  IFVERB2(cerr << "-----" << endl;);

  Vars.resize(300,0);
  IP=SC=0;
  while (1) {
    IFVERB2(cerr << "  ip=" << IP << " command=" << Prog[IP] << " steps=" << SC << endl;);
    stringstream ss(Prog[IP]);
    string cmd=""; ss >> cmd;
    bool done = false;
    if (cmd=="") { // empty command
      done = true;
    }
    if (cmd=="label") {
      done = true;
    }
    if (cmd=="jeq") { // jump if equal 
      string r1, r2, l;
      if (!(ss >> r1 >> r2 >> l)) ERROR("Incorrectly formatted instruction.");
      if (r1.size()!=1u || r2.size()!=1u || !islower(r1[0]) || !islower(r2[0])) ERROR("Invalid register name.");
      if (Vars[int(r1[0])] == Vars[int(r2[0])]) {
        label=Labels.find(l); 
        if (label!=Labels.end()) IP=(label->second)-1;
        else ERROR("No such label.");
      }
      done = true;
    }
    if (cmd=="jgt") { // jump if greater
      string r1, r2, l;
      if (!(ss >> r1 >> r2 >> l)) ERROR("Incorrectly formatted instruction.");
      if (r1.size()!=1u || r2.size()!=1u || !islower(r1[0]) || !islower(r2[0])) ERROR("Invalid register name.");
      if (Vars[int(r1[0])] > Vars[int(r2[0])]) {
        label=Labels.find(l); 
        if (label!=Labels.end()) IP=(label->second)-1;
        else ERROR("No such label.");
      }
      done = true;
    }
    if (cmd=="jz") { // jump if zero
      string r1, l;
      if (!(ss >> r1 >> l)) ERROR("Incorrectly formatted instruction.");
      if (r1.size()!=1u || !islower(r1[0])) ERROR("Invalid register name.");
      if (Vars[int(r1[0])] == 0) {
        label=Labels.find(l); 
        if (label!=Labels.end()) IP=(label->second)-1;
        else ERROR("No such label.");
      }
      done = true;
    }
    if (cmd=="jump") {
      string l;
      if (!(ss >> l)) ERROR("Incorrectly formatted instruction.");
      label=Labels.find(l); 
      if (label!=Labels.end()) IP=(label->second)-1;
      else ERROR("No such label.");
      done = true;
    }
    if (cmd=="jempty") { // jump if queue empty
      string l;
      if (!(ss >> l)) ERROR("Incorrectly formatted instruction.");
      if (Q.empty()) {
        label=Labels.find(l); 
        if (label!=Labels.end()) IP=(label->second)-1;
        else ERROR("No such label.");
      }
      done = true;
    }
    if (cmd=="print") {
      cout << QGET() << endl;
      done = true;
    }
    if (cmd=="stop") {
      IFVERB(cerr << "Terminated correctly after " << SC << " steps." << endl;)
      return 0;
      done = true; // :)
    }
    if (cmd=="add") {
      x=QGET(); y=QGET(); Q.push_back((x+y)%65536);
      done = true;
    }
    if (cmd=="sub") {
      x=QGET(); y=QGET(); Q.push_back((65536+x-y)%65536);
      done = true;
    }
    if (cmd=="mul") {
      x=QGET(); y=QGET(); Q.push_back((x*y)%65536);
      done = true;
    }
    if (cmd=="div") {
      x=QGET(); y=QGET(); if (!y) ERROR("Division by zero."); Q.push_back(x/y);
      done = true;
    }
    if (cmd=="mod") {
      x=QGET(); y=QGET(); if (!y) ERROR("Division by zero."); Q.push_back(x%y);
      done = true;
    }
    if (cmd=="get") {
      string r1;
      if (!(ss >> r1)) ERROR("Incorrectly formatted instruction.");
      if (r1.size()!=1u || !islower(r1[0])) ERROR("Invalid register name.");
      Vars[int(r1[0])] = QGET();
      done = true;
    }
    if (cmd=="put") {
      number x;
      if (ss >> x) {
        Q.push_back(x%65536);
      } else {
        ss.clear();
        string r1;
        if (!(ss >> r1)) ERROR("Incorrectly formatted instruction.");
        if (r1.size()!=1u || !islower(r1[0])) ERROR("Invalid register name.");
        Q.push_back( Vars[int(r1[0])] );
      }
      done = true;
    }
    if (!done) ERROR("Invalid instruction.");
    IP++; SC++; 
    if (SC>MAXSTEPS) ERROR("Too many steps.");
  }
  return 0;
}

// vim: fdm=marker
