This commit is contained in:
Nero 2019-03-10 19:23:09 +00:00
commit 86b5b2a7aa
3 changed files with 258 additions and 0 deletions

140
kprintf.asm Normal file
View File

@ -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

13
main.asm Normal file
View File

@ -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"

105
mem.asm Normal file
View File

@ -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