Refocus malloc on lisp node allocation, implement basic cons creation
This commit is contained in:
parent
34eb0a2bd6
commit
b080b4830b
32
cons.asm
Normal file
32
cons.asm
Normal 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
|
36
heap.asm
36
heap.asm
@ -2,7 +2,9 @@ verbose_malloc:
|
|||||||
call malloc
|
call malloc
|
||||||
push si ; both backup and input value for kprintf
|
push si ; both backup and input value for kprintf
|
||||||
mov si,.fmt
|
mov si,.fmt
|
||||||
|
push di
|
||||||
call kprintf
|
call kprintf
|
||||||
|
pop di
|
||||||
pop si
|
pop si
|
||||||
ret
|
ret
|
||||||
.fmt:
|
.fmt:
|
||||||
@ -11,42 +13,44 @@ verbose_malloc:
|
|||||||
; Allocates memory on the kernel heap
|
; Allocates memory on the kernel heap
|
||||||
; Strategy: smallest fit
|
; Strategy: smallest fit
|
||||||
; in cx size in bytes
|
; in cx size in bytes
|
||||||
; out si pointer
|
; dl node type
|
||||||
|
; out ds:di pointer
|
||||||
;
|
;
|
||||||
; Header:
|
; Header:
|
||||||
; word ptr to next item (this implies its size)
|
; word ptr to next item (this implies its size)
|
||||||
; byte 00 = free, >0 used (refcount)
|
; byte 00 = free, >0 used (refcount)
|
||||||
|
; byte high-level type
|
||||||
malloc:
|
malloc:
|
||||||
push ax
|
push ax
|
||||||
mov si,heap
|
push bx
|
||||||
|
mov di,heap
|
||||||
|
add cx, 0x04 ; header length
|
||||||
jmp .check
|
jmp .check
|
||||||
.next:
|
.next:
|
||||||
mov si,[si]
|
add di,[di]
|
||||||
.check:
|
.check:
|
||||||
mov ax,[si]
|
mov ax,[di]
|
||||||
test ax,0xFFFF
|
test ax,0xFFFF
|
||||||
jz .append ; we are on last entry
|
jz .append ; we are on last entry
|
||||||
cmp byte [si+2],0x00
|
cmp byte [di+2],0x00
|
||||||
jne .next ; entry is in use
|
jne .next ; entry is in use
|
||||||
sub ax,cx
|
sub ax,cx
|
||||||
je .reuse ; entry has exact length
|
je .reuse ; entry has exact length
|
||||||
jc .next ; entry too short
|
jc .next ; entry too short
|
||||||
jmp .next ; we'd have to split, not implemented yet
|
jmp .next ; we'd have to split, not implemented yet
|
||||||
.reuse: ; exact length
|
.reuse: ; exact length
|
||||||
mov byte [si+2],0xFF
|
mov byte [di+2],0xFF
|
||||||
jmp .end
|
jmp .end
|
||||||
.append: ; append to last item
|
.append: ; append to last item
|
||||||
mov ax, si
|
mov word [di], cx
|
||||||
add ax, 3
|
mov byte [di+2], 0x00
|
||||||
add ax, cx
|
mov byte [di+3], dl
|
||||||
mov word [si], ax
|
push di
|
||||||
mov byte [si+2], 0xFF
|
add word di, [di]
|
||||||
push si
|
mov byte [di],0x0000
|
||||||
mov word si,[si]
|
pop di
|
||||||
mov byte [si],0x0000
|
|
||||||
pop si
|
|
||||||
.end:
|
.end:
|
||||||
add si, 0x03
|
pop bx
|
||||||
pop ax
|
pop ax
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
9
int.asm
Normal file
9
int.asm
Normal 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
|
13
main.asm
13
main.asm
@ -48,10 +48,11 @@ main:
|
|||||||
|
|
||||||
call ivt_set
|
call ivt_set
|
||||||
|
|
||||||
mov cx, 0x0010
|
mov ax, 0
|
||||||
call verbose_malloc
|
mov bx, 0
|
||||||
call verbose_malloc
|
call cons
|
||||||
call verbose_malloc
|
mov si, di
|
||||||
|
call print
|
||||||
|
|
||||||
ret
|
ret
|
||||||
|
|
||||||
@ -60,5 +61,9 @@ main:
|
|||||||
%include "kprintf.asm"
|
%include "kprintf.asm"
|
||||||
%include "heap.asm"
|
%include "heap.asm"
|
||||||
|
|
||||||
|
%include "cons.asm"
|
||||||
|
%include "int.asm"
|
||||||
|
%include "print.asm"
|
||||||
|
|
||||||
heap:
|
heap:
|
||||||
dw 0
|
dw 0
|
||||||
|
25
print.asm
Normal file
25
print.asm
Normal 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
|
Loading…
Reference in New Issue
Block a user