Move kernel libs into subdir, create NBP-specific entrypoint

This commit is contained in:
Nero 2019-03-23 20:17:47 +00:00
parent c2e68a217e
commit 260db9f8d7
7 changed files with 45 additions and 19 deletions

34
kernel/debug.asm Normal file
View file

@ -0,0 +1,34 @@
; this kprints registers
; expect to be called as interrupt routine
debug_reg_ir:
push ss
push es
push ds
push di
push si
push bp
push sp
push dx
push cx
push bx
push ax
mov ax,cs
mov ds,ax
mov si,.fmt
call kprintf
pop ax
pop bx
pop cx
pop dx
pop sp
pop bp
pop si
pop di
pop ds
pop es
pop ss
iret
.fmt:
db "AX=%X BX=%X CX=%X DX=%X SP=%X BP=%X SI=%X DI=%X", 0x0A, 0x0D, "DS=%X ES=%X SS=%X IP=%X CS=%X FL=%X", 0

28
kernel/heap.asm Normal file
View file

@ -0,0 +1,28 @@
heap_size:
dw 0x0100
heap_init:
push ax ; byte value (0)
push cx ; loop counter
push di ; target ptr
xor ax, ax
mov cx, [heap_size]
mov di, heap
rep stosb ; rep makes this loop cx times, incrementing di, writing al
pop di
pop cx
pop ax
ret
; in ax number of bytes to alloc
; out ds:di
malloc:
push dx ; length of block index
or ax,0x0F
inc ax
mov dx, 0x100
mov bp, heap
int 0x2E
pop dx
ret

19
kernel/intr.asm Normal file
View file

@ -0,0 +1,19 @@
; set item in interrupt vector table
; in: bx interrupt number
; ds:dx new handler address
ivt_set:
push es
push ax
xor ax,ax
mov es,ax
sal bx,1
sal bx,1
mov [es:bx], dx
mov [es:bx+2], ds
pop ax
pop es
ret

140
kernel/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
push bx
mov bx, 0x0000
mov ah, 0x0e
int 0x10
pop bx
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