Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdbool.h>
- void filterDigit (char *s)
- {
- char *p = s;
- for (*p; *p; ++p)
- if (isdigit(*p))
- *s++ = *p;
- *s = '\0';
- }
- void decToBin (char *value, char* output)
- {
- int v, i;
- v = atoi(value);
- if (v > 8 | v < 0)
- {
- output[5] = '\0';
- for (i = 4; i >= 0; --i, v >>= 1)
- {
- output[i] = (v & 1) + '0';
- }
- } else {
- output[3] = '\0';
- for (i = 2; i >= 0; --i, v >>= 1)
- {
- output[i] = (v & 1) + '0';
- }
- }
- }
- void binToHex(const char *inStr, char *outStr) {
- static char hex[] = "0123456789abcdef";
- int len = strlen(inStr) / 4;
- int i = strlen(inStr) % 4;
- char current = 0;
- if(i)
- {
- while(i--)
- {
- current = (current << 1) + (*inStr - '0');
- inStr++;
- }
- *outStr = hex[current];
- ++outStr;
- }
- while(len--)
- {
- current = 0;
- for(i = 0; i < 4; ++i)
- {
- current = (current << 1) + (*inStr - '0');
- inStr++;
- }
- *outStr = hex[current];
- ++outStr;
- }
- *outStr = 0;}
- bool isInt(const char *str)
- {
- if (*str == '-')
- ++str;
- if (!*str)
- return false;
- while (*str)
- {
- if (!isdigit(*str))
- return false;
- else
- ++str;
- }
- return true;
- }
- int main(int argc, char *argv[]) {
- int immediateAdd = 1, pc = (int)strtol(argv[3], NULL, 16);
- if (argc != 4)
- {
- printf("Usage: %s filename\nor\nUsage: %s filename sTableName address\n", argv[0]);
- } else {
- FILE *file, *sTable;
- char opCode[4], srcDest[2], src1[2], src2[5], instCode[20], line[25], sTableSymbol[5], sTableValue[5];
- file = fopen(argv[1], "r");
- sTable = fopen(argv[2], "r");
- while (fgets(line, 25, file) != NULL)
- {
- sscanf(line, "%s %[^,],%[^,],%s", opCode, srcDest, &src1, &src2);
- pc++;
- if (strncmp(opCode, "br", 2) != 0)
- {
- filterDigit(srcDest);
- decToBin(srcDest, srcDest);
- }
- if (strcmp(opCode, "ld") != 0)
- {
- filterDigit(src1);
- decToBin(src1, src1);
- }
- if (!isInt(src2))
- {
- filterDigit(src2);
- immediateAdd = 0;
- }
- decToBin(src2, src2);
- if (strncmp(opCode, "br", 2) == 0)
- {
- while (fscanf(sTable, "%s %s", sTableSymbol, sTableValue) != EOF)
- {
- int n = 0, z = 0, p = 0, i;
- const char *nCheck = "n", *zCheck = "z", *pCheck = "p";
- char *opcCheck = opCode;
- while (*opcCheck)
- {
- if (strchr(nCheck, *opcCheck))
- {
- n = 1;
- }
- if (strchr(zCheck, *opcCheck))
- {
- z = 1;
- }
- if (strchr(pCheck, *opcCheck))
- {
- p = 1;
- }
- opcCheck++;
- }
- if (strncmp(srcDest, sTableSymbol, 1) == 0)
- {
- int offset = (int)strtol(sTableValue, NULL, 16), dif = 0;
- dif = offset - pc;
- int sta, en;
- int count = 0;
- char offsetCreate[9]= "";
- for(sta = 8; sta >= 0; sta--)
- {
- en = 1 << sta;
- if(en & dif)
- {
- offsetCreate[count] = '1';
- } else {
- offsetCreate[count] = '0';
- }
- count++;
- }
- offsetCreate[9] = 0;
- sprintf(instCode, "0000%d%d%d%s", n, z, p, offsetCreate);
- }
- }
- sTable = fopen(argv[2], "r");
- } else if (strcmp(opCode, "ld") == 0) {
- while (fscanf(sTable, "%s %s", sTableSymbol, sTableValue) != EOF)
- {
- if (strncmp(src1, sTableSymbol, 3) == 0)
- {
- int offset = (int)strtol(sTableValue, NULL, 16), dif = 0;
- dif = offset - pc;
- int sta, en;
- int count = 0;
- char offsetCreate[9]= "";
- for(sta = 8; sta >= 0; sta--)
- {
- en = 1 << sta;
- if(en & dif)
- {
- offsetCreate[count] = '1';
- } else {
- offsetCreate[count] = '0';
- }
- count++;
- }
- offsetCreate[9] = 0;
- sprintf(instCode, "0010%s%s", srcDest, offsetCreate);
- }
- }
- sTable = fopen(argv[2], "r");
- } else if (strcmp(opCode, "jmp") == 0) {
- sprintf(instCode, "1100000%s000000", srcDest);
- } else if (strcmp(opCode, "add") == 0) {
- if (immediateAdd == 0)
- {
- sprintf(instCode, "0001%s%s000%s", srcDest, src1, src2);
- } else {
- sprintf(instCode, "0001%s%s1%s", srcDest, src1, src2);
- }
- } else {
- if (immediateAdd == 0)
- {
- sprintf(instCode, "0101%s%s000%s", srcDest, src1, src2);
- } else {
- sprintf(instCode, "0101%s%s1%s", srcDest, src1, src2);
- }
- }
- immediateAdd = 1;
- binToHex(instCode, instCode);
- printf("%s\n", instCode);
- }
- fclose(file), fclose(sTable);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement