Refocus malloc on lisp node allocation, implement basic cons creation

This commit is contained in:
Nero 2019-03-18 13:27:00 +00:00
parent 34eb0a2bd6
commit b080b4830b
5 changed files with 95 additions and 20 deletions

32
cons.asm Normal file
View File

@ -0,0 +1,32 @@
; All ptrs in ds segment, pointing to a node as returned by malloc
; in ax ptr car
; bx ptr cdr
; out di ptr con
cons:
push cx ; length for malloc
push dx
mov cx,4
mov dl,0x01 ; type for cons
call malloc
pop dx
pop cx
mov word [di+0x04],ax
mov word [di+0x06],bx
ret
; in ax ptr con
; out di ptr car
car:
mov si,ax
; TODO: type check
mov di,[si+0x04]
ret
; in ax ptr con
; out di ptr cdr
cdr:
mov si,ax
; TODO: type check
mov di,[si+0x06]
ret

View File

@ -2,7 +2,9 @@ verbose_malloc:
call malloc
push si ; both backup and input value for kprintf
mov si,.fmt
push di
call kprintf
pop di
pop si
ret
.fmt:
@ -11,42 +13,44 @@ verbose_malloc:
; Allocates memory on the kernel heap
; Strategy: smallest fit
; in cx size in bytes
; out si pointer
; dl node type
; out ds:di pointer
;
; Header:
; word ptr to next item (this implies its size)
; byte 00 = free, >0 used (refcount)
; byte high-level type
malloc:
push ax
mov si,heap
push bx
mov di,heap
add cx, 0x04 ; header length
jmp .check
.next:
mov si,[si]
add di,[di]
.check:
mov ax,[si]
mov ax,[di]
test ax,0xFFFF
jz .append ; we are on last entry
cmp byte [si+2],0x00
cmp byte [di+2],0x00
jne .next ; entry is in use
sub ax,cx
je .reuse ; entry has exact length
jc .next ; entry too short
jmp .next ; we'd have to split, not implemented yet
.reuse: ; exact length
mov byte [si+2],0xFF
mov byte [di+2],0xFF
jmp .end
.append: ; append to last item
mov ax, si
add ax, 3
add ax, cx
mov word [si], ax
mov byte [si+2], 0xFF
push si
mov word si,[si]
mov byte [si],0x0000
pop si
mov word [di], cx
mov byte [di+2], 0x00
mov byte [di+3], dl
push di
add word di, [di]
mov byte [di],0x0000
pop di
.end:
add si, 0x03
pop bx
pop ax
ret

9
int.asm Normal file
View File

@ -0,0 +1,9 @@
; Create a integer atom from a string describing said integer
; in ds:si pointer to function arguments
; ds:di pointer to integer atom
int:
sub sp, 0x30
mov bp, sp ; buffer for integer
add sp, 0x30
ret

View File

@ -48,10 +48,11 @@ main:
call ivt_set
mov cx, 0x0010
call verbose_malloc
call verbose_malloc
call verbose_malloc
mov ax, 0
mov bx, 0
call cons
mov si, di
call print
ret
@ -60,5 +61,9 @@ main:
%include "kprintf.asm"
%include "heap.asm"
%include "cons.asm"
%include "int.asm"
%include "print.asm"
heap:
dw 0

25
print.asm Normal file
View File

@ -0,0 +1,25 @@
; dump contents of a node to screen
; in si
print:
push si ; byte iterator
push cx ; position of next item
push ax ; al is byte value
mov word cx, [si] ; fetch position of next item
cmp cx, 0x40
jng .loop
mov cx, 0x40
.loop:
lodsb
call kprint8
mov al, 0x20
call kputc
loopnz .loop
mov al, 0x0A
call kputc
mov al, 0x0D
call kputc
pop ax
pop cx
pop si
ret