From 385bf1541422cf1eafc51af945bbb92287726f49 Mon Sep 17 00:00:00 2001 From: Ain <41307858+nero@users.noreply.github.com> Date: Fri, 30 Aug 2019 17:40:47 +0000 Subject: [PATCH] Add malloc --- kernel/main.asm | 6 +++++- kernel/malloc.asm | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 kernel/malloc.asm diff --git a/kernel/main.asm b/kernel/main.asm index f9ea6c7..12ad3af 100644 --- a/kernel/main.asm +++ b/kernel/main.asm @@ -98,4 +98,8 @@ start: %include "intr.asm" %include "debug.asm" -align 512 \ No newline at end of file +%include "malloc.asm" + +times 512 nop + +align 512 diff --git a/kernel/malloc.asm b/kernel/malloc.asm new file mode 100644 index 0000000..9496110 --- /dev/null +++ b/kernel/malloc.asm @@ -0,0 +1,54 @@ +; All these functions assume DS=0 + +malloc_reset: + xor ax, ax + mov es, ax + call malloc_get_table + mov cx, 0x100 ; 256 words + rep stosw + ret + +; Input: +; 0:BX - ptr to mem paragraph +; Output: +; AL - bitmask for byte in table +; BX - offset in table +malloc_calc_offsets: + ; ch is bit offset + ; cl is operand for bit shifts + push cx + + ; strip last 4 bits (offset within paragraph) + mov cl, 4 + shr bx, cl + + ; backup the next 3 bits of bx (wll be bit offset) + mov ch, bl + and ch, 0x07 + + ; calculate byte offset by shifting off 3 bits + mov cl, 3 + shr bx, cl + + ; setup bit mask in al, shifted by bit offset + mov al, 0x01 + mov cl, ch + shl al, cl + + pop cx + ; fallthrough to setup DI + +malloc_get_table: + mov di, 0x0600 + ret + +malloc_mark_allocated: + call malloc_calc_offsets + or byte [bx+di], al + ret + +malloc_mark_free: + call malloc_calc_offsets + not al + and byte [bx+di], al + ret