Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- FILE *hex;
- char *buffer;
- char * decToHex(int num) {
- char hex[100];
- sprintf(hex, "%x", num);
- char *cpy = malloc(3);
- cpy[2] = 0;
- int diff = 'A' - 'a';
- if (num < 0) {
- cpy[0] = hex[6] > 58 ? hex[6] + diff : hex[6];
- cpy[1] = hex[7] > 58 ? hex[7] + diff : hex[7];
- } else if (num == 0) {
- cpy[0] = '0';
- cpy[1] = '0';
- } else if (hex[1] == 0) {
- cpy[0] = '0';
- cpy[1] = hex[0] > 58 ? hex[0] + diff : hex[0];
- } else {
- cpy[0] = hex[0] > 58 ? hex[0] + diff : hex[0];
- cpy[1] = hex[1] > 58 ? hex[1] + diff : hex[1];
- }
- return cpy;
- }
- void clearWhiteSpace(char **str) {
- while (**str <= 32 || **str == ',') {
- if (**str == 0)
- return;
- (*str)++;
- }
- }
- char *getNextLine() {
- clearWhiteSpace(&buffer);
- char *res = buffer;
- if (*buffer == 0)
- return 0;
- while (*buffer != 10) {
- buffer++;
- }
- // *buffer = 0;
- buffer++;
- return res;
- }
- void nextNum(int *res, int *error, char **str) {
- *res = 0;
- *error = 0;
- clearWhiteSpace(str);
- if (!((**str >= '0' && **str <= '9') || **str == '-')) {
- *error = 1;
- return;
- }
- int neg = 1;
- if (**str == '-') {
- neg = -1;
- (*str)++;
- }
- while (**str >= '0' && **str <= '9') {
- *res = (*res) * 10 + (**str - '0');
- (*str)++;
- }
- *res = (*res) * neg;
- }
- char readReg(char **str) {
- clearWhiteSpace(str);
- (*str) += 1; //r
- int num;
- int error;
- nextNum(&num, &error, str);
- return *(decToHex(num) + 1);
- }
- char * readImm(char **str) {
- clearWhiteSpace(str);
- int num;
- int error;
- nextNum(&num, &error, str);
- return decToHex(num);
- }
- void processText() {
- char *str = getNextLine();
- while (str) {
- if (str[0] == 'm' && str[1] == 'o' && str[2] == 'v') { //movh and movl
- if (str[3] == 'l') {
- fprintf(hex, "8");
- printf("8");
- str += 4;
- } else {
- fprintf(hex, "9");
- printf("9");
- str += 4;
- }
- char reg = readReg(&str);
- char * imm = readImm(&str);
- fprintf(hex, "%c\n%c%c\n", imm[0], imm[1], reg);
- printf("%c\n%c%c\n", imm[0], imm[1], reg);
- } else if (str[0] == 'l' && str[1] == 'd') { //ld
- str += 2;
- char target = readReg(&str);
- char regA = readReg(&str);
- fprintf(hex, "F%c\n0%c\n", regA, target);
- printf("F%c\n0%c\n", regA, target);
- } else if (str[0] == 's' && str[1] == 't') { //st
- str += 2;
- char target = readReg(&str);
- char regA = readReg(&str);
- fprintf(hex, "F%c\n1%c\n", regA, target);
- printf("F%c\n1%c\n", regA, target);
- } else if (str[0] == 'j') { //jz, jnz, js, jns
- char op;
- if (str[1] == 'z') {
- str += 2;
- op = '0';
- } else if (str[1] == 's') {
- str += 2;
- op = '2';
- } else {
- if (str[2] == 'z') {
- op = '1';
- } else {
- op = '3';
- }
- str += 3;
- }
- char target = readReg(&str);
- char regA = readReg(&str);
- fprintf(hex, "E%c\n%c%c\n", regA, op, target);
- printf("E%c\n%c%c\n", regA, op, target);
- } else {
- str += 3;
- char target = readReg(&str);
- char regA = readReg(&str);
- char regB = readReg(&str);
- fprintf(hex, "0%c\n%c%c\n", regA, regB, target);
- printf("0%c\n%c%c\n", regA, regB, target);
- }
- str = getNextLine();
- }
- }
- int main(int argc, char **argv) {
- if (argc != 2) {
- fprintf(stderr,"usage: %s <name>\n",argv[0]);
- exit(1);
- }
- char* name = argv[1];
- size_t len = strlen(name);
- size_t hexLen = len+5; // ".hex" + 0
- char* hexName = (char*) malloc(hexLen);
- if (hexName == 0) {
- perror("malloc");
- exit(1);
- } else {
- strncpy(hexName,name,hexLen);
- strncat(hexName,".hex",hexLen);
- }
- size_t srcLen = len+3; // ".s" + 0
- char* srcName = (char*) malloc(srcLen);
- if (hexName == 0) {
- perror("malloc");
- exit(1);
- }
- strncpy(srcName,name,srcLen);
- strncat(srcName,".s",srcLen);
- FILE * src = fopen (srcName, "rb");
- if (src) {
- fseek (src, 0, SEEK_END);
- long length = ftell (src);
- fseek (src, 0, SEEK_SET);
- buffer = malloc (length + 1);
- if (buffer) {
- buffer[length] = '\0';
- fread (buffer, 1, length, src);
- }
- fclose (src);
- }
- char * bufferfree = buffer;
- hex = fopen(hexName,"w");
- if (hex == 0) {
- perror(hexName);
- exit(1);
- }
- fprintf(hex, "@0\n");
- printf("@0\n");
- processText();
- fclose(hex);
- free(bufferfree);
- free(srcName);
- free(hexName);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement