The assembler compiler is implemented
This commit is contained in:
parent
d60ff6e26a
commit
baf2324588
41
asm-savers.h
41
asm-savers.h
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#define ntsys_exit_if_error(parser) if (!ntsys_asm_arg_new((parser)->string, (parser)->cache))
|
#define ntsys_exit_if_error(parser) if (!ntsys_asm_arg_new((parser)->string, (parser)->cache))
|
||||||
#define ntsys_run_asm_parser(ss) __ntsys_arg_function_loader(&(ss))
|
#define ntsys_run_asm_parser(ss) __ntsys_arg_function_loader(&(ss))
|
||||||
#define create_ntsys_asm_parser(cache, fp, str, table, table_size, labels, labels_pos, labels_count, cyc) ((ntsys_argument_parser_t){(cache), (fp), (str), (table), (table_size), (labels), (labels_pos), (labels_count), (cyc)})
|
#define create_ntsys_asm_parser(cache, fp, str, table, table_size, labels, labels_pos, labels_count, cyc, f_sz) ((ntsys_argument_parser_t){(cache), (fp), (str), (table), (table_size), (labels), (labels_pos), (labels_count), (cyc), (f_sz), NULL})
|
||||||
#define create_ntsys_asm_save(ss) ((ntsys_asm_save_t){(ss), (ss)})
|
#define create_ntsys_asm_save(ss) ((ntsys_asm_save_t){(ss), (ss)})
|
||||||
#define create_ntsys_asm_cache(ss) ((ntsys_asm_cache_t)&(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 ntsys_asm_arg_new(str, __cache) ((ntsys_get_arg((__cache))) && ((str) = __cache->string))
|
||||||
@ -147,6 +147,8 @@ typedef struct {
|
|||||||
word_t* labels_positions;
|
word_t* labels_positions;
|
||||||
size_t* labels_count;
|
size_t* labels_count;
|
||||||
byte_t is_rewrite;
|
byte_t is_rewrite;
|
||||||
|
dword_t* file_size;
|
||||||
|
word_t* memory_size;
|
||||||
} ntsys_argument_parser_t;
|
} ntsys_argument_parser_t;
|
||||||
|
|
||||||
static void __input_datatype(ntsys_argument_parser_t* parser, byte_t* mode) {
|
static void __input_datatype(ntsys_argument_parser_t* parser, byte_t* mode) {
|
||||||
@ -355,16 +357,45 @@ static int select_argument(ntsys_argument_parser_t* parser) {
|
|||||||
}
|
}
|
||||||
i ++;
|
i ++;
|
||||||
}
|
}
|
||||||
ntsys_error("Undefined operator!");
|
printf("\033[1mntsys: \033[91mUndefined operator \"%s\"!\033[0m\n", str);
|
||||||
/* WARNING! Обязательно заменить на EXIT_FAILURE! EXIT_SUCCESS нужен для отладки! */
|
return EXIT_FAILURE;
|
||||||
puts(str);
|
}
|
||||||
return EXIT_SUCCESS;
|
|
||||||
|
byte_t fGH6VSEzu7qNiGVE_hd_ew = 0;
|
||||||
|
|
||||||
|
static void __write_header(ntsys_argument_parser_t* parser) {
|
||||||
|
if (fGH6VSEzu7qNiGVE_hd_ew == 1) return;
|
||||||
|
fGH6VSEzu7qNiGVE_hd_ew = 1;
|
||||||
|
fputc(0x4D, parser->fp);
|
||||||
|
fputc(0x5A, parser->fp);
|
||||||
|
parser->memory_size = &fGH6VSEzu7qNiGVE_mem_size;
|
||||||
|
*parser->file_size += 8;
|
||||||
|
int __i = 1;
|
||||||
|
if (!(*((char *)&__i) == 1)) {
|
||||||
|
/* BIG ENDIAN */
|
||||||
|
size_t i = 0;
|
||||||
|
byte_t* ar = (byte_t*)parser->file_size;
|
||||||
|
while (i < (sizeof(dword_t))) {
|
||||||
|
byte_t s = ar[i];
|
||||||
|
ar[i] = ar[(sizeof(dword_t)) - i - 1];
|
||||||
|
ar[(sizeof(dword_t)) - i - 1] = s;
|
||||||
|
i ++;
|
||||||
|
}
|
||||||
|
ar = (byte_t*)parser->memory_size;
|
||||||
|
byte_t s = ar[0];
|
||||||
|
ar[0] = ar[1];
|
||||||
|
ar[1] = s;
|
||||||
|
}
|
||||||
|
fwrite(parser->file_size, 1, sizeof(dword_t), parser->fp);
|
||||||
|
fwrite(parser->memory_size, 1, sizeof(word_t), parser->fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
int __ntsys_arg_function_loader(ntsys_argument_parser_t* parser) {
|
int __ntsys_arg_function_loader(ntsys_argument_parser_t* parser) {
|
||||||
|
if (parser->is_rewrite) __write_header(parser);
|
||||||
while (ntsys_asm_arg_new(parser->string, parser->cache)) {
|
while (ntsys_asm_arg_new(parser->string, parser->cache)) {
|
||||||
if (select_argument(parser) == EXIT_FAILURE) return EXIT_FAILURE;
|
if (select_argument(parser) == EXIT_FAILURE) return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
if (parser->is_rewrite == 0) *parser->file_size = ftell(parser->fp) + 1;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,4 @@
|
|||||||
main: ; Метка main
|
main: ; Метка main
|
||||||
push.string "Hello, world!\n" ; Записываем строку
|
push.string "Hello, world!\n" ; Записываем строку
|
||||||
push byte 0x00 ; Запись номера SYSCALL
|
push byte 0x00 ; Запись номера SYSCALL
|
||||||
syscall ; Системный вызов
|
syscall ; Системный вызов
|
||||||
load: ; Цикл, здесь программа "зависнет"
|
|
||||||
push db load ; Адрес как db
|
|
||||||
jmp ; Выполняем
|
|
||||||
@ -12,7 +12,7 @@
|
|||||||
* XX - шестнадцатиричные данные
|
* XX - шестнадцатиричные данные
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const size_t ntsys_command_count = 3;
|
const size_t ntsys_command_count = 0x1B;
|
||||||
|
|
||||||
char* ntsys_asm_table[] = {
|
char* ntsys_asm_table[] = {
|
||||||
"push=00:T:D",
|
"push=00:T:D",
|
||||||
@ -44,12 +44,17 @@ char* ntsys_asm_table[] = {
|
|||||||
|
|
||||||
"mwrt=16:T",
|
"mwrt=16:T",
|
||||||
"mread=17:T",
|
"mread=17:T",
|
||||||
|
|
||||||
|
"getsp=18",
|
||||||
|
"setsp=19",
|
||||||
|
|
||||||
|
"syscall=1A"
|
||||||
};
|
};
|
||||||
|
|
||||||
int ntsys_asm_from_string(size_t* mempos, char* str, FILE* fp, byte_t cycle, ntsys_labels_array_t labels, word_t* labels_pos, size_t* labels_count) {
|
int ntsys_asm_from_string(size_t* mempos, char* str, FILE* fp, byte_t cycle, ntsys_labels_array_t labels, word_t* labels_pos, size_t* labels_count, dword_t* fsz) {
|
||||||
ntsys_asm_save_t __save = create_ntsys_asm_save(str);
|
ntsys_asm_save_t __save = create_ntsys_asm_save(str);
|
||||||
ntsys_asm_cache_t __cache = create_ntsys_asm_cache(__save);
|
ntsys_asm_cache_t __cache = create_ntsys_asm_cache(__save);
|
||||||
ntsys_argument_parser_t parser = create_ntsys_asm_parser(__cache, fp, str, ntsys_asm_table, ntsys_command_count, labels, labels_pos, labels_count, cycle);
|
ntsys_argument_parser_t parser = create_ntsys_asm_parser(__cache, fp, str, ntsys_asm_table, ntsys_command_count, labels, labels_pos, labels_count, cycle, fsz);
|
||||||
return ntsys_run_asm_parser(parser);
|
return ntsys_run_asm_parser(parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,8 +94,9 @@ int ntsys_compile_asm(char* filename, char* asm_out) {
|
|||||||
rewind(fp);
|
rewind(fp);
|
||||||
FILE* ff = tmp;
|
FILE* ff = tmp;
|
||||||
size_t labels_count = 0;
|
size_t labels_count = 0;
|
||||||
|
dword_t fsz = 0;
|
||||||
while ((st = ntsys_get_str(st, fp)) != NULL) {
|
while ((st = ntsys_get_str(st, fp)) != NULL) {
|
||||||
if (ntsys_asm_from_string(&mem_pos, st, tmp, 0, labels, labels_pos, &labels_count) != EXIT_SUCCESS) {
|
if (ntsys_asm_from_string(&mem_pos, st, tmp, 0, labels, labels_pos, &labels_count, &fsz) != EXIT_SUCCESS) {
|
||||||
free(st);
|
free(st);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
fclose(tmp);
|
fclose(tmp);
|
||||||
@ -109,7 +115,7 @@ int ntsys_compile_asm(char* filename, char* asm_out) {
|
|||||||
free(st);
|
free(st);
|
||||||
rewind(fp);
|
rewind(fp);
|
||||||
while ((st = ntsys_get_str(st, fp)) != NULL) {
|
while ((st = ntsys_get_str(st, fp)) != NULL) {
|
||||||
if (ntsys_asm_from_string(&mem_pos, st, ff, 1, labels, labels_pos, &labels_count) != EXIT_SUCCESS) {
|
if (ntsys_asm_from_string(&mem_pos, st, ff, 1, labels, labels_pos, &labels_count, &fsz) != EXIT_SUCCESS) {
|
||||||
free(st);
|
free(st);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
fclose(tmp);
|
fclose(tmp);
|
||||||
|
|||||||
13
ntsys.h
13
ntsys.h
@ -24,6 +24,8 @@
|
|||||||
#if (!defined(__LIB_ntsys_H_))
|
#if (!defined(__LIB_ntsys_H_))
|
||||||
#define __LIB_ntsys_H_
|
#define __LIB_ntsys_H_
|
||||||
|
|
||||||
|
#define NTSYS_ASSEMBLE_STANDARD_MEMORY_SIZE 4096
|
||||||
|
|
||||||
#define NTSYS_VERSION "1.0"
|
#define NTSYS_VERSION "1.0"
|
||||||
|
|
||||||
#define NAME_OF_32_BITS_TYPE unsigned int
|
#define NAME_OF_32_BITS_TYPE unsigned int
|
||||||
@ -57,12 +59,14 @@ size_t __ntsys_buffer_size = 0;
|
|||||||
unsigned int __ntsys_buffer_ptr = 0;
|
unsigned int __ntsys_buffer_ptr = 0;
|
||||||
int fGH6VSEzu7qNiGVE_stat = EXIT_SUCCESS;
|
int fGH6VSEzu7qNiGVE_stat = EXIT_SUCCESS;
|
||||||
|
|
||||||
|
|
||||||
void ntsys_error(char* _err) {
|
void ntsys_error(char* _err) {
|
||||||
printf("\033[1mntsys: \033[91m%s\033[0m\n", _err);
|
printf("\033[1mntsys: \033[91m%s\033[0m\n", _err);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "ntfiles.h"
|
#include "ntfiles.h"
|
||||||
|
|
||||||
|
word_t fGH6VSEzu7qNiGVE_mem_size = NTSYS_ASSEMBLE_STANDARD_MEMORY_SIZE;
|
||||||
|
|
||||||
#include "ntsys-assembler.h"
|
#include "ntsys-assembler.h"
|
||||||
|
|
||||||
static void __help(void) {
|
static void __help(void) {
|
||||||
@ -201,6 +205,7 @@ int _lib_ntsys_main(int argc, char** argv) {
|
|||||||
char mode = 'R';
|
char mode = 'R';
|
||||||
unsigned char out = 0;
|
unsigned char out = 0;
|
||||||
unsigned char debug = 0;
|
unsigned char debug = 0;
|
||||||
|
unsigned char mem = 0;
|
||||||
while (t < argc) {
|
while (t < argc) {
|
||||||
char* str = argv[t];
|
char* str = argv[t];
|
||||||
if (str[0] == '-') {
|
if (str[0] == '-') {
|
||||||
@ -223,6 +228,9 @@ int _lib_ntsys_main(int argc, char** argv) {
|
|||||||
out = 1;
|
out = 1;
|
||||||
mode = 'A';
|
mode = 'A';
|
||||||
break;
|
break;
|
||||||
|
case 'm':
|
||||||
|
mem = 1;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
ntsys_error("Unknown option!");
|
ntsys_error("Unknown option!");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
@ -231,6 +239,9 @@ int _lib_ntsys_main(int argc, char** argv) {
|
|||||||
if (out) {
|
if (out) {
|
||||||
asm_out = str;
|
asm_out = str;
|
||||||
out = 0;
|
out = 0;
|
||||||
|
} else if (mem) {
|
||||||
|
fGH6VSEzu7qNiGVE_mem_size = strtol(str, NULL, 10);
|
||||||
|
mem = 0;
|
||||||
} else {
|
} else {
|
||||||
filename = str;
|
filename = str;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user