From b33125f0471d581101342911c658b3fbab029ee0 Mon Sep 17 00:00:00 2001 From: Yourtion Date: Tue, 3 May 2016 12:54:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8API=E6=98=BE=E7=A4=BA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20_day/Makefile | 8 +++++-- 20_day/bootpack.h | 5 +++- 20_day/console.c | 56 ++++++++++++++++++++++++++++++--------------- 20_day/dsctbl.c | 2 +- 20_day/hello.nas | 5 ++-- 20_day/hello2.nas | 8 +++++++ 20_day/naskfunc.nas | 17 ++++++-------- 7 files changed, 67 insertions(+), 34 deletions(-) create mode 100644 20_day/hello2.nas diff --git a/20_day/Makefile b/20_day/Makefile index aed58d4..502df1c 100644 --- a/20_day/Makefile +++ b/20_day/Makefile @@ -49,16 +49,20 @@ bootpack.hrb : bootpack.bim Makefile hello.hrb : hello.nas Makefile $(NASK) hello.nas hello.hrb hello.lst -haribote.sys : asmhead.bin bootpack.hrb hello.hrb Makefile +hello2.hrb : hello2.nas Makefile + $(NASK) hello2.nas hello2.hrb hello2.lst + +haribote.sys : asmhead.bin bootpack.hrb Makefile copy /B asmhead.bin+bootpack.hrb haribote.sys -haribote.img : ipl10.bin haribote.sys Makefile +haribote.img : ipl10.bin haribote.sys hello.hrb hello2.hrb Makefile $(EDIMG) imgin:../z_tools/fdimg0at.tek \ wbinimg src:ipl10.bin len:512 from:0 to:0 \ copy from:haribote.sys to:@: \ copy from:ipl10.nas to:@: \ copy from:make.bat to:@: \ copy from:hello.hrb to:@: \ + copy from:hello2.hrb to:@: \ imgout:haribote.img # 其他指令 diff --git a/20_day/bootpack.h b/20_day/bootpack.h index d350ba4..ad51fc8 100644 --- a/20_day/bootpack.h +++ b/20_day/bootpack.h @@ -31,7 +31,7 @@ void asm_inthandler2c(void); unsigned int memtest_sub(unsigned int start, unsigned int end); void farjmp(int eip, int cs); void farcall(int eip, int cs); -void asm_cons_putchar(void); +void asm_hrb_api(void); /* fifo.c */ struct FIFO32 { @@ -237,12 +237,15 @@ struct CONSOLE { void console_task(struct SHEET *sheet, unsigned int memtotal); void cons_putchar(struct CONSOLE *cons, int chr, char move); void cons_newline(struct CONSOLE *cons); +void cons_putstr0(struct CONSOLE *cons, char *s); +void cons_putstr1(struct CONSOLE *cons, char *s, int l); void cons_runcmd(char *cmdline, struct CONSOLE *cons, int *fat, unsigned int memtotal); void cmd_mem(struct CONSOLE *cons, unsigned int memtotal); void cmd_cls(struct CONSOLE *cons); void cmd_dir(struct CONSOLE *cons); void cmd_type(struct CONSOLE *cons, int *fat, char *cmdline); int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline); +void hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax); /* file.c */ struct FILEINFO { diff --git a/20_day/console.c b/20_day/console.c index 5a4112e..60a7103 100644 --- a/20_day/console.c +++ b/20_day/console.c @@ -147,6 +147,23 @@ void cons_newline(struct CONSOLE *cons) return; } +void cons_putstr0(struct CONSOLE *cons, char *s) +{ + for (; *s != 0; s++) { + cons_putchar(cons, *s, 1); + } + return; +} + +void cons_putstr1(struct CONSOLE *cons, char *s, int l) +{ + int i; + for (i = 0; i < l; i++) { + cons_putchar(cons, s[i], 1); + } + return; +} + void cons_runcmd(char *cmdline, struct CONSOLE *cons, int *fat, unsigned int memtotal) { if (strcmp(cmdline, "mem") == 0) { @@ -160,9 +177,7 @@ void cons_runcmd(char *cmdline, struct CONSOLE *cons, int *fat, unsigned int mem } else if (cmdline[0] != 0) { if (cmd_app(cons, fat, cmdline) == 0) { /*不是命令,不是应用程序,也不是空行*/ - putfonts8_asc_sht(cons->sht, 8, cons->cur_y, COL8_FFFFFF, COL8_000000, "Bad command.", 12); - cons_newline(cons); - cons_newline(cons); + cons_putstr0(cons, "Bad command.\n\n"); } } return; @@ -171,13 +186,9 @@ void cons_runcmd(char *cmdline, struct CONSOLE *cons, int *fat, unsigned int mem void cmd_mem(struct CONSOLE *cons, unsigned int memtotal) { struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR; - char s[30]; - sprintf(s, "total %dMB", memtotal / (1024 * 1024)); - putfonts8_asc_sht(cons->sht, 8, cons->cur_y, COL8_FFFFFF, COL8_000000, s, 30); - cons_newline(cons); sprintf(s, "free %dKB", memman_total(memman) / 1024); - putfonts8_asc_sht(cons->sht, 8, cons->cur_y, COL8_FFFFFF, COL8_000000, s, 30); - cons_newline(cons); - cons_newline(cons); + char s[60]; + sprintf(s, "total %dMB\nfree %dKB\n\n", memtotal / (1024 * 1024), memman_total(memman) / 1024); + cons_putstr0(cons, s); return; } @@ -206,15 +217,14 @@ void cmd_dir(struct CONSOLE *cons) } if (finfo[i].name[0] != 0xe5) { if ((finfo[i].type & 0x18) == 0) { - sprintf(s, "filename.ext %7d", finfo[i].size); + sprintf(s, "filename.ext %7d\n", finfo[i].size); for (j = 0; j < 8; j++) { s[j] = finfo[i].name[j]; } s[ 9] = finfo[i].ext[0]; s[10] = finfo[i].ext[1]; s[11] = finfo[i].ext[2]; - putfonts8_asc_sht(cons->sht, 8, cons->cur_y, COL8_FFFFFF, COL8_000000, s, 30); - cons_newline(cons); + cons_putstr0(cons, s); } } } @@ -232,14 +242,11 @@ void cmd_type(struct CONSOLE *cons, int *fat, char *cmdline) /*找到文件的情况*/ p = (char *) memman_alloc_4k(memman, finfo->size); file_loadfile(finfo->clustno, finfo->size, p, fat, (char *) (ADR_DISKIMG + 0x003e00)); - for (i = 0; i < finfo->size; i++) { - cons_putchar(cons, p[i], 1); - } + cons_putstr1(cons, p, finfo->size); memman_free_4k(memman, (int) p, finfo->size); } else { /*没有找到文件的情况*/ - putfonts8_asc_sht(cons->sht, 8, cons->cur_y, COL8_FFFFFF, COL8_000000, "File not found.", 15); - cons_newline(cons); + cons_putstr0(cons, "File not found.\n"); } cons_newline(cons); return; @@ -287,3 +294,16 @@ int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline) /*没有找到文件的情况*/ return 0; } + +void hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax) +{ + struct CONSOLE *cons = (struct CONSOLE *) *((int *) 0x0fec); + if (edx == 1) { + cons_putchar(cons, eax & 0xff, 1); + } else if (edx == 2) { + cons_putstr0(cons, (char *) ebx); + } else if (edx == 3) { + cons_putstr1(cons, (char *) ebx, ecx); + } + return; +} diff --git a/20_day/dsctbl.c b/20_day/dsctbl.c index 8714625..1887cde 100644 --- a/20_day/dsctbl.c +++ b/20_day/dsctbl.c @@ -27,7 +27,7 @@ void init_gdtidt(void) set_gatedesc(idt + 0x21, (int) asm_inthandler21, 2 * 8, AR_INTGATE32); set_gatedesc(idt + 0x27, (int) asm_inthandler27, 2 * 8, AR_INTGATE32); set_gatedesc(idt + 0x2c, (int) asm_inthandler2c, 2 * 8, AR_INTGATE32); - set_gatedesc(idt + 0x40, (int) asm_cons_putchar, 2 * 8, AR_INTGATE32); + set_gatedesc(idt + 0x40, (int) asm_hrb_api, 2 * 8, AR_INTGATE32); return; } diff --git a/20_day/hello.nas b/20_day/hello.nas index 8f75991..029ed22 100644 --- a/20_day/hello.nas +++ b/20_day/hello.nas @@ -1,6 +1,7 @@ [INSTRSET "i486p"] [BITS 32] MOV ECX,msg + MOV EDX,1 putloop: MOV AL,[CS:ECX] CMP AL,0 @@ -9,6 +10,6 @@ putloop: ADD ECX,1 JMP putloop fin: - RETF + RETF msg: - DB "hello",0 \ No newline at end of file + DB "hello",0 diff --git a/20_day/hello2.nas b/20_day/hello2.nas new file mode 100644 index 0000000..3550694 --- /dev/null +++ b/20_day/hello2.nas @@ -0,0 +1,8 @@ +[INSTRSET "i486p"] +[BITS 32] + MOV EDX,2 + MOV EBX,msg + INT 0x40 + RETF +msg: + DB "hello",0 diff --git a/20_day/naskfunc.nas b/20_day/naskfunc.nas index 38aa311..2feacd8 100644 --- a/20_day/naskfunc.nas +++ b/20_day/naskfunc.nas @@ -17,10 +17,10 @@ GLOBAL _asm_inthandler27, _asm_inthandler2c GLOBAL _memtest_sub GLOBAL _farjmp, _farcall - GLOBAL _asm_cons_putchar + GLOBAL _asm_hrb_api EXTERN _inthandler20, _inthandler21 EXTERN _inthandler27, _inthandler2c - EXTERN _cons_putchar + EXTERN _hrb_api [SECTION .text] @@ -217,14 +217,11 @@ _farcall: ; void farcall(int eip, int cs); CALL FAR [ESP+4] ; eip, cs RET -_asm_cons_putchar: +_asm_hrb_api: STI - PUSHAD - PUSH 1 - AND EAX,0xff ; 将AH和EAX的高位置0,将EAX置为已存入字符编码的状态 - PUSH EAX - PUSH DWORD [0x0fec] ; 读取内存并PUSH该值 - CALL _cons_putchar - ADD ESP,12 ; 将栈中的数据丢弃 + PUSHAD ; 用于保存寄存器值的PUSH + PUSHAD ; 用于向hrb_api传值的PUSH + CALL _hrb_api + ADD ESP,32 POPAD IRETD