From b080b4830b7bdb6bd30d3d2720778de6d054e281 Mon Sep 17 00:00:00 2001 From: Ain <41307858+nero@users.noreply.github.com> Date: Mon, 18 Mar 2019 13:27:00 +0000 Subject: [PATCH] Refocus malloc on lisp node allocation, implement basic cons creation --- cons.asm | 32 ++++++++++++++++++++++++++++++++ heap.asm | 36 ++++++++++++++++++++---------------- int.asm | 9 +++++++++ main.asm | 13 +++++++++---- print.asm | 25 +++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 cons.asm create mode 100644 int.asm create mode 100644 print.asm diff --git a/cons.asm b/cons.asm new file mode 100644 index 0000000..50c1f62 --- /dev/null +++ b/cons.asm @@ -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 diff --git a/heap.asm b/heap.asm index f92e978..86c0fe0 100644 --- a/heap.asm +++ b/heap.asm @@ -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 diff --git a/int.asm b/int.asm new file mode 100644 index 0000000..379f726 --- /dev/null +++ b/int.asm @@ -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 diff --git a/main.asm b/main.asm index 4af2c0b..c6386ce 100644 --- a/main.asm +++ b/main.asm @@ -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 diff --git a/print.asm b/print.asm new file mode 100644 index 0000000..e0122b2 --- /dev/null +++ b/print.asm @@ -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