diff --git a/asm-savers.h b/asm-savers.h new file mode 100644 index 0000000..b69683a --- /dev/null +++ b/asm-savers.h @@ -0,0 +1,122 @@ +#if (!defined(NTSYS__S_ASSEMBLER_H___)) +#define NTSYS__S_ASSEMBLER_H___ + +#define create_ntsys_asm_save(ss) ((ntsys_asm_save_t){(ss), (ss)}) +#define create_ntsys_asm_cache(ss) ((ntsys_asm_cache_t)&(ss)) +#define ntsys_asm_arg_new(str, __cache) ((ntsys_get_arg((__cache))) && ((str) = __cache->string)) + +#define __READ_FILE_WITH_EOF_OR_LN ((c = getc(fp)) != EOF && c != '\n' && c != '\r') +#define is_space(c) (c >= 0 && c <= 32) +#define __SPACE_SHIFT while (is_space(st[pos])) { pos ++; } +#define __NEXT_ARG {__e_byte_t isStr = 0;while (!is_space(st[save]) || isStr == 1) { if (st[save] == '"' && st[save - 1] != '\\') {isStr = !isStr;}save ++; }}while (is_space(st[save])) { save ++; }{size_t n_p = save;__e_byte_t isStr = 0;while (!is_space(st[n_p]) || isStr == 1) { if (st[n_p] == '"' && st[n_p - 1] != '\\') {isStr = !isStr;}n_p ++; }st[n_p] = '\0';} +#define get_mem_pos() *mem_pos = ftell(fp) == 0 ? 0 : ftell(fp) - 1; + +char* ntsys_get_str(char* ptr, FILE* fp) { + if (ptr != NULL) { + free(ptr); + } + unsigned long len = 0; + int c; + { + unsigned long save_ptr = ftell(fp); + while (__READ_FILE_WITH_EOF_OR_LN) { + len ++; + } + fseek(fp, save_ptr, SEEK_SET); + } + if (len == 0) return NULL; + ptr = malloc(len); + if (ptr == NULL) { + ntsys_error("Out of memory!"); + return NULL; + } + len = 0; + while (__READ_FILE_WITH_EOF_OR_LN) { + ptr[len] = c; + len ++; + } + ptr[len] = '\0'; + return ptr; +} + +typedef struct { + char* string; + char* cache; +} ntsys_asm_save_t; + +typedef ntsys_asm_save_t* ntsys_asm_cache_t; + +static void __str_slice(char* str) { + size_t i = 1; + while (i < strlen(str)) { + str[i - 1] = str[i]; + i ++; + } + str[strlen(str) - 1] = '\0'; +} + +char* ntsys_get_arg(ntsys_asm_cache_t save) { + char* str = save->cache; + while (1) { + if (!is_space(str[0]) || str[0] == '\0') { + break; + } + str ++; + } + { + size_t n_p = 0; + __e_byte_t isStr = 0; + while (1) { + if (str[n_p] == '"' && str[((n_p == 0) ? 1 : 0) - 1] != '\\') {isStr = !isStr;} + if ((is_space(str[n_p]) && isStr == 0) || str[n_p] == '\0') { + break; + } + n_p ++; + } + save->cache = &str[n_p + 1]; + str[n_p] = '\0'; + } + if (strlen(str) == 1 || strlen(str) == 0) { + str = NULL; + save->string = NULL; + return NULL; + } + if (str[0] == '"') { + str ++; + if (str[strlen(str) - 1] != '"' || str[(strlen(str) < 2 ? 2 : strlen(str)) - 2] == '\\') { + ntsys_error("No close of string!"); + str = NULL; + save->string = NULL; + return NULL; + } else { + str[strlen(str) - 1] = '\0'; + size_t i = 0; + while (i < strlen(str)) { + if (str[i] == '\\') { + __str_slice(&str[i]); + switch (str[i]) { + case '0': str[i] = '\0'; break; + case 'a': str[i] = '\a'; break; + case 'b': str[i] = '\b'; break; + case 't': str[i] = '\t'; break; + case 'n': str[i] = '\n'; break; + case 'v': str[i] = '\v'; break; + case 'f': str[i] = '\f'; break; + case 'r': str[i] = '\r'; break; + default: + ntsys_error("Unknown ident!"); + str = NULL; + save->string = NULL; + return NULL; + break; + } + } + i ++; + } + } + } + save->string = str; + return str; +} + +#endif \ No newline at end of file diff --git a/ntsys b/ntsys index b57339c..fc8c00a 100755 Binary files a/ntsys and b/ntsys differ diff --git a/ntsys-assembler.h b/ntsys-assembler.h index f8ec283..f393dcb 100644 --- a/ntsys-assembler.h +++ b/ntsys-assembler.h @@ -3,283 +3,14 @@ #define NTSYS_MAX_LABEL_LENGTH 32 -#define __READ_FILE_WITH_EOF_OR_LN ((c = getc(fp)) != EOF && c != '\n' && c != '\r') -#define is_space(c) (c >= 0 && c <= 32) -#define __SPACE_SHIFT while (is_space(st[pos])) { pos ++; } -#define __NEXT_ARG {__e_byte_t isStr = 0;while (!is_space(st[save]) || isStr == 1) { if (st[save] == '"' && st[save - 1] != '\\') {isStr = !isStr;}save ++; }}while (is_space(st[save])) { save ++; }{size_t n_p = save;__e_byte_t isStr = 0;while (!is_space(st[n_p]) || isStr == 1) { if (st[n_p] == '"' && st[n_p - 1] != '\\') {isStr = !isStr;}n_p ++; }st[n_p] = '\0';} -#define get_mem_pos() *mem_pos = ftell(fp) == 0 ? 0 : ftell(fp) - 1; +#include "asm-savers.h" -static char* get_str(char* ptr, FILE* fp) { - if (ptr != NULL) { - free(ptr); +int ntsys_asm_from_string(size_t* mempos, char* str, FILE* fp) { + ntsys_asm_save_t __save = create_ntsys_asm_save(str); + ntsys_asm_cache_t __cache = create_ntsys_asm_cache(__save); + while (ntsys_asm_arg_new(str, __cache)) { + puts(str); } - unsigned long len = 0; - int c; - { - unsigned long save_ptr = ftell(fp); - while (__READ_FILE_WITH_EOF_OR_LN) { - len ++; - } - fseek(fp, save_ptr, SEEK_SET); - } - if (len == 0) return NULL; - ptr = malloc(len); - if (ptr == NULL) { - ntsys_error("Out of memory!"); - return NULL; - } - len = 0; - while (__READ_FILE_WITH_EOF_OR_LN) { - ptr[len] = c; - len ++; - } - ptr[len] = '\0'; - return ptr; -} - -/* - * XX - шестнадцатиричное число - * :S - тип данных - * :D - сами данные (или метка) - * - * ------------------ Формат ------------------- - * - * имя_комманды=описание - * - */ - -char __fun_names_ntsys[][20] = { - "push=00:S:D", - "pop=01:S", - "push.string=02:D" -}; - -static size_t __index_of(char* arr, char c) { - size_t i = 0; - while (i < strlen(arr)) { - if (arr[i] == c) break; - i ++; - } - return i; -} - -word_t* shfdiyde_labels = NULL; -char** shdgsjgdsjjgds_names = NULL; -size_t sfdfshgshgfs_lb_pos = 0; -#define labels_names shdgsjgdsjjgds_names -#define labels shfdiyde_labels -#define lb_pos sfdfshgshgfs_lb_pos - -int ntsys_asm_from_string(size_t* mem_pos, char* st, FILE* fp) { - if (st == NULL) { - ntsys_error("Null Pointer Exception!"); - return EXIT_FAILURE; - } - size_t len = strlen(st); - size_t pos = 0; - __e_byte_t isStr = 0; - while (pos < len) { - size_t sP = pos; - if (pos == 0) sP = 1; - if (st[pos] == '"' && st[sP - 1] != '\\') { - isStr = !isStr; - } - if ((st[pos] == '#' || st[pos] == ';') && isStr == 0) { - st[pos] = '\0'; - break; - } - pos ++; - } - if (isStr == 1) { - ntsys_error("Find no close string!"); - return EXIT_FAILURE; - } - isStr = 0; - len = strlen(st); - pos = 0; - { - __SPACE_SHIFT; - size_t save = pos; - while (!is_space(st[pos])) { pos ++; } - st[pos] = '\0'; - size_t arr_len = sizeof(__fun_names_ntsys) / sizeof(__fun_names_ntsys[0]); - size_t i = 0; - while (i < arr_len) { - char eq[20]; - strcpy(eq, __fun_names_ntsys[i]); - size_t par_pos = 0; - { - size_t u = 0; - size_t d = strlen(__fun_names_ntsys[i]); - while (u < d) { - eq[u] = __fun_names_ntsys[i][u]; - if (eq[u] == '=') { - eq[u] = '\0'; - par_pos = u + 1; - break; - } - u ++; - } - } - char* h = (st + save); - if (h[strlen(st + save) - 1] == ':') { - get_mem_pos(); - h[strlen(st + save) - 1] = '\0'; - labels_names[lb_pos] = h; - labels[lb_pos++] = ((word_t)*mem_pos + 1); - break; - }; - if (strcmp((st + save), eq) == 0) { - size_t d = strlen(__fun_names_ntsys[i] + par_pos); - size_t p = 0; - __e_byte_t data_type = 1; - while (p < d) { - char* str = &__fun_names_ntsys[i][par_pos]; - if (str[p] == ':') { - p ++; - switch (str[p]) { - case 'D': - { - __NEXT_ARG; - __e_byte_t sys_cnt = 10; - __e_byte_t is_str = 0; - if (st[save] == '"') { - is_str = 1; - size_t f = 0; - save ++; - while (1) { - if (st[save + f] == '"' && st[save + f - 1] != '\\') { - putc('\0', fp); - break; - } - if (st[save + f] == '\\' && st[save + f - 1] != '\\') { f ++; continue; } - if (st[save + f - 1] == '\\') { - switch (st[save + f]) { - case '0': - putc('\0', fp); break; - case 'a': - putc('\a', fp); break; - case 'b': - putc('\b', fp); break; - case 't': - putc('\t', fp); break; - case 'n': - putc('\n', fp); break; - case 'v': - putc('\v', fp); break; - case 'f': - putc('\f', fp); break; - case 'r': - putc('\r', fp); break; - case '\\': - putc('\\', fp); break; - default: - ntsys_error("Unknown ident!"); - return EXIT_FAILURE; - } - } else { - putc(st[save + f], fp); - } - f ++; - } - } else if (st[save + 1] == 'x' && !is_space(st[save])) { - sys_cnt = 16; - } else if (st[save + 1] == 'b' && !is_space(st[save])) { - sys_cnt = 2; - } - if (is_str == 0) { - char* eq__ = &st[save]; - if (data_type == 0xCB) { - unsigned int __f = 0; - __e_byte_t is_stdfdf = 0; - while (__f < lb_pos) { - printf("Name \"%s\" equals test to \"%s\"\n", labels_names[__f], eq__); - if (strcmp(&labels_names[__f][0], eq__) == 0) { - fwrite((__e_byte_t*)((word_t*)&labels[__f]), 1, sizeof(word_t), fp); - is_stdfdf = 1; - break; - } - __f ++; - } - if (is_stdfdf == 0) { - ntsys_error("Invalid ident!"); - return EXIT_FAILURE; - } - } else { - if (sys_cnt != 10) { - save += 2; - } - char* arr = NULL; - long cnt = strtol(&st[save], &arr, sys_cnt); - switch (data_type) { - case 0x01: fwrite((__e_byte_t*)((byte_t*)&cnt), 1, sizeof(byte_t), fp); - break; - case 0x02: fwrite((__e_byte_t*)((word_t*)&cnt), 1, sizeof(word_t), fp); - break; - case 0x04: fwrite((__e_byte_t*)((dword_t*)&cnt), 1, sizeof(dword_t), fp); - break; - case 0x08: fwrite((__e_byte_t*)((qword_t*)&cnt), 1, sizeof(qword_t), fp); - break; - case 0xF1: fwrite((__e_byte_t*)((sbyte_t*)&cnt), 1, sizeof(sbyte_t), fp); - break; - case 0xF2: fwrite((__e_byte_t*)((sword_t*)&cnt), 1, sizeof(sword_t), fp); - break; - case 0xF4: fwrite((__e_byte_t*)((sdword_t*)&cnt), 1, sizeof(sdword_t), fp); - break; - case 0xF8: fwrite((__e_byte_t*)((sqword_t*)&cnt), 1, sizeof(sqword_t), fp); - break; - } - } - } - } - break; - case 'S': - { - __NEXT_ARG; - if (strcmp(&st[save], "byte") == 0) { - data_type = 0x01; - } else if (strcmp(&st[save], "word") == 0) { - data_type = 0x02; - } else if (strcmp(&st[save], "dword") == 0) { - data_type = 0x04; - } else if (strcmp(&st[save], "qword") == 0) { - data_type = 0x08; - } else if (strcmp(&st[save], "sbyte") == 0) { - data_type = 0xF1; - } else if (strcmp(&st[save], "sword") == 0) { - data_type = 0xF2; - } else if (strcmp(&st[save], "sdword") == 0) { - data_type = 0xF4; - } else if (strcmp(&st[save], "sqword") == 0) { - data_type = 0xF8; - } else if (strcmp(&st[save], "db") == 0) { - data_type = 0xCB; - } else { - ntsys_error("Incorrect datatype!"); - return EXIT_FAILURE; - } - if (data_type != 0xCB) { - fputc(data_type, fp); - } else { - fputc(0x02, fp); - } - } - break; - } - } else { - char* hx = "0123456789ABCDEF"; - __e_byte_t hex = (__index_of(hx, str[p + 1])) | (__index_of(hx, str[p]) << 4); - fputc(hex, fp); - p ++; - } - p ++; - } - break; - } - i ++; - } - } - get_mem_pos(); return EXIT_SUCCESS; } @@ -305,37 +36,13 @@ int ntsys_compile_asm(char* filename, char* asm_out) { } } rewind(fp); - labels = malloc(sizeof(word_t) * lb_size); - labels_names = (char**)malloc(sizeof(char*) * lb_size); - unsigned int count = 0; - while (count < lb_size) { - labels_names[count] = (char*)malloc(sizeof(char) * NTSYS_MAX_LABEL_LENGTH); - count ++; - } - while ((st = get_str(st, fp)) != NULL) { + while ((st = ntsys_get_str(st, fp)) != NULL) { if (ntsys_asm_from_string(&mem_pos, st, fw) != EXIT_SUCCESS) return EXIT_FAILURE; } - count = 0; - while (count < lb_size) { - free(labels_names[count]); - count ++; - } - free(labels_names); - free(labels); free(st); fclose(fp); fclose(fw); return EXIT_SUCCESS; } -#undef __READ_FILE_WITH_EOF_OR_LN -#undef __SPACE_SHIFT -#undef is_space -#undef __NEXT_ARG -#undef get_mem_pos -#undef labels -#undef labels_names -#undef lb_pos -#undef NTSYS_MAX_LABEL_LENGTH - #endif /* NTSYS_ASSEMBLER_H___ */ \ No newline at end of file diff --git a/out.exe b/out.exe index 56ae775..e69de29 100644 Binary files a/out.exe and b/out.exe differ