From 86b5b2a7aac8458f7d86c19deb0590267c43772e Mon Sep 17 00:00:00 2001 From: Ain <41307858+nero@users.noreply.github.com> Date: Sun, 10 Mar 2019 19:23:09 +0000 Subject: [PATCH] PoC --- kprintf.asm | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.asm | 13 +++++ mem.asm | 105 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 258 insertions(+) create mode 100644 kprintf.asm create mode 100644 main.asm create mode 100644 mem.asm diff --git a/kprintf.asm b/kprintf.asm new file mode 100644 index 0000000..12a416a --- /dev/null +++ b/kprintf.asm @@ -0,0 +1,140 @@ +; important functions in this file: kprintf + +; write a character to kernel output +; in: al +kputc: + push ax ; dont destroy ah + push dx + mov ah,0x02 ; DOS putc + mov dl,al + int 0x21 + pop dx + pop ax + ret + +; write a string to kernel output +; in: ds:di +kputs: + push ax +.loop: + lodsb + cmp al,0x00 + je .end + call kputc + jmp .loop +.end: + pop ax + ret + +; prints a nibble in hex +; in: al +kprint4: + and al, 0x0F + add al, 0x30 + cmp al, 0x3a + jl kputc + add al, 0x07 + jmp kputc + +; print a byte +; in: al +kprint8: + push ax ; avoid destroying ah + aam 16 ; high nibble moved into ah + xchg ah,al ; high nibble first + call kprint4 + xchg ah,al + call kprint4 + pop ax + ret + +; print a word +; in: ax +kprint16: + xchg ah,al + call kprint8 + xchg ah,al + call kprint8 + ret + +; read a word from the stack, using bp as stack pointer +; in: bp +; out: ax, bp +kprintf_lodsw: + push ds ; work segment for lodsw + push si ; work pointer for lodsw + push ss ; data transfer to ds + pop ds + mov si,bp + lodsw + mov bp,si ; write back incremented value + pop si + pop ds + ret + +; print data from stack +; in: ds:si, ss:sp +kprintf: + push ax + push bp ; state variable for stack lodsw + push si ; return original pointer to caller + mov ax, sp + add ax, 0x08 + mov bp, ax +.loop: + lodsb + cmp al,0x00 + je .end + cmp al,0x25 ; '%' + je .fseq + call kputc + jmp .loop +.end: + pop si + pop bp + mov al,0x0A + call kputc + mov al,0x0D + call kputc + pop ax + ret +.fseq: + lodsb + cmp al,0x00 + je .end + cmp al,0x25 ; '%' + je .fmt_pc + cmp al,0x63 ; 'c' + je .fmt_c + cmp al,0x73 ; 's' + je .fmt_s + cmp al,0x78 ; 'x' + je .fmt_x + cmp al,0x58 ; 'X' + je .fmt_X + mov al,0x3F ; '?' + call kputc + jmp .loop +.fmt_pc: + mov al,0x25 + call kputc + jmp .loop +.fmt_c: + call kprintf_lodsw + call kputc + jmp .loop +.fmt_s: + push si + call kprintf_lodsw + mov si,ax + call kputs + pop si + jmp .loop +.fmt_x: + call kprintf_lodsw + call kprint8 + jmp .loop +.fmt_X: + call kprintf_lodsw + call kprint16 + jmp .loop diff --git a/main.asm b/main.asm new file mode 100644 index 0000000..32f7a51 --- /dev/null +++ b/main.asm @@ -0,0 +1,13 @@ +cpu 8086 + +org 0x0100 +jmp main + +main: + call mm_print + ret + +%include "kprintf.asm" + +; mem MUST be last because it will write after it +%include "mem.asm" diff --git a/mem.asm b/mem.asm new file mode 100644 index 0000000..bd179c1 --- /dev/null +++ b/mem.asm @@ -0,0 +1,105 @@ +; converts a memory block type code into char representation +; in: al +; out: al +; destroys ah +mm_type: + push bp + push si + mov bp,.typechar + and ax,0x00FF + mov si,ax + mov al,[bp+si] + pop si + pop bp + ret +.typechar: + db "F?ARU", 0 + +mm_print: + push cs + pop ds + mov cx,0x0000 + mov bp,memtab + mov si,0x0000 + +.loop: + push si + + mov ax, bp + add ax, si + add ax, 0x06 + push ax + + mov al,[bp+si+0x05] + push ax ; task id + + mov al,[bp+si+0x04] + call mm_type + push ax ; memory type + + mov bx,cx + add cx,[bp+si+0x00] + mov ax,cx + dec ax + push ax ; end addr + push bx ; start addr + + mov si,.linefmt + call kprintf + + pop ax + pop ax + pop ax + pop ax + pop ax + pop si + + ; look for next line + mov ax,[bp+si+0x02] + test ax,0xFFFF + mov si,ax + jnz .loop + ret +.linefmt: + db "%X0 %XF %c %x %s", 0 + +; pre-filled table for memory management +align 16 +memtab: + ; interrupt vector table + dw 0x0040 + dw 0x0010 + db 3 + db 0 + db "IVT", 0 +align 16 + ; bios data area (writes here during runtime) + dw 0x0010 + dw 0x0020 + db 3 + db 0 + db "BIOS DATA", 0 +align 16 + dw 0x9FB0 + dw 0x0030 + db 1 + db 0 + db 0 +align 16 + dw 0x2000 + dw 0x0040 + db 4 + db 0 + db "I/O BUS", 0 +align 16 + dw 0x3000 + dw 0x0050 + db 4 + db 0 + db "BIOS EXT", 0 +align 16 + dw 0x1000 + dw 0x0000 + db 4 + db 0 + db "BIOS", 0