#include
using namespace std;
#define hex 'x'<(y).to_string()
#define asm(s) mem[pc]=bitset<16>(s).to_ulong()
//#define RESULT_ONLY
map m;
map addr;
ifstream fin("in.txt"),from_mem("mem_in.txt");
ofstream fout("out.txt"),to_mem("mem_out.txt");
string line[65540];
int orig,mem[65540],R[8],nzp,first,pc,cnt,n;
int to10(string s){
int res=0,base;
if(s[0]=='#') base=10;
else base=16;
for(int i=s.size()-1,j=1,t;i;i--){
if(i==1&&s[1]=='-'){
res*=-1;continue;
}
if(s[i]>='0'&&s[i]<='9') t=s[i]-'0';
else t=s[i]-'A'+10;
res+=t*j;
j*=base;
}
return res;
}
void Init(){
m[".ORIG"]=1;m[".FILL"]=2;m[".BLKW"]=3;m[".STRINGZ"]=4;
m["ADD"]=5;m["AND"]=6;m["BRp"]=7;m["BRz"]=8;m["BRzp"]=9;
m["BRn"]=10;m["BRnp"]=11;m["BRnz"]=12;m["BRnzp"]=13;
m["JMP"]=14;m["JSR"]=15;m["JSRR"]=16;m["LD"]=17;m["LDI"]=18;
m["LDR"]=19;m["LEA"]=20;m["NOT"]=21;m["RET"]=22;m["ST"]=23;
m["STI"]=24;m["STR"]=25;m["TRAP"]=26;m["GETC"]=27;
m["OUT"]=28;m["PUTS"]=29;m["IN"]=30;m["HALT"]=32;
string s;
while(fin>>s&&s[0]!='x');
orig=to10(s);
fin.seekg(0,ios::beg);
for(int i=0;i<65536;i++){
string t;
from_mem>>t;
from_mem>>t;
if(t!="x0000") mem[i]=to10(t);
}
}
void Show(){
fout<<"pc:"<>s;
if(s==".END") break;
if(m[s]==1||s[0]==';'){ //.ORIG||注释
getline(fin,s);
n--;
continue;
}
if(!m[s]){ //Lable
addr[s]=n;
line[n]+=s+' ';
fin>>s;
}
if(m[s]==2){ //.FILL
fin>>t;
line[n]+=s+' '+t;
mem[n]=to10(t);
}
if(m[s]==3){ //.BLKW
int tmp;
fin>>tmp;
line[n]=s+char(tmp+'0');
n+=tmp-1;
}
if(m[s]==4){ //.STRINGZ
fin>>t;
line[n]=s+t;
int len=t.size();
for(int i=1;i4){//else
getline(fin,t);
line[n]+=s+t;
}
}
for(int i=0;i<=7;i++){
for(int j=0;j<=5;j++) fin.get();
fin>>s;
R[i]=to10(s);
}
}
void SecondPass(){
string s;
for(pc=orig;pc>s;
if(!m[s]) ss>>s;
if(m[s]==5){//ADD
ss>>s;
if(s[6]=='R') asm("0001"+bin(3,s[1]-'0')+bin(3,s[4]-'0')+"000"+bin(3,s[7]-'0'));
else{
string t;
for(int i=6;s[i];i++) t+=s[i];
asm("0001"+bin(3,s[1]-'0')+bin(3,s[4]-'0')+"1"+bin(5,to10(t)));
}
}
if(m[s]==6){//AND
ss>>s;
if(s[6]=='R') asm("0101"+bin(3,s[1]-'0')+bin(3,s[4]-'0')+"000"+bin(3,s[7]-'0'));
else{//AND
string t;
for(int i=6;s[i];i++) t+=s[i];
asm("0101"+bin(3,s[1]-'0')+bin(3,s[4]-'0')+"1"+bin(5,to10(t)));
}
}
if(m[s]>=7&&m[s]<=13){//BR
int tnzp=m[s]-'0'-6;
ss>>s;
if(s[0]=='#'||s[0]=='x') asm("0000"+bin(3,tnzp)+bin(9,to10(s)));
else asm("0000"+bin(3,tnzp)+bin(9,addr[s]-pc-1));
}
if(m[s]==14){//JMP
ss>>s;
asm("1100000"+bin(3,R[s[1]-'0'])+"000000");
}
if(m[s]==15){//JSR
ss>>s;
if(s[0]=='#'||s[0]=='x') asm("01001"+bin(11,to10(s)));
else asm("01001"+bin(11,addr[s]-pc-1));
}
if(m[s]==16){//JSRR
ss>>s;
asm("0100000"+bin(3,s[1]-'0')+"000000");
}
if(m[s]==17){//LD
ss>>s;
string t;
for(int i=3;s[i];i++)t+=s[i];
if(t[0]=='#'||t[0]=='x') asm("0010"+bin(3,s[1]-'0')+bin(9,to10(t)));
else asm("0010"+bin(3,s[1]-'0')+bin(9,addr[t]-pc-1));
}
if(m[s]==18){//LDI
ss>>s;
string t;
for(int i=3;s[i];i++)t+=s[i];
if(t[0]=='#'||t[0]=='x') asm("1010"+bin(3,s[1]-'0')+bin(9,to10(t)));
else asm("1010"+bin(3,s[1]-'0')+bin(9,addr[t]-pc-1));
}
if(m[s]==19){//LDR
ss>>s;
string t;
for(int i=6;s[i];i++) t+=s[i];
asm("0110"+bin(3,s[1]-'0')+bin(3,s[4]-'0')+bin(6,to10(t)));
}
if(m[s]==20){//LEA
ss>>s;
string t;
for(int i=3;s[i];i++) t+=s[i];
if(t[0]=='#'||t[0]=='x') asm("1110"+bin(3,s[1]-'0')+bin(9,to10(t)));
else asm("1110"+bin(3,s[1]-'0')+bin(9,addr[t]-pc-1));
}
if(m[s]==21){//NOT
ss>>s;
asm("1001"+bin(3,s[1]-'0')+bin(3,s[4]-'0')+"111111");
}
if(m[s]==22){//RET
ss>>s;
asm("1100000111000000");
}
if(m[s]==23){//ST
ss>>s;
string t;
for(int i=3;s[i];i++)t+=s[i];
if(t[0]=='#'||t[0]=='x') asm("0011"+bin(3,s[1]-'0')+bin(9,to10(t)));
else asm("0011"+bin(3,s[1]-'0')+bin(9,addr[t]-pc-1));
}
if(m[s]==24){//STI
ss>>s;
string t;
for(int i=3;s[i];i++)t+=s[i];
if(t[0]=='#'||t[0]=='x') asm("1011"+bin(3,s[1]-'0')+bin(9,to10(t)));
else asm("1011"+bin(3,s[1]-'0')+bin(9,addr[t]-pc-1));
}
if(m[s]==25){//STR
ss>>s;
string t;
for(int i=6;s[i];i++) t+=s[i];
asm("0111"+bin(3,s[1]-'0')+bin(3,s[4]-'0')+bin(6,to10(t)));
}
if(m[s]==26){ //TRAP
ss>>s;
asm("11110000"+bin(8,to10(s)));
}
if(m[s]>=27&&m[s]<=32) asm("11110000"+bin(8,bin(8,m[s]+5)));
}
}
void Execute(){
string s;
for(pc=orig;pc>s)) continue;
if(!m[s]) ss>>s;
if(s=="HALT") break;
if(m[s]==5){ //ADD
ss>>s;
if(s[6]=='R') R[s[1]-'0']=(R[s[4]-'0']+R[s[7]-'0'])%65536;
else{
string t;
for(int i=6;s[i];i++) t+=s[i];
R[s[1]-'0']=(R[s[4]-'0']+to10(t))%65536;
}
setnzp(s);
}
if(m[s]==6){ //AND
ss>>s;
if(s[6]=='R') R[s[1]-'0']=R[s[4]-'0']&R[s[7]-'0'];
else{
string t;
for(int i=6;s[i];i++) t+=s[i];
R[s[1]-'0']=R[s[4]-'0']&to10(t);
}
setnzp(s);
}
//BR
if( (m[s]==7&&nzp>0) || (m[s]==8&&!nzp) || (m[s]==9&&!(nzp<0)) || (m[s]==10&&nzp<0) || (m[s]==11&&nzp) || (m[s]==12&&!(nzp>0)) || (m[s]==13) ){
ss>>s;
if(s[0]=='#'||s[0]=='x') pc+=to10(s);
else pc=addr[s]-1;
}
if(m[s]==14){ //JMP
ss>>s;
pc=R[s[1]-'0']-1;
}
if(m[s]==15){ //JSR
R[7]=pc;
ss>>s;
if(s[0]=='#'||s[0]=='x') pc+=to10(s);
else pc=addr[s]-1;
}
if(m[s]==16){ //JSRR
R[7]=pc;
ss>>s;
pc=R[s[1]-'0']-1;
}
if(m[s]==17){ //LD
ss>>s;
string t;
for(int i=3;s[i];i++)t+=s[i];
if(t[0]=='#'||t[0]=='x') R[s[1]-'0']=mem[pc+to10(t)+1];
else R[s[1]-'0']=mem[addr[t]];
setnzp(s);
}
if(m[s]==18){ //LDI
ss>>s;
string t;
for(int i=3;s[i];i++) t+=s[i];
if(t[0]=='#'||t[0]=='x') R[s[1]-'0']=mem[mem[pc+to10(t)+1]];
else R[s[1]-'0']=mem[mem[addr[t]]];
setnzp(s);
}
if(m[s]==19){ //LDR
ss>>s;
string t;
for(int i=6;s[i];i++) t+=s[i];
if(t[0]=='#'||t[0]=='x') R[s[1]-'0']=mem[R[s[4]-'0']+to10(t)];
else R[s[1]-'0']=mem[R[s[4]-'0']+addr[t]];
setnzp(s);
}
if(m[s]==20){ //LEA
ss>>s;
string t;
for(int i=3;s[i];i++) t+=s[i];
if(t[0]=='#'||t[0]=='x') R[s[1]-'0']=pc+to10(t)+1;
else R[s[1]-'0']=addr[t];
setnzp(s);
}
if(m[s]==21){ //NOT
ss>>s;
R[s[1]-'0']=~R[s[4]-'0'];
setnzp(s);
}
if(m[s]==22) pc=R[7]; //RET
if(m[s]==23){ //ST
ss>>s;
string t;
for(int i=3;s[i];i++) t+=s[i];
if(t[0]=='#'||t[0]=='x') mem[pc+to10(t)+1]=R[s[1]-'0'];
else mem[addr[t]]=R[s[1]-'0'];
}
if(m[s]==24){ //STI
ss>>s;
string t;
for(int i=3;s[i];i++) t+=s[i];
if(t[0]=='#'||t[0]=='x') mem[mem[pc+to10(t)+1]]=R[s[1]-'0'];
else mem[mem[addr[t]]]=R[s[1]-'0'];
}
if(m[s]==25){ //STR
ss>>s;
string t;
for(int i=6;s[i];i++) t+=s[i];
if(t[0]=='#'||t[0]=='x') mem[R[s[4]-'0']+to10(t)]=R[s[1]-'0'];
else mem[R[s[4]-'0']+addr[t]]=R[s[1]-'0'];
}
if(m[s]==26){ //TRAP
ss>>s;
if(s=="x20") R[0]=cin.get();
if(s=="x21") cout<