ASM example hello_user.asm
This commit is contained in:
parent
62fef4bb93
commit
3d96c422c7
@ -99,3 +99,5 @@
|
|||||||
> ; Точка входа
|
> ; Точка входа
|
||||||
> ```
|
> ```
|
||||||
- **GLOBAL** - Ассемблерная инструкция, означает метку для старта.
|
- **GLOBAL** - Ассемблерная инструкция, означает метку для старта.
|
||||||
|
- **EXIT** - Ассемблерная инструкция, означает немедленное завершение программы.
|
||||||
|
- **RCALL** - Ассемблерная инструкция, означает вызов функции (```rcall тип имя```).
|
||||||
@ -5,6 +5,7 @@
|
|||||||
- ```--out / -o - set output file name```
|
- ```--out / -o - set output file name```
|
||||||
- ```--debug / -d - display debugging info```
|
- ```--debug / -d - display debugging info```
|
||||||
- ```--mem / -m - asm output memory size```
|
- ```--mem / -m - asm output memory size```
|
||||||
|
- ```--nfull / -n - no print all debug info```
|
||||||
## Опция HELP
|
## Опция HELP
|
||||||
Может быть вызвана как ```--help``` или ```-h```.
|
Может быть вызвана как ```--help``` или ```-h```.
|
||||||
Эта опция выводит краткую справку.
|
Эта опция выводит краткую справку.
|
||||||
@ -24,3 +25,6 @@
|
|||||||
Может быть вызвана как ```--mem``` или ```-m```.
|
Может быть вызвана как ```--mem``` или ```-m```.
|
||||||
Эта опция меняет размер памяти, выделяймой под программу при компиляции ассемблера.
|
Эта опция меняет размер памяти, выделяймой под программу при компиляции ассемблера.
|
||||||
Рекомендуется к использованию всегда.
|
Рекомендуется к использованию всегда.
|
||||||
|
## Опция NFULL
|
||||||
|
Может быть вызвана как ```--nfull``` или ```-n```.
|
||||||
|
Эта опция выключаеть вывод аддресов и запрос выполнения в [режиме отладки](#опция-debug).
|
||||||
98
hello_user.asm
Normal file
98
hello_user.asm
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
global db main ; Точка входа
|
||||||
|
|
||||||
|
readmem: ; Функция чтения из памяти
|
||||||
|
push dw 0x00 ; Адрес
|
||||||
|
mread dw ; Читаем
|
||||||
|
ret ; Конец функции
|
||||||
|
|
||||||
|
insub: ; Уменьшение адреса
|
||||||
|
push dw 1 ; На сколько уменьшать
|
||||||
|
push byte 2 ; Поменять два места
|
||||||
|
swap dw ; Тип данных - size_t
|
||||||
|
sub dw ; Уменьшить
|
||||||
|
ret ; Конец функции
|
||||||
|
|
||||||
|
writemem: ; Функция записи в память
|
||||||
|
push dw 0x00 ; Адрес
|
||||||
|
mwrt dw ; Записываем
|
||||||
|
ret ; Конец функции
|
||||||
|
|
||||||
|
decrement: ; Управление данными
|
||||||
|
rcall db readmem ; Прочитать
|
||||||
|
rcall db insub ; Уменьшить
|
||||||
|
rcall db writemem ; Записать
|
||||||
|
ret ; Конец функции
|
||||||
|
|
||||||
|
main: ; Точка входа
|
||||||
|
push.string "Введите ваше имя: " ; Строка
|
||||||
|
push byte 0x00 ; Дескриптор
|
||||||
|
syscall ; Выводим
|
||||||
|
|
||||||
|
begin: ; Настройка адресов
|
||||||
|
push dw 0x02 ; Указатель
|
||||||
|
push dw 0x00 ; Адрес
|
||||||
|
mwrt dw ; Записываем
|
||||||
|
|
||||||
|
inloop: ; Цикл чтения символов
|
||||||
|
push byte 0x01 ; Дескриптор
|
||||||
|
syscall ; Читаем
|
||||||
|
copy byte ; Копируем char
|
||||||
|
|
||||||
|
push dw 0x00 ; Адрес
|
||||||
|
mread dw ; Читаем
|
||||||
|
|
||||||
|
mwrt byte ; Записываем
|
||||||
|
|
||||||
|
push dw 0x00 ; Читаем
|
||||||
|
mread dw ; Указатель
|
||||||
|
|
||||||
|
push dw 0x01 ; Увеличить
|
||||||
|
add dw ; На 1
|
||||||
|
|
||||||
|
push dw 0x00 ; Адрес
|
||||||
|
mwrt word ; Сохраняем указатель
|
||||||
|
|
||||||
|
push byte 0x0A ; Символ переноса
|
||||||
|
push db inloop ; Цикл
|
||||||
|
push byte 2 ; Кол-во слов
|
||||||
|
swap word ; Меняем их местами
|
||||||
|
neq byte ; Если не \n - повторить цикл
|
||||||
|
|
||||||
|
userhello: ; Приветствие
|
||||||
|
push.string "Привет, " ; Текст
|
||||||
|
push byte 0x00 ; Дескриптор
|
||||||
|
syscall ; Выводим
|
||||||
|
rcall db decrement ; Уменьшаем
|
||||||
|
push byte 0x00 ; Символ конца строки
|
||||||
|
|
||||||
|
readcycle: ; Цикл чтения
|
||||||
|
push db writeusername ; Конец цикла
|
||||||
|
push dw 0x00 ; Адрес
|
||||||
|
mread dw ; Указатель
|
||||||
|
push dw 0x01 ; Число
|
||||||
|
eq dw ; Если равно - выйти
|
||||||
|
|
||||||
|
push db rdecrem ; Адрес
|
||||||
|
|
||||||
|
push dw 0x00 ; Иначе
|
||||||
|
mread dw ; Читаем указатель
|
||||||
|
mread byte ; Читаем байт по указателю
|
||||||
|
|
||||||
|
push byte 0x0A ; Если символ переноса
|
||||||
|
eq byte ; Пропускаем добавление на стек
|
||||||
|
|
||||||
|
push dw 0x00 ; Иначе
|
||||||
|
mread dw ; Читаем указатель
|
||||||
|
mread byte ; Читаем байт по указателю
|
||||||
|
|
||||||
|
rdecrem: ; Уменьшение указателя
|
||||||
|
rcall db decrement ; Уменьшаем
|
||||||
|
push db readcycle ; Указатель на цикл
|
||||||
|
jmp ; Повторить цикл
|
||||||
|
|
||||||
|
writeusername: ; Выводим имя
|
||||||
|
push byte 0x00 ; Дескриптор
|
||||||
|
syscall ; Выводим
|
||||||
|
push.string "!\n" ; Выводим !
|
||||||
|
push byte 0x00 ; Дескриптор
|
||||||
|
syscall ; Выводим
|
||||||
BIN
hello_user.exe
Normal file
BIN
hello_user.exe
Normal file
Binary file not shown.
@ -583,6 +583,7 @@ static byte_t get_pop_sp_byte(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void nt_fs_syscall(void) {
|
static void nt_fs_syscall(void) {
|
||||||
|
/* printf("\nStack point %d 0x%X\n", __ntsys_stack_pointer, __ntsys_stack_pointer); */
|
||||||
switch (get_pop_sp_byte()) {
|
switch (get_pop_sp_byte()) {
|
||||||
case 0x00:
|
case 0x00:
|
||||||
{
|
{
|
||||||
@ -600,7 +601,7 @@ static void nt_fs_syscall(void) {
|
|||||||
case 0x02:
|
case 0x02:
|
||||||
{
|
{
|
||||||
fGH6VSEzu7qNiGVE_stat = get_pop_sp_word();
|
fGH6VSEzu7qNiGVE_stat = get_pop_sp_word();
|
||||||
cnt = __ntsys_buffer_size - 1;
|
cnt = __ntsys_buffer_size;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 0x03:
|
case 0x03:
|
||||||
@ -623,6 +624,7 @@ static void nt_fs_syscall(void) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
/* printf("\nStack point %d 0x%X\n", __ntsys_stack_pointer, __ntsys_stack_pointer); */
|
||||||
}
|
}
|
||||||
|
|
||||||
static void stack_swap(void) {
|
static void stack_swap(void) {
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
* XX - шестнадцатиричные данные
|
* XX - шестнадцатиричные данные
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const size_t ntsys_command_count = 0x23;
|
const size_t ntsys_command_count = 0x26;
|
||||||
|
|
||||||
char* ntsys_asm_table[] = {
|
char* ntsys_asm_table[] = {
|
||||||
"push=00:T:D",
|
"push=00:T:D",
|
||||||
@ -52,11 +52,15 @@ char* ntsys_asm_table[] = {
|
|||||||
|
|
||||||
"swap=1B:T",
|
"swap=1B:T",
|
||||||
"dup=1C:T",
|
"dup=1C:T",
|
||||||
|
"copy=1C:T",
|
||||||
|
|
||||||
"set=1D",
|
"set=1D",
|
||||||
"mov=1E",
|
"mov=1E",
|
||||||
|
|
||||||
"global=00:T:D0D",
|
"global=00:T:D0D",
|
||||||
|
"exit=000200000001021A",
|
||||||
|
"rcall=00:T:D1F",
|
||||||
|
|
||||||
"call=1F",
|
"call=1F",
|
||||||
"ret=20",
|
"ret=20",
|
||||||
|
|
||||||
|
|||||||
23
ntsys.h
23
ntsys.h
@ -85,10 +85,11 @@ static void __help(void) {
|
|||||||
" " "--asm / -a - compile assembler program" "\n"
|
" " "--asm / -a - compile assembler program" "\n"
|
||||||
" " "--out / -o - set output file name" "\n"
|
" " "--out / -o - set output file name" "\n"
|
||||||
" " "--debug / -d - display debugging info" "\n"
|
" " "--debug / -d - display debugging info" "\n"
|
||||||
" " "--mem / -m - asm output memory size" "\0"
|
" " "--mem / -m - asm output memory size" "\n"
|
||||||
|
" " "--nfull / -n - no print all debug info" "\0"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
static int __exe_run(__e_byte_t* memory, unsigned int memorysize, unsigned long filesize, FILE* fp, __e_byte_t debug) {
|
static int __exe_run(__e_byte_t* memory, __e_byte_t fulldebug, unsigned int memorysize, unsigned long filesize, FILE* fp, __e_byte_t debug) {
|
||||||
__ntsys_stack_pointer = memorysize;
|
__ntsys_stack_pointer = memorysize;
|
||||||
__ntsys_stack = memory;
|
__ntsys_stack = memory;
|
||||||
__e_byte_t* buff = malloc(filesize - 8);
|
__e_byte_t* buff = malloc(filesize - 8);
|
||||||
@ -105,12 +106,14 @@ static int __exe_run(__e_byte_t* memory, unsigned int memorysize, unsigned long
|
|||||||
putchar('\n');
|
putchar('\n');
|
||||||
while (__ntsys_buffer_ptr < __ntsys_buffer_size) {
|
while (__ntsys_buffer_ptr < __ntsys_buffer_size) {
|
||||||
__e_byte_t cmd = buff[__ntsys_buffer_ptr];
|
__e_byte_t cmd = buff[__ntsys_buffer_ptr];
|
||||||
|
if (fulldebug) {
|
||||||
fprintf(stdout, "Command %d 0x%X\n"
|
fprintf(stdout, "Command %d 0x%X\n"
|
||||||
"Position %d 0x%X\n"
|
"Position %d 0x%X\n"
|
||||||
"Stack point %d 0x%X\n\n"
|
"Stack point %d 0x%X\n\n"
|
||||||
, cmd, cmd,
|
, cmd, cmd,
|
||||||
__ntsys_buffer_ptr, __ntsys_buffer_ptr,
|
__ntsys_buffer_ptr, __ntsys_buffer_ptr,
|
||||||
__ntsys_stack_pointer, __ntsys_stack_pointer);
|
__ntsys_stack_pointer, __ntsys_stack_pointer);
|
||||||
|
}
|
||||||
__ntsys_buffer_ptr ++;
|
__ntsys_buffer_ptr ++;
|
||||||
fn_fGH6VSEzu7qNiGVE[cmd]();
|
fn_fGH6VSEzu7qNiGVE[cmd]();
|
||||||
FILE* df = fopen("dump.bin", "wb");
|
FILE* df = fopen("dump.bin", "wb");
|
||||||
@ -120,16 +123,21 @@ static int __exe_run(__e_byte_t* memory, unsigned int memorysize, unsigned long
|
|||||||
}
|
}
|
||||||
fwrite(__ntsys_stack, 1, memorysize, df);
|
fwrite(__ntsys_stack, 1, memorysize, df);
|
||||||
fclose(df);
|
fclose(df);
|
||||||
|
if (fulldebug) {
|
||||||
puts("Press any key to continue...");
|
puts("Press any key to continue...");
|
||||||
getchar();
|
getchar();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (debug) {
|
||||||
|
printf("\nStack point %d 0x%X\n", __ntsys_stack_pointer, __ntsys_stack_pointer);
|
||||||
|
}
|
||||||
free(buff);
|
free(buff);
|
||||||
if (fGH6VSEzu7qNiGVE_stat != EXIT_SUCCESS) return fGH6VSEzu7qNiGVE_stat;
|
if (fGH6VSEzu7qNiGVE_stat != EXIT_SUCCESS) return fGH6VSEzu7qNiGVE_stat;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ntsys_api_call(char* filename, unsigned char debug) {
|
int ntsys_api_call(char* filename, unsigned char debug, unsigned char isFullDebug) {
|
||||||
|
|
||||||
/* File data */
|
/* File data */
|
||||||
|
|
||||||
@ -193,7 +201,7 @@ int ntsys_api_call(char* filename, unsigned char debug) {
|
|||||||
printf("File size %lu 0x%lX\n", filesize, filesize);
|
printf("File size %lu 0x%lX\n", filesize, filesize);
|
||||||
printf("Memory size %u 0x%X\n", mem_size, mem_size);
|
printf("Memory size %u 0x%X\n", mem_size, mem_size);
|
||||||
}
|
}
|
||||||
int stat = __exe_run(memory, mem_size, filesize, fp, debug);
|
int stat = __exe_run(memory, isFullDebug, mem_size, filesize, fp, debug);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
free(memory);
|
free(memory);
|
||||||
return stat;
|
return stat;
|
||||||
@ -206,6 +214,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 is_full_deb = 1;
|
||||||
unsigned char mem = 0;
|
unsigned char mem = 0;
|
||||||
while (t < argc) {
|
while (t < argc) {
|
||||||
char* str = argv[t];
|
char* str = argv[t];
|
||||||
@ -216,6 +225,10 @@ int _lib_ntsys_main(int argc, char** argv) {
|
|||||||
case 'd':
|
case 'd':
|
||||||
debug = 1;
|
debug = 1;
|
||||||
break;
|
break;
|
||||||
|
case 'n':
|
||||||
|
debug = 1;
|
||||||
|
is_full_deb = 0;
|
||||||
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
__help();
|
__help();
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
@ -254,7 +267,7 @@ int _lib_ntsys_main(int argc, char** argv) {
|
|||||||
ntsys_error("Please specify the file name!");
|
ntsys_error("Please specify the file name!");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
if (mode == 'R') return ntsys_api_call(filename, debug);
|
if (mode == 'R') return ntsys_api_call(filename, debug, is_full_deb);
|
||||||
return ntsys_compile_asm(filename, asm_out);
|
return ntsys_compile_asm(filename, asm_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user