Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 991 Accepted Submission(s): 244
In JG system there are 6 instructions which are listed in Form 2.
Operation code is generated according to Form 3.
Destination operator code and source operator code is the register code of the register which is related to.
There are 31 registers in total. Their names are R1,R2,R3…,R30,R31. The register code of Ri is the last 5 bits of the number of i in the binary system. For eaxample the register code of R1 is 00001, the register code of R2 is 00010, the register code of R7 is 00111, the register code of R10 is 01010, the register code of R31 is 11111.
So we can transfer an instruction into a 16-bit binary code easyly. For example, if we want to transfer the instruction ADD R1,R2, we know the operation is ADD whose operation code is 000001, destination operator code is 00001 which is the register code of R1, and source operator code is 00010 which is the register code of R2. So we joint them to get the 16-bit binary code which is 0000010000100010.
However for the instruction SET Ra, there is no source register, so we fill the lower 5 bits with five 0s. For example, the 16-bit binary code of SET R10 is 0001100101000000
You are expected to write a program to transfer an instruction into a 16-bit binary code or vice-versa.
First line contains a type sign, ‘0’ or ‘1’.
‘1’ means you should transfer an instruction into a 16-bit binary code;
‘0’ means you should transfer a 16-bit binary code into an instruction.
For the second line.
If the type sign is ‘1’, an instruction will appear in the standard form which will be given in technical specification;
Otherwise, a 16-bit binary code will appear instead.
Please process to the end of file.
[Technical Specification]
The standard form of instructions is
ADD Ra,Rb
SUB Ra,Rb
DIV Ra,Rb
MUL Ra,Rb
MOVE Ra,Rb
SET Ra
which are also listed in the Form 2.
1≤a,b≤31
There is exactly one space after operation, and exactly one comma between Ra and Rb other than the instruction SET Ra. No other character will appear in the instruction.
For type ‘1’, transfer the instruction into 16-bit binary code and output it in a single line.
#include <iostream> #include <stdio.h> #include <math.h> #include <stdlib.h> #include <algorithm> #include <string.h> using namespace std; char opr0[][10] = {"0","ADD","SUB","DIV","MUL","MOVE","SET"}; char opr1[][10] = {"0","000001","000010","000011","000100","000101","000110"}; char opr2[][10] = {"0","00001","00010","00011","00100","00101","00110","00111","01000", "01001","01010","01011","01100","01101","01110","01111","10000","10001","10010","10011", "10100","10101","10110","10111","11000","11001","11010","11011","11100","11101","11110","11111" }; int main() { char s[20],s1[20],s2[100]; int opr; while(scanf("%d",&opr)!=EOF) { if(opr==1) { scanf("%s %s",s,s1); if(strcmp(s,"ADD")==0) printf("%s",opr1[1]); if(strcmp(s,"SUB")==0) printf("%s",opr1[2]); if(strcmp(s,"DIV")==0) printf("%s",opr1[3]); if(strcmp(s,"MUL")==0) printf("%s",opr1[4]); if(strcmp(s,"MOVE")==0) printf("%s",opr1[5]); if(strcmp(s,"SET")==0) printf("%s",opr1[6]); int len = strlen(s1); int sum = 0; if(strcmp(s,"SET")==0) { for(int i=1; i<len; i++) sum = sum*10+s1[i]-'0'; printf("%s",opr2[sum]); printf("00000"); } else { int i; for(i=1; i<len&&s1[i]!=','; i++) sum = sum*10+s1[i]-'0'; printf("%s",opr2[sum]); sum = 0; i+=2; for(; i<len; i++) sum = sum*10+s1[i]-'0'; printf("%s",opr2[sum]); } printf("\n"); } else { scanf("%s",s2); char s3[100],s4[100],s5[100]; int len = strlen(s2); if(strlen(s2)!=16) { printf("Error!\n"); continue; } int cnt = 0,cnt1=0,cnt2=0; for(int i=0; i<6&&i<len; i++) s3[cnt++] = s2[i]; s3[cnt]='\0'; for(int i=6; i<11&&i<len; i++) s4[cnt1++] = s2[i]; s4[cnt1]='\0'; for(int i=11; i<=15&&i<len; i++) s5[cnt2++] = s2[i]; s5[cnt2]='\0'; bool flag = false; int t1=7; for(int i=1; i<=6; i++) { if(strcmp(s3,opr1[i])==0) { t1 = i; break; } } if(t1==7) flag = true; int t2 = 32; for(int i=1; i<32; i++) { if(strcmp(s4,opr2[i])==0) { t2 = i; break; } } if(t2==32) flag = true; if(t1==6) { if(strcmp(s5,"00000")!=0) flag = true; if(!flag) printf("%s R%d\n",opr0[t1],t2); else printf("Error!\n"); } else { int t3 = 32; for(int i=1; i<32; i++) { if(strcmp(s5,opr2[i])==0) { t3 = i; break; } } if(t3==32) flag = true; if(flag) printf("Error!\n"); else printf("%s R%d,R%d\n",opr0[t1],t2,t3); } } } return 0; }