Advertisement
Guest User

Elisey-Feodor

a guest
May 20th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.79 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <fcntl.h>
  6. FILE * inputs;
  7.  
  8. enum code {
  9. HALT = 0,
  10. SYSCALL = 1,
  11. ADD = 2,
  12. ADDI = 3,
  13. SUB = 4,
  14. SUBI = 5,
  15. MUL = 6,
  16. MULI = 7,
  17. DIV = 8,
  18. DIVI = 9,
  19. LC = 12,
  20. SHL = 13,
  21. SHLI = 14,
  22. SHR = 15,
  23. SHRI = 16,
  24. AND = 17,
  25. ANDI = 18,
  26. OR = 19,
  27. ORI = 20,
  28. XOR = 21,
  29. XORI = 22,
  30. NOT = 23,
  31. MOV = 24,
  32. ADDD = 32,
  33. SUBD = 33,
  34. MULD = 34,
  35. DIVD = 35,
  36. ITOD = 36,
  37. DTOI = 37,
  38. PUSH = 38,
  39. POP = 39,
  40. CALL = 40,
  41. CALLI = 41,
  42. RET = 42,
  43. CMP = 43,
  44. CMPI = 44,
  45. CMPD = 45,
  46. JMP = 46,
  47. JNE = 47,
  48. JEQ = 48,
  49. JLE = 49,
  50. JL = 50,
  51. JGE = 51,
  52. JG = 52,
  53. LOAD = 64,
  54. STORE = 65,
  55. LOAD2 = 66,
  56. STORE2 = 67,
  57. LOADR = 68,
  58. LOADR2 = 69,
  59. STORER = 70,
  60. STORER2 = 71
  61. };
  62.  
  63. enum type {
  64. RI = 0,
  65. RR = 1,
  66. RM = 2,
  67. BD = 3
  68. };
  69.  
  70. enum code getCommandCode (char* commandName) {
  71. char command;
  72. if (!strcmp(commandName, "halt")) {
  73. command = 0;
  74. }
  75. if (!strcmp(commandName, "syscall")) {
  76. command = 1;
  77. }
  78. if (!strcmp(commandName, "add")) {
  79. command = 2;
  80. }
  81. if (!strcmp(commandName, "addi")) {
  82. command = 3;
  83. }
  84. if (!strcmp(commandName, "sub")) {
  85. command = 4;
  86. }
  87. if (!strcmp(commandName, "subi")) {
  88. command = 5;
  89. }
  90. if (!strcmp(commandName, "mul")) {
  91. command = 6;
  92. }
  93. if (!strcmp(commandName, "muli")) {
  94. command = 7;
  95. }
  96. if (!strcmp(commandName, "div")) {
  97. command = 8;
  98. }
  99. if (!strcmp(commandName, "divi")) {
  100. command = 9;
  101. }
  102. if (!strcmp(commandName, "lc")) {
  103. command = 12;
  104. }
  105. if (!strcmp(commandName, "shl")) {
  106. command = 13;
  107. }
  108. if (!strcmp(commandName, "shli")) {
  109. command = 14;
  110. }
  111. if (!strcmp(commandName, "shr")) {
  112. command = 15;
  113. }
  114. if (!strcmp(commandName, "shri")) {
  115. command = 16;
  116. }
  117. if (!strcmp(commandName, "and")) {
  118. command = 17;
  119. }
  120. if (!strcmp(commandName, "andi")) {
  121. command = 18;
  122. }
  123. if (!strcmp(commandName, "or")) {
  124. command = 19;
  125. }
  126. if (!strcmp(commandName, "ori")) {
  127. command = 20;
  128. }
  129. if (!strcmp(commandName, "xor")) {
  130. command = 21;
  131. }
  132. if (!strcmp(commandName, "xori")) {
  133. command = 22;
  134. }
  135. if (!strcmp(commandName, "not")) {
  136. command = 23;
  137. }
  138. if (!strcmp(commandName, "mov")) {
  139. command = 24;
  140. }
  141. if (!strcmp(commandName, "addd")) {
  142. command = 32;
  143. }
  144. if (!strcmp(commandName, "subd")) {
  145. command = 33;
  146. }
  147. if (!strcmp(commandName, "muld")) {
  148. command = 34;
  149. }
  150. if (!strcmp(commandName, "divd")) {
  151. command = 35;
  152. }
  153. if (!strcmp(commandName, "itod")) {
  154. command = 36;
  155. }
  156. if (!strcmp(commandName, "dtoi")) {
  157. command = 37;
  158. }
  159. if (!strcmp(commandName, "push")) {
  160. command = 38;
  161. }
  162. if (!strcmp(commandName, "pop")) {
  163. command = 39;
  164. }
  165. if (!strcmp(commandName, "call")) {
  166. command = 40;
  167. }
  168. if (!strcmp(commandName, "calli")) {
  169. command = 41;
  170. }
  171. if (!strcmp(commandName, "ret")) {
  172. command = 42;
  173. }
  174. if (!strcmp(commandName, "cmp")) {
  175. command = 43;
  176. }
  177. if (!strcmp(commandName, "cmpi")) {
  178. command = 44;
  179. }
  180. if (!strcmp(commandName, "cmpd")) {
  181. command = 45;
  182. }
  183. if (!strcmp(commandName, "jmp")) {
  184. command = 46;
  185. }
  186. if (!strcmp(commandName, "jne")) {
  187. command = 47;
  188. }
  189. if (!strcmp(commandName, "jeq")) {
  190. command = 48;
  191. }
  192. if (!strcmp(commandName, "jle")) {
  193. command = 49;
  194. }
  195. if (!strcmp(commandName, "jl")) {
  196. command = 50;
  197. }
  198. if (!strcmp(commandName, "jge")) {
  199. command = 51;
  200. }
  201. if (!strcmp(commandName, "jg")) {
  202. command = 52;
  203. }
  204. if (!strcmp(commandName, "load")) {
  205. command = 64;
  206. }
  207. if (!strcmp(commandName, "store")) {
  208. command = 65;
  209. }
  210. if (!strcmp(commandName, "load2")) {
  211. command = 66;
  212. }
  213. if (!strcmp(commandName, "store2")) {
  214. command = 67;
  215. }
  216. if (!strcmp(commandName, "loadr")) {
  217. command = 68;
  218. }
  219. if (!strcmp(commandName, "loadr2")) {
  220. command = 69;
  221. }
  222. if (!strcmp(commandName, "storer")) {
  223. command = 70;
  224. }
  225. if (!strcmp(commandName, "storer2")) {
  226. command = 71;
  227. }
  228. return command;
  229. }
  230.  
  231. enum type getCommandType(enum code command) {
  232. switch (command) {
  233. case HALT: return BD;
  234. case SYSCALL: return RI;
  235. case ADD: return RR;
  236. case ADDI: return RI;
  237. case SUB: return RR;
  238. case SUBI: return RI;
  239. case MUL: return RR;
  240. case MULI: return RI;
  241. case DIV: return RR;
  242. case DIVI: return
  243. RI;
  244. case LC: return RI;
  245. case SHL: return RR;
  246. case SHLI: return RI;
  247. case SHR: return RR;
  248. case SHRI: return RI;
  249. case AND: return RR;
  250. case ANDI: return RI;
  251. case OR: return RR;
  252. case ORI: return RI;
  253. case XOR: return RR;
  254. case XORI: return RI;
  255. case NOT: return RI;
  256. case MOV: return RR;
  257. case ADDD: return RR;
  258. case SUBD: return RR;
  259. case MULD: return RR;
  260. case DIVD: return RR;
  261. case ITOD: return RR;
  262. case DTOI: return RR;
  263. case PUSH: return RI;
  264. case POP: return RI;
  265. case CALL: return RR;
  266. case CALLI: return BD;
  267. case RET: return BD;
  268. case CMP: return RR;
  269. case CMPI: return RI;
  270. case CMPD: return RR;
  271. case JMP: return BD;
  272. case JNE: return BD;
  273. case JEQ: return BD;
  274. case JLE: return BD;
  275. case JL: return BD;
  276. case JGE: return BD;
  277. case JG: return BD;
  278. case LOAD: return RM;
  279. case STORE: return RM;
  280. case LOAD2: return RM;
  281. case STORE2: return RM;
  282. case LOADR: return RR;
  283. case LOADR2: return RM;
  284. case STORER: return RR;
  285. case STORER2: return RR;
  286. default: return BD;
  287. }
  288. }
  289.  
  290. char* labels[100];
  291. int* labelsNums;
  292. int labelsCount;
  293.  
  294. int clearAsm() {
  295. FILE* input = fopen("input.fasm", "r");
  296. FILE* inputJumps = fopen("input_jumps.fasm", "w");
  297.  
  298. char line[100];
  299. char commandStek[100];
  300. int lineNum = 0;
  301.  
  302. while (fscanf(input, "%[^\n]\n", line) != EOF) {
  303. int i = 0;
  304. int j;
  305. while ((i < 100) && (line[i] != ';') && (line[i] != ':')) {
  306. commandStek[i] = line[i];
  307. i++;
  308. }
  309. commandStek[i] = '\0';
  310.  
  311. //printf("%s %d\n", commandStek, i);
  312.  
  313. if (i != 0) {
  314. if (line[i] == ':') {
  315. labels[labelsCount] = malloc(100 * sizeof(char));
  316. strcpy(labels[labelsCount], commandStek);
  317. if ((labelsCount % 10) == 0) {
  318. labelsNums = realloc(labelsNums, labelsCount + 10);
  319. }
  320. labelsNums[labelsCount] = lineNum - labelsCount;
  321. labelsCount++;
  322. }
  323. lineNum++;
  324. }
  325. }
  326.  
  327. fseek(input, 0L, SEEK_SET);
  328.  
  329. while (fscanf(input, "%[^\n]\n", line) != EOF) {
  330. int i = 0;
  331. int j;
  332. while ((i < 100) && (line[i] != ';') && (line[i] != ':')) {
  333. commandStek[i] = line[i];
  334. i++;
  335. }
  336. commandStek[i] = '\0';
  337. //printf("%s %d\n", commandStek, i);
  338.  
  339. if (i != 0) {
  340. if (line[i] != ':') {
  341. char command[10];
  342. char second[10];
  343. sscanf(commandStek, "%s %s", command, second);
  344. int commandCode = getCommandCode(command);
  345. if (((commandCode >= 46) && (commandCode <= 52)) || !strcmp(command, "end") || !strcmp(command, "call") || !strcmp(command, "calli")) {
  346. for (j = 0; j < labelsCount; j++) {
  347. if (!strcmp(labels[j], second)) {
  348. fprintf(inputJumps, "%s %d\n", command, labelsNums[j]);
  349. break;
  350. }
  351. }
  352. } else {
  353. fprintf(inputJumps, "%s\n", commandStek);
  354. }
  355. }
  356. }
  357. }
  358. fclose(input);
  359. fclose(inputJumps);
  360. return 0;
  361. }
  362.  
  363. unsigned int rr(char* line) {
  364. char* command;
  365. int reg1;
  366. int reg2;
  367. short int num;
  368.  
  369. sscanf (line, "%ms r%d r%d %hd", &command, &reg1, &reg2, &num);
  370. int commandCode = getCommandCode(command);
  371. unsigned int res;
  372. res = commandCode « 24;
  373. res += reg1 « 20;
  374. res += reg2 « 16;
  375. res += num;
  376. free(command);
  377. return res;
  378. }
  379.  
  380. unsigned int ri(char* line) {
  381. char* command;
  382. int reg1;
  383. int num;
  384.  
  385. sscanf (line, "%ms r%d %d", &command, &reg1, &num);
  386. int commandCode = getCommandCode(command);
  387. unsigned int res;
  388. res = commandCode « 24;
  389. res += reg1 « 20;
  390. res += num & ((2 « 20) - 1);
  391. free(command);
  392. return res;
  393. }
  394.  
  395. unsigned int rm(char* line) {
  396. char* command;
  397. int reg1;
  398. int num;
  399.  
  400. sscanf (line, "%ms r%d %u", &command, &reg1, &num);
  401. int commandCode = getCommandCode(command);
  402. unsigned int res;
  403. res = commandCode « 24;
  404. res += reg1 « 20;
  405. res += num & ((2 « 20) - 1);
  406. free(command);
  407. return res;
  408. }
  409. unsigned int bd(char* line) {
  410. char* command;
  411. int num;
  412.  
  413. sscanf (line, "%ms %u", &command, &num);
  414. int commandCode = getCommandCode(command);
  415. unsigned int res;
  416. res = commandCode « 24;
  417. res += num & ((2«24) - 1);
  418. free(command);
  419. return res;
  420. }
  421.  
  422. int assembler() {
  423. FILE* input = fopen("input_jumps.fasm", "r");
  424. FILE* output = fopen("output.o", "w");
  425.  
  426. //fprintf(output, "ThisIsFUPM2Exec\0");
  427.  
  428. char line[100];
  429. int mask;
  430. unsigned int commandWithArgs;
  431.  
  432. while (fscanf(input,
  433. "%[^\n]\n", line) != EOF) {
  434. char command[10];
  435. enum code commandCode;
  436. enum type commandType;
  437.  
  438. sscanf(line, "%s", command);
  439. commandCode = getCommandCode(command);
  440. commandType = getCommandType(commandCode);
  441. switch (commandType) {
  442. case RI:
  443. commandWithArgs = ri(line);
  444. break;
  445. case RM:
  446. commandWithArgs = rm(line);
  447. break;
  448. case RR:
  449. commandWithArgs = rr(line);
  450. break;
  451. case BD:
  452. commandWithArgs = bd(line);
  453. break;
  454. }
  455.  
  456.  
  457. mask = 255;
  458.  
  459. fprintf(output, "%c%c%c%c", (commandWithArgs) & mask, (commandWithArgs » 8) & mask, (commandWithArgs » 16) & mask, (commandWithArgs » 24) & mask);
  460. }
  461. fseek(output, 0, SEEK_SET);
  462. fprintf(output, "ThisIsFUPM2Exec");
  463. fprintf(output, "__No_matter__");
  464. fprintf(output, "%c", (commandWithArgs & mask));
  465. fprintf(output, "meoy");
  466. fprintf(output, "My English hometask: The Caspian sea is the largest enclosed body of water on earth, which can be classified as the largest drainless lake, or as a full-fledged sea. The length of the Caspian sea from North to South is about 1200 kilometers , from West to East — from 195 to 435 kilometers, an average of 310-320 kilometers. The Caspian sea washes the shores of five coastal countries: Kazakhstan, Iran, Turkmenistan, Russia, Azerbaijan. It is also really the third deepest lake");
  467. fclose(input);
  468. fclose(output);
  469. return 0;
  470. }
  471.  
  472. int reg[17]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  473. int * memory;
  474. int size_of_memory;
  475. int * stek;
  476.  
  477. int trans_ri(char a, char b, char c, int r, int df){
  478. r = (c & 240);
  479. df = ((c & 15) « 16) + (b « 8) + a;
  480. return 0;
  481.  
  482. }
  483. ;
  484. int trans_rr(char a, char b, char c, int r, int l, int df){
  485. r = (c & 240);
  486. l = (c & 15);
  487. df = (b « 8) + a;
  488. return 0;
  489. }
  490. int trans_rm(char a, char b, char c, int r, unsigned un){
  491. r = (c & 240);
  492. un = ((c&15) « 16) + (b « 8) + a;
  493. return 0;
  494. }
  495. int trans_bd(char a, char b, char c, unsigned un){
  496. un = (c « 16) + (b « 8) + a;
  497. return 0;
  498.  
  499. }
  500. int halt(unsigned un){
  501. fclose(inputs);
  502. exit(0);
  503. }
  504. int syscall(int r, int df){
  505. switch (df){
  506. case 0:
  507. {fclose(inputs);
  508. exit(reg[r]);}
  509. case 1:
  510. {int t = 1;
  511. int k = 0;
  512. char name[260];
  513. while (t){
  514. char c[4];
  515. c[0] = (char)((stek[reg[14]-1] » 24));
  516. c[1] = (char)((stek[reg[14]-1] » 16) & 255);
  517. c[2] = (char)((stek[reg[14]-1] » 8)& 255);
  518. c[3] = (char)((stek[reg[14]-1] & 255));
  519. reg[14]--;
  520. int i = 0;
  521. while (c[i]!= '\0'){
  522. name[k+i] = c[i];
  523. i++;
  524. }
  525. if (c[i]== '\0'){
  526. name[k+i] = c[i];
  527. t = 0;
  528. }
  529. k += 4;
  530. }
  531. int mode = stek[reg[14]-1];
  532. reg[14]--;
  533. char mod[3];
  534. if (mode == 0) {
  535. mod[0] = 'r';
  536. mod[1] = '\0';
  537. }
  538. else if (mode == 1) {
  539. mod[0] = 'w';
  540. mod[1] = '\0';
  541. }
  542. else if (mode == 2) {
  543. mod[0] = 'r';
  544. mod[1] = 'w';
  545. mod[2] = '\0';
  546. }
  547. FILE * file = fopen(name, mod);
  548. reg[r] = (int)file;}
  549. case 2:
  550. {int t = 1;
  551. int k = 0;
  552. char name[260];
  553. while (t){
  554. char c[4];
  555. c[0] = (char)((stek[reg[14]-1] » 24));
  556. c[1] = (char)((stek[reg[14]-1] » 16) & 255);
  557. c[2] = (char)((stek[reg[14]-1] » 8)& 255);
  558. c[3] = (char)((stek[reg[14]-1] & 255));
  559. reg[14]--;
  560. int i = 0;
  561. while (c[i]!= '\0'){
  562. name[k+i] = c[i];
  563. i++;
  564. }
  565. if (c[i]== '\0'){
  566. name[k+i] = c[i];
  567. t = 0;
  568. }
  569. k += 4;
  570. }
  571. void * buf = (void *)(reg[r]);
  572. int len = stek[reg[14]-1];
  573. reg[14]--;
  574. read(name, buf, len);}
  575. case 3:
  576. {int t = 1;
  577. int k = 0;
  578. char name[260];
  579. while (t){
  580. char c[4];
  581. c[0] = (char)((stek[reg[14]-1] » 24));
  582. c[1] = (char)((stek[reg[14]-1] » 16) & 255);
  583. c[2] = (char)((stek[reg[14]-1] » 8)& 255);
  584. c[3] = (char)((stek[reg[14]-1] & 255));
  585. reg[14]--;
  586. int i = 0;
  587. while (c[i]!= '\0'){
  588. name[k+i] = c[i];
  589. i++;
  590. }
  591. if (c[i]== '\0'){
  592. name[k+i] = c[i];
  593. t = 0;
  594. }
  595. k += 4;
  596. }
  597. void * buf = (void *)(reg[r]);
  598. int len = stek[reg[14]-1];
  599. reg[14]--;
  600. write(name, buf, len);}
  601. case 4:
  602. {int t = 1;
  603. int k = 0;
  604. char name[260];
  605. while (t){
  606. char c[4];
  607. c[0] = (char)((stek[reg[14]-1] »
  608. 24));
  609. c[1] = (char)((stek[reg[14]-1] » 16) & 255);
  610. c[2] = (char)((stek[reg[14]-1] » 8)& 255);
  611. c[3] = (char)((stek[reg[14]-1] & 255));
  612. reg[14]--;
  613. int i = 0;
  614. while (c[i]!= '\0'){
  615. name[k+i] = c[i];
  616. i++;
  617. }
  618. if (c[i]== '\0'){
  619. name[k+i] = c[i];
  620. t = 0;
  621. }
  622. k += 4;
  623. }
  624. void * buf = (void *)(reg[r]);
  625. int len = stek[reg[14]-1];
  626. reg[14]--;
  627. write(name, buf, len);}
  628. case 5: reg[r] = malloc(1, reg[r]);
  629. case 6: free(reg[r]);
  630. case 100: scanf("%d", &reg[r]);
  631. case 101: scanf("%d%d", &reg[r], &reg[r+1]);
  632. case 102: printf("%d", reg[r]);
  633. case 103: printf("%d%d", reg[r], reg[r+1]);
  634. case 104: reg[r] = getchar();
  635. case 105: putchar(char(reg[r]));
  636. return 0;
  637. }
  638. }
  639.  
  640. int add(int r, int l, int df){
  641. int var = reg[l] + df;
  642. reg[r] += var;
  643. return 0;
  644. }
  645. int addi(int a, int df){
  646. reg[r] += df;
  647. return 0;
  648. }
  649. int sub(int r, int l, int df){
  650. int var = reg[l] + df;
  651. reg[r] -= var;
  652. return 0;
  653. }
  654. int subi(int r, int df){
  655. reg[r] -= df;
  656. return 0;
  657. }
  658. int mul(int r, int l, int df){
  659. int first = reg[r];
  660. int second = reg[l]
  661. long long var = first * second;
  662. reg[r + 1] = int(var » 32);
  663. reg[r] = int(var & 4294967295);
  664. return 0;
  665. }
  666. int muli(int r, int df){
  667. int first = reg[r];
  668. long long var = first * df;
  669. reg[r + 1] = int(var » 32);
  670. reg[r] = int(var & 4294967295);
  671. return 0;
  672. }
  673. int div(int r, int l, int df){
  674. long long var = (reg[r+1] « 32) + reg[r];
  675. int del = reg[l]
  676. int ost = var % del;
  677. int res = var / del;
  678. reg[r] = int(res);
  679. reg[r + 1] = int(ost);
  680. return 0;
  681. }
  682. int divi(int r, int df){
  683. long long var = (reg[r+1] « 32) + reg[r];
  684. int del = df;
  685. int ost = var % del;
  686. int res = var / del;
  687. reg[r] = int(res);
  688. reg[r + 1] = int(ost);
  689. return 0;
  690. }
  691. int lc(int r, int df){
  692. reg[r] = df;
  693. return 0;
  694. }
  695. int shl(int r, int l, int df){
  696. reg[r] «= reg[l];
  697. return 0;
  698. }
  699. int shli(int r, int df){
  700. reg[r] «= df;
  701. return 0;
  702. }
  703. int shr(int r, int l, int df){
  704. reg[r] »= reg[l];
  705. return 0;
  706. }
  707. int shri(int r, int df){
  708. reg[r] »= df;
  709. return 0;
  710. }
  711. int and(int r, int l, int df){
  712. reg[r] &= reg[l];
  713. return 0;
  714. }
  715. int andi(int r, int df){
  716. reg[r] &= df;
  717. return 0;
  718. }
  719. int or(int r, int l, int df){
  720. reg[r] |= reg[l];
  721. return 0;
  722. }
  723. int ori(int r, int df){
  724. reg[r] |= df;
  725. return 0;
  726. }
  727. int xor(int r, int l, int df){
  728. reg[r] ^= reg[l];
  729. return 0;
  730. }
  731. int xori(int r, int df){
  732. reg[r] ^= df;
  733. return 0;
  734. }
  735. int not(int r, int df){
  736. reg[r] = !reg[r];
  737. return 0;
  738. }
  739. int mov(int r, int l, int df){
  740. int res = reg[l] + df;
  741. reg[r] = int(res);
  742. return 0;
  743. }
  744. int addd(int r, int l, int df){
  745. int r_deg = ((reg[r] » 21) & 1023);
  746. int l_deg = ((reg[r] » 21) & 1023);
  747. unsigned long r_l = unsigned long(reg[r]);
  748. unsigned long r_1 = unsigned long(reg[r + 1]);
  749. unsigned long l_1 = unsigned long(reg[l + 1]);
  750. unsigned long l_l = unsigned long(reg[l]);
  751. char r_sign = (reg[r] » 31);
  752. char l_sign = (reg[l] » 31);
  753. unsigned long long r_int = ((r_l & 2097151) « 32) + r_1 + (1 « 54);
  754. unsigned long long l_int = ((l_l & 2097151) « 32) + l_1 + (1 « 54);
  755. if (r_deg > l_deg){
  756. while (l_deg != r_deg){
  757. l_deg ++;
  758. l_int »= 1;
  759. if l_int == 0{
  760. return 0;
  761. }
  762. }
  763. }
  764. else {
  765. while (l_deg != r_deg){
  766. r_deg ++;
  767. r_int »= 1;
  768. if r_int == 0{
  769. if (((r_sign) ^ (l_sign)) == 1) {
  770. reg[r] = reg[l];
  771. reg[r + 1] = reg[l + 1];
  772. return 0;
  773. }
  774. else {
  775. reg[r] = reg[l] ^ (1 « 31);
  776. reg[r + 1] = reg[l + 1];
  777.  
  778. }
  779. }
  780. }
  781. }
  782. if (((r_sign) ^ (l_sign)) == 0) {
  783. r_int += l_int;
  784. }
  785. else {
  786. if (r_int > l_int){
  787. r_int -= l_int;
  788. }
  789. else {
  790. r_int = l_int - r_int;
  791. r_sign = l_sign
  792. }
  793. }
  794. r_deg += 10; //не уверен
  795. while ((r_int » 63)!=1){
  796. r_int « 1
  797. r_deg —;
  798. }
  799. r_int « 1;
  800. r_deg —;
  801.  
  802. reg[r]=int(r_sign « 31) + (r_deg « 21) + (((r_int & 2097151) » 43));
  803. reg[r + 1] =int(r_int » 11);
  804. return 0;
  805. }
  806. int subd(int r, int l, int df){ //не работает на разных знаках пока как и сложение.
  807. int r_deg = ((reg[r] » 21) & 1023);
  808. int l_deg = ((reg[r] » 21) & 1023);
  809. unsigned long r_l = unsigned long(reg[r]);
  810. unsigned long r_1 = unsigned long(reg[r + 1]);
  811. unsigned long l_1 = unsigned long(reg[l +
  812. 1]);
  813. unsigned long l_l = unsigned long(reg[l]);
  814. char r_sign = (reg[r] » 31);
  815. char l_sign = (reg[l] » 31);
  816. unsigned long long r_int = ((r_l & 2097151) « 32) + r_1 + (1 « 54);
  817. unsigned long long l_int = ((l_l & 2097151) « 32) + l_1 + (1 « 54);
  818. if (r_deg > l_deg){
  819. while (l_deg != r_deg){
  820. l_deg ++;
  821. l_int »= 1;
  822. if l_int == 0{
  823. return 0;
  824. }
  825. }
  826. }
  827. else {
  828. while (l_deg != r_deg){
  829. r_deg ++;
  830. r_int »= 1;
  831. if r_int == 0{
  832. if (((r_sign) ^ (l_sign)) == 1) {
  833. reg[r] = reg[l];
  834. reg[r + 1] = reg[l + 1];
  835. return 0;
  836. }
  837. else {
  838. reg[r] = reg[l] ^ (1 « 31);
  839. reg[r + 1] = reg[l + 1];
  840.  
  841. }
  842. }
  843. }
  844. }
  845. if (((r_sign) ^ (l_sign)) == 1) {
  846. r_int += l_int;
  847. }
  848. else {
  849. if (r_int > l_int){
  850. r_int -= l_int;
  851. }
  852. else {
  853. r_int = l_int - r_int;
  854. r_sign = l_sign
  855. }
  856. }
  857. r_deg += 10; //не уверен
  858. while ((r_int » 63)!=1){
  859. r_int « 1
  860. r_deg —;
  861. }
  862. r_int « 1;
  863. r_deg —;
  864.  
  865. reg[r]=int(r_sign « 31) + (r_deg « 21) + (((r_int & 2097151) » 43));
  866. reg[r + 1] =int(r_int » 11);
  867. return 0;
  868. }
  869. int muld(int r, int l, int df){
  870. int r_deg = ((reg[r] » 21) & 1023);
  871. int l_deg = ((reg[r] » 21) & 1023);
  872. char r_sign = (reg[r] » 31);
  873. char l_sign = (reg[l] » 31);
  874. int r_int = (reg[r] & 2097151) + (1 « 22);
  875. int r_int1 = reg[r + 1];
  876. int l_int = (reg[l] & 2097151) + (1 « 22);
  877. int l_int1 = reg[l + 1];
  878. unsigned long long res_1 = r_int * l_int;
  879. unsigned long long res_2 = r_int * l_int1;
  880. unsigned long long res_3 = r_int1 * l_int;
  881. unsigned long long res_4 = (r_int1 * l_int1) » 32;
  882. unsigned long long res_5 = res_4 + res_3;
  883. if (res_5 < res_4 || res_5 < res_3) int m = 1;
  884. else int m = 0;
  885. unsigned long long res_6 = res_5 + res_2;
  886. if (res_5 < res_4 || res_5 < res_3) int n = 1;
  887. else int n = 0;
  888. unsigned long long res_7 = (res_6 » 32)+ n + m + res_1;
  889. int f = 63
  890. while (((1 « f) & res_7) == 0){
  891. f--;
  892. }
  893. f--;
  894. r_sign = r_sign ^ l_sign;
  895. r_deg += l_deg;
  896. r_deg &= 1024;
  897. reg[r] = int((r_sign « 31) + (r_deg « 21) + (res_7 » (41 -f-64 + 2*f)));
  898. reg[r+1] = int(res_7 » (11 - f-64 + 2*f));
  899. return 0;
  900. }
  901. int divd(int r, int l, int df){
  902. int r_deg = ((reg[r] » 21) & 1023);
  903. int l_deg = ((reg[r] » 21) & 1023);
  904. unsigned long r_l = unsigned long(reg[r]);
  905. unsigned long r_1 = unsigned long(reg[r + 1]);
  906. unsigned long l_1 = unsigned long(reg[l + 1]);
  907. unsigned long l_l = unsigned long(reg[l]);
  908. char r_sign = (reg[r] » 31);
  909. char l_sign = (reg[l] » 31);
  910. unsigned long long r_int = ((r_l & 2097151) « 32) + r_1 + (1 « 54);
  911. unsigned long long l_int = ((l_l & 2097151) « 32) + l_1 + (1 « 54);
  912. unsigned long long res = r_int/l_int;
  913. r_int /= l_int;
  914. int f = 63
  915. while (((1 « f) & res) == 0){
  916. f--;
  917. }
  918. while (f < 52){
  919. f++;
  920. r_int « 1;
  921. res «= 1;
  922. r_deg -=1
  923. res +=(r_int/l_int);
  924. }
  925. if ((r_sign ^ l_sign)==1) r_sign =1;
  926. reg[r]=int((r_sign « 31) + (r_deg « 21) + (((r_int & 2097151) » 43)));
  927. reg[r + 1] =int(r_int » 11);
  928. }
  929.  
  930. int *stek = malloc(sizeof(int) * 1024);
  931. int *commandstek = malloc(sizeof(int) * 1024);
  932. int CommandStekNumber = 0;
  933.  
  934. int push(int r, int n){
  935. stek[reg[14]] = reg[r] + n;
  936. reg[14] ++;
  937. return 0;
  938. }
  939.  
  940. int pop(int r, int n){
  941. if (reg[14]<=0) return 1;
  942. reg[r] = stek[reg[14]-1] + n;
  943. reg[14];
  944. return 0;
  945. }
  946.  
  947. int kolya = 1;
  948.  
  949. int call(int r, int l, int n){
  950. kolya = 0;
  951. commandstek[CommandStekNumber] = reg[15];
  952. CommandStekNumber ++;
  953. reg[r] = reg[15];
  954. reg[15] = reg[l] + n;
  955. return 0;
  956. }
  957.  
  958. int calli(unsigned int n){
  959. kolya = 0;
  960. commandstek[CommandStekNumber] = reg[15];
  961. CommandStekNumber ++;
  962. reg[15] = n;
  963. return 0;
  964. }
  965.  
  966. int ret(unsigned int n){
  967. kolya = 0;
  968. reg[15] = commandstek[CommandStekNumber - 1] + n;
  969. return 0;
  970. }
  971.  
  972. // flags: reg[16]
  973. // 0 - флаг переноса (переполнение при сложении для беззнаковых);
  974. // 1 - флаг переполнения (для знаковых);
  975. // 2 - флаг четности (четное число битов равных 1);
  976. // 3 - флаг полупереноса (перенос из 3 бита в 4);
  977. // 4 - флаг нуля();
  978. // 5 - флаг знака (старший бит результата);
  979.  
  980.  
  981. int cmp(int r1, int r2, int n){
  982. reg[16] = 0;
  983. if (reg[r1] < reg[r2]){
  984. reg[16] += 16;
  985. }
  986. else if (reg[r1] == reg[r2]){
  987. reg[16] += 8;
  988. }
  989. return
  990. 0;
  991. }
  992.  
  993. int cmpi(int r; int n){
  994. reg[16] = 0;
  995. if (reg[r] < n){
  996. reg[16] += 16;
  997. }
  998. else if (reg[r] == n){
  999. reg[16] += 8;
  1000. }
  1001. return 0;
  1002. }
  1003.  
  1004. int cmpd(int r, int l, int df){
  1005. reg[16] = 0;
  1006. r< - 1 16
  1007. r = -1 8
  1008. int r_deg = ((reg[r] » 21) & 1023);
  1009. int l_deg = ((reg[r] » 21) & 1023);
  1010. unsigned long r_l = unsigned long(reg[r]);
  1011. unsigned long r_1 = unsigned long(reg[r + 1]);
  1012. unsigned long l_1 = unsigned long(reg[l + 1]);
  1013. unsigned long l_l = unsigned long(reg[l]);
  1014. char r_sign = (reg[r] » 31);
  1015. char l_sign = (reg[l] » 31);
  1016. unsigned long long r_int = ((r_l & 2097151) « 32) + r_1 + (1 « 54);
  1017. unsigned long long l_int = ((l_l & 2097151) « 32) + l_1 + (1 « 54);
  1018. if ((r_sign ^ l_sign_)== 1){
  1019. if (r_sign == 1){
  1020. reg[16] = 16;
  1021. }
  1022. }
  1023. else {
  1024. if (r_deg == l_deg){
  1025. if(r_int == l_int){
  1026. reg[17] = 8;
  1027. }
  1028. else if(((r_int < l_int) & (r_int == 1))||((r_int > l_int) & (r_int == 0))){
  1029. reg[17] = 16;
  1030. }
  1031. }
  1032. else if (((r_deg < l_deg) & (r_int == 1))||((r_deg > l_deg) & (r_int == 0))){
  1033. reg[17] = 16;
  1034. }
  1035. }
  1036. }
  1037. int jmp(unsigned int n){
  1038. kolya = 0;
  1039. reg[15] = n;
  1040. return 0;
  1041. }
  1042.  
  1043. int jne(unsigned int n){
  1044. if ((reg[16] & 8) == 0){
  1045. reg[15] = n;
  1046. kolya = 0;
  1047. }
  1048. return 0;
  1049. }
  1050.  
  1051. int jeq(unsigned int n){
  1052. if ((reg[16] & 8) == 8){
  1053. reg[15] = n;
  1054. kolya = 0;
  1055. }
  1056. return 0;
  1057. }
  1058.  
  1059. int jle(unsigned int n){
  1060. if (((reg[16] & 8) == 8) || ((reg[16] & 16) == 16)){
  1061. reg[15] = n;
  1062. kolya = 0;
  1063. }
  1064. return 0;
  1065. }
  1066.  
  1067. int jl(unsigned int n){
  1068. if (((reg[16] & 8) != 8) && ((reg[16] & 16) == 16)){
  1069. reg[15] = n;
  1070. kolya = 0;
  1071. }
  1072. return 0;
  1073. }
  1074.  
  1075. int jge(unsigned int n){
  1076. if (((reg[16] & 8) == 8) || ((reg[16] & 16) != 16)){
  1077. reg[15] = n;
  1078. kolya = 0;
  1079. }
  1080. return 0;
  1081. }
  1082.  
  1083. int jg(unsigned int n){
  1084. if (((reg[16] & 8) != 8) && ((reg[16] & 16) != 16)){
  1085. reg[15] = n;
  1086. kolya = 0;
  1087. }
  1088. return 0;
  1089. }
  1090.  
  1091. int *memory = malloc(sizeof(int) * 622144);
  1092. int size_of_memory;
  1093.  
  1094. int store(int r, unsigned n){
  1095. memory[n] = reg[r];
  1096. return 0;
  1097. }
  1098.  
  1099. int load (int r, unsigned n){
  1100. reg[r] = memory[n];
  1101. return 0;
  1102. }
  1103.  
  1104. int load2(int r, unsigned n){
  1105. reg[r] = memory[n];
  1106. reg[r+1] = memory[n+1];
  1107. return 0;
  1108. }
  1109.  
  1110. int store2(int r, unsigned n){
  1111. memory[n] = reg[r];
  1112. memory[n+1] = reg[r+1];
  1113. return 0;
  1114. }
  1115.  
  1116. int loadr(int r1, int r2, int n){
  1117. reg[r1] = memory[r2+n];
  1118. return 0;
  1119. }
  1120.  
  1121. int storer(int r1, int r2, int n){
  1122. memory[r2+n] = reg[r1];
  1123. return 0;
  1124. }
  1125.  
  1126. int loadr2(int r1, int r2, int n){
  1127. reg[r1] = memory[r2+n];
  1128. reg[r1+1] = memory[r2+n+1];
  1129. return 0;
  1130. }
  1131.  
  1132. int storer2(int r1, int r2, int n){
  1133. memory[r2+n] = reg[r1];
  1134. memory[r2+n+1] = reg[r1+1];
  1135. return 0;
  1136. }
  1137.  
  1138. int translitter(){
  1139. inputs = fopen("output.o", "r");
  1140. int a, b, c, d;
  1141. int i, start;
  1142. int r, l, df;
  1143. char[100] line;
  1144. unsigned un;
  1145. fseek(inputs, 28 ,SEEK_SET);
  1146. fscanf(inputs, "%d", start);
  1147. fseek(inputs, 512 + (start * 4), SEEK_SET);
  1148. while(fscanf(inputs,"%c%c%c%c", a, b, c, d) != EOF) {
  1149. kolya = 1
  1150. switch(d){
  1151. case HALT:
  1152. trans_bd(a, b, c, un);
  1153. halt(un);
  1154. break;
  1155. case SYSCALL:
  1156. trans_ri(a, b, c, r, df);
  1157. syscall(r, df);
  1158. break;
  1159. case ADD:
  1160. trans_rr(a, b, c, r, l, df);
  1161. add(r, l, df);
  1162. break;
  1163. case ADDI:
  1164. trans_ri(a, b, c, r, df);
  1165. addi(r, df);
  1166. break;
  1167. case SUB:
  1168. trans_rr(a, b, c, r, l, df);
  1169. sub(r, l, df);
  1170. break;
  1171. case SUBI:
  1172. trans_ri(a, b, c, r, df);
  1173. subi(r, df);
  1174. break;
  1175. case MUL:
  1176. trans_rr(a, b, c, r, l, df);
  1177. mul(r, l, df);
  1178. break;
  1179. case MULI:
  1180. trans_ri(a, b, c, r, df);
  1181. muli(r, df);
  1182. break;
  1183. case DIV:
  1184. trans_rr(a, b, c, r, l, df);
  1185. div(r, l, df);
  1186. break;
  1187. case DIVI:
  1188. trans_ri(a, b, c, r, df);
  1189. divi(r, df);
  1190. break;
  1191. case LC:
  1192. trans_ri(a, b, c, r, df);
  1193. lc(r, df);
  1194. break;
  1195. case SHL:
  1196. trans_rr(a, b, c, r, l, df);
  1197. shl(r, l, df);
  1198. break;
  1199. case SHLI:
  1200. trans_ri(a, b, c, r, df);
  1201. shli(r, df);
  1202. break;
  1203. case SHR:
  1204. trans_rr(a, b, c, r, l, df);
  1205. shr(r, l, df);
  1206. break;
  1207. case SHRI:
  1208. trans_ri(a, b, c, r, df);
  1209. shri(r, df);
  1210. break;
  1211. case AND:
  1212. trans_rr(a, b, c, r, l, df);
  1213. and(r, l, df);
  1214. break;
  1215. case ANDI:
  1216. trans_ri(a, b, c, r, df);
  1217. andi(r, df);
  1218. break;
  1219. case
  1220. OR:
  1221. trans_rr(a, b, c, r, l, df);
  1222. or(r, l, df);
  1223. break;
  1224. case ORI:
  1225. trans_ri(a, b, c, r, df);
  1226. ori(r, df);
  1227. break;
  1228. case XOR:
  1229. trans_rr(a, b, c, r, l, df);
  1230. xor(r, l, df);
  1231. break;
  1232. case XORI:
  1233. trans_ri(a, b, c, r, df);
  1234. xori(r, df);
  1235. break;
  1236. case NOT:
  1237. trans_ri(a, b, c, r, df);
  1238. not(r, df);
  1239. break;
  1240. case MOV:
  1241. trans_rr(a, b, c, r, l, df);
  1242. mov(r, l, df);
  1243. break;
  1244. case ADDD:
  1245. trans_rr(a, b, c, r, l, df);
  1246. addd(r, l, df);
  1247. break;
  1248. case SUBD:
  1249. trans_rr(a, b, c, r, l, df);
  1250. subd(r, l, df);
  1251. break;
  1252. case MULD:
  1253. trans_rr(a, b, c, r, l, df);
  1254. muld(r, l, df);
  1255. break;
  1256. case DIVD:
  1257. trans_rr(a, b, c, r, l, df);
  1258. divd(r, l, df);
  1259. break;
  1260. case ITOD:
  1261. trans_rr(a, b, c, r, l, df);
  1262. itod(r, l, df);
  1263. break;
  1264. case DTOI:
  1265. trans_rr(a, b, c, r, l, df);
  1266. dtoi(r, l, df);
  1267. break;
  1268. case PUSH:
  1269. trans_ri(a, b, c, r, df);
  1270. push(r, df);
  1271. break;
  1272. case POP:
  1273. trans_ri(a, b, c, r, df);
  1274. pop(r, df);
  1275. break;
  1276. case CALL:
  1277. trans_rr(a, b, c, r, l, df);
  1278. call(r, l, df);
  1279. break;
  1280. case CALLI:
  1281. trans_bd(a, b, c, un);
  1282. calli(un);
  1283. break;
  1284. case RET:
  1285. trans_bd(a, b, c, un);
  1286. ret(un);
  1287. break;
  1288. case CMP:
  1289. trans_rr(a, b, c, r, l, df);
  1290. cmp(r, l, df);
  1291. break;
  1292. case CMPI:
  1293. trans_ri(a, b, c, r, df);
  1294. cmpi(r, df);
  1295. break;
  1296. case CMPD:
  1297. trans_rr(a, b, c, r, l, df);
  1298. cmpd(r, l, df);
  1299. break;
  1300. case JMP:
  1301. trans_bd(a, b, c, un);
  1302. jmp(un);
  1303. break;
  1304. case JNE:
  1305. trans_bd(a, b, c, un);
  1306. jne(un);
  1307. break;
  1308. case JEQ:
  1309. trans_bd(a, b, c, un);
  1310. jeq(un);
  1311. break;
  1312. case JLE:
  1313. trans_bd(a, b, c, un);
  1314. jle(un);
  1315. break;
  1316. case JL:
  1317. trans_bd(a, b, c, un);
  1318. jl(un);
  1319. break;
  1320. case JGE:
  1321. trans_bd(a, b, c, un);
  1322. jge(un);
  1323. break;
  1324. case JG:
  1325. trans_bd(a, b, c, un);
  1326. jg(un);
  1327. break;
  1328. case LOAD:
  1329. trans_rm(a, b, c, r, un);
  1330. load(r, un);
  1331. break;
  1332. case STORE:
  1333. trans_rm(a, b, c, r, un);
  1334. store(r, un);
  1335. break;
  1336. case LOAD2:
  1337. trans_rm(a, b, c, r, un);
  1338. load2(r, un);
  1339. break;
  1340. case STORE2:
  1341. trans_rr(a, b, c, r, l, df);
  1342. store2(r, l, df);
  1343. break;
  1344. case LOADR:
  1345. trans_rr(a, b, c, r, l, df);
  1346. loadr(r, l, df);
  1347. break;
  1348. case LOADR2:
  1349. trans_rr(a, b, c, r, un);
  1350. loadr2(r, un);
  1351. break;
  1352. case STORER:
  1353. trans_rr(a, b, c, r, l, df);
  1354. storer(r, l, df);
  1355. break;
  1356. case STORER2:
  1357. trans_rr(a, b, c, r, l, df);
  1358. storer2(r, l, df);
  1359. break;
  1360. default:
  1361. trans_bd(a, b, c, un);
  1362. break;
  1363. }
  1364. fseek(inputs, kolya*4, SEEK_CUR);
  1365. }
  1366. return 0;
  1367. }
  1368. int main{
  1369. clearAsm();
  1370. assembler();
  1371. translitter();
  1372. return 0;
  1373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement