Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5.  
  6. FILE *hex;
  7.  
  8. char *buffer;
  9.  
  10. char * decToHex(int num) {
  11.     char hex[100];
  12.     sprintf(hex, "%x", num);
  13.     char *cpy = malloc(3);
  14.     cpy[2] = 0;
  15.     int diff = 'A' - 'a';
  16.     if (num < 0) {
  17.         cpy[0] = hex[6] > 58 ? hex[6] + diff : hex[6];
  18.         cpy[1] = hex[7] > 58 ? hex[7] + diff : hex[7];
  19.     } else if (num == 0) {
  20.         cpy[0] = '0';
  21.         cpy[1] = '0';
  22.     } else if (hex[1] == 0) {
  23.         cpy[0] = '0';
  24.         cpy[1] = hex[0] > 58 ? hex[0] + diff : hex[0];
  25.     } else {
  26.         cpy[0] = hex[0] > 58 ? hex[0] + diff : hex[0];
  27.         cpy[1] = hex[1] > 58 ? hex[1] + diff : hex[1];
  28.     }
  29.     return cpy;
  30. }
  31.  
  32. void clearWhiteSpace(char **str) {
  33.     while (**str <= 32 || **str == ',') {
  34.         if (**str == 0)
  35.             return;
  36.         (*str)++;
  37.     }
  38. }
  39.  
  40. char *getNextLine() {
  41.     clearWhiteSpace(&buffer);
  42.     char *res = buffer;
  43.     if (*buffer == 0)
  44.         return 0;
  45.     while (*buffer != 10) {
  46.         buffer++;
  47.     }
  48.  // *buffer = 0;
  49.     buffer++;
  50.     return res;
  51. }
  52.  
  53. void nextNum(int *res, int *error, char **str) {
  54.     *res = 0;
  55.     *error = 0;
  56.     clearWhiteSpace(str);
  57.     if (!((**str >= '0' && **str <= '9') || **str == '-')) {
  58.         *error = 1;
  59.         return;
  60.     }
  61.     int neg = 1;
  62.     if (**str == '-') {
  63.         neg = -1;
  64.         (*str)++;
  65.     }
  66.     while (**str >= '0' && **str <= '9') {
  67.         *res = (*res) * 10 + (**str - '0');
  68.         (*str)++;
  69.     }
  70.     *res = (*res) * neg;
  71. }
  72.  
  73. char readReg(char **str) {
  74.     clearWhiteSpace(str);
  75.     (*str) += 1; //r
  76.     int num;
  77.     int error;
  78.     nextNum(&num, &error, str);
  79.     return *(decToHex(num) + 1);
  80. }
  81.  
  82. char * readImm(char **str) {
  83.     clearWhiteSpace(str);
  84.     int num;
  85.     int error;
  86.     nextNum(&num, &error, str);
  87.     return decToHex(num);
  88. }
  89.  
  90. void processText() {
  91.     char *str = getNextLine();
  92.     while (str) {
  93.         if (str[0] == 'm' && str[1] == 'o' && str[2] == 'v') { //movh and movl
  94.             if (str[3] == 'l') {
  95.                 fprintf(hex, "8");
  96.                 printf("8");
  97.                 str += 4;
  98.             } else {
  99.                 fprintf(hex, "9");
  100.                 printf("9");
  101.                 str += 4;
  102.             }
  103.             char reg = readReg(&str);
  104.             char * imm = readImm(&str);
  105.             fprintf(hex, "%c\n%c%c\n", imm[0], imm[1], reg);
  106.             printf("%c\n%c%c\n", imm[0], imm[1], reg);
  107.         } else if (str[0] == 'l' && str[1] == 'd') {        //ld
  108.             str += 2;
  109.             char target = readReg(&str);
  110.             char regA = readReg(&str);
  111.             fprintf(hex, "F%c\n0%c\n", regA, target);
  112.             printf("F%c\n0%c\n", regA, target);
  113.         } else if (str[0] == 's' && str[1] == 't') {        //st
  114.             str += 2;
  115.             char target = readReg(&str);
  116.             char regA = readReg(&str);
  117.             fprintf(hex, "F%c\n1%c\n", regA, target);
  118.             printf("F%c\n1%c\n", regA, target);
  119.         } else if (str[0] == 'j') {                         //jz, jnz, js, jns
  120.             char op;
  121.             if (str[1] == 'z') {
  122.                 str += 2;
  123.                 op = '0';
  124.             } else if (str[1] == 's') {
  125.                 str += 2;
  126.                 op = '2';
  127.             } else {
  128.                 if (str[2] == 'z') {
  129.                     op = '1';
  130.                 } else {
  131.                     op = '3';
  132.                 }
  133.                 str += 3;
  134.             }
  135.             char target = readReg(&str);
  136.             char regA = readReg(&str);
  137.             fprintf(hex, "E%c\n%c%c\n", regA, op, target);
  138.             printf("E%c\n%c%c\n", regA, op, target);
  139.  
  140.         } else {
  141.             str += 3;
  142.             char target = readReg(&str);
  143.             char regA = readReg(&str);
  144.             char regB = readReg(&str);
  145.             fprintf(hex, "0%c\n%c%c\n", regA, regB, target);
  146.             printf("0%c\n%c%c\n", regA, regB, target);
  147.         }
  148.  
  149.  
  150.         str = getNextLine();
  151.     }
  152.  
  153. }
  154.  
  155. int main(int argc, char **argv) {
  156.     if (argc != 2) {
  157.         fprintf(stderr,"usage: %s <name>\n",argv[0]);
  158.         exit(1);
  159.     }
  160.  
  161.     char* name = argv[1];
  162.     size_t len = strlen(name);
  163.  
  164.     size_t hexLen = len+5;  // ".hex" + 0
  165.     char* hexName = (char*) malloc(hexLen);
  166.     if (hexName == 0) {
  167.         perror("malloc");
  168.         exit(1);
  169.     } else {
  170.         strncpy(hexName,name,hexLen);
  171.         strncat(hexName,".hex",hexLen);
  172.     }
  173.  
  174.     size_t srcLen = len+3; // ".s" + 0
  175.     char* srcName = (char*) malloc(srcLen);
  176.     if (hexName == 0) {
  177.         perror("malloc");
  178.         exit(1);
  179.     }
  180.     strncpy(srcName,name,srcLen);
  181.     strncat(srcName,".s",srcLen);
  182.  
  183.     FILE * src = fopen (srcName, "rb");
  184.  
  185.     if (src) {
  186.         fseek (src, 0, SEEK_END);
  187.         long length = ftell (src);
  188.         fseek (src, 0, SEEK_SET);
  189.         buffer = malloc (length + 1);
  190.  
  191.         if (buffer) {
  192.             buffer[length] = '\0';
  193.             fread (buffer, 1, length, src);
  194.         }
  195.         fclose (src);
  196.     }
  197.     char * bufferfree = buffer;
  198.  
  199.     hex = fopen(hexName,"w");
  200.     if (hex == 0) {
  201.         perror(hexName);
  202.         exit(1);
  203.     }
  204.  
  205.     fprintf(hex, "@0\n");
  206.     printf("@0\n");
  207.     processText();
  208.  
  209.     fclose(hex);
  210.  
  211.     free(bufferfree);
  212.     free(srcName);
  213.     free(hexName);
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement