2019-08-30 19:40:47 +02:00
|
|
|
; All these functions assume DS=0
|
|
|
|
|
2019-08-31 00:18:05 +02:00
|
|
|
malloc_reserve:
|
2019-08-30 23:48:39 +02:00
|
|
|
push bx
|
|
|
|
call malloc_mark_allocated
|
|
|
|
pop bx
|
|
|
|
add bx, 0x10
|
2019-08-31 00:18:05 +02:00
|
|
|
loop malloc_reserve
|
2019-08-30 23:48:39 +02:00
|
|
|
ret
|
|
|
|
|
2019-08-30 19:40:47 +02:00
|
|
|
malloc_reset:
|
|
|
|
xor ax, ax
|
|
|
|
mov es, ax
|
|
|
|
call malloc_get_table
|
|
|
|
mov cx, 0x100 ; 256 words
|
|
|
|
rep stosw
|
2019-08-30 23:48:39 +02:00
|
|
|
|
|
|
|
; reserve IVT and BDA
|
|
|
|
xor bx, bx
|
|
|
|
mov cx, 0x50
|
2019-08-31 00:18:05 +02:00
|
|
|
call malloc_reserve
|
2019-08-30 23:48:39 +02:00
|
|
|
|
|
|
|
; reserve malloc table itself
|
|
|
|
mov bx, 0x0600
|
|
|
|
mov cx, 0x20
|
2019-08-31 00:18:05 +02:00
|
|
|
call malloc_reserve
|
2019-08-30 23:48:39 +02:00
|
|
|
|
|
|
|
; reserve location for chainloaded boot sector
|
|
|
|
mov bx, 0x7C00
|
|
|
|
mov cx, 0x20
|
2019-08-31 00:18:05 +02:00
|
|
|
call malloc_reserve
|
2019-08-30 19:40:47 +02:00
|
|
|
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
|
|
|
|
|
2019-08-30 19:43:46 +02:00
|
|
|
; IN: 0:BX ptr to paragraph to be marked as allocated
|
2019-08-30 19:40:47 +02:00
|
|
|
malloc_mark_allocated:
|
|
|
|
call malloc_calc_offsets
|
|
|
|
or byte [bx+di], al
|
|
|
|
ret
|
|
|
|
|
2019-08-30 19:43:46 +02:00
|
|
|
; IN: 0:BX ptr to paragraph to be marked as free
|
2019-08-30 19:40:47 +02:00
|
|
|
malloc_mark_free:
|
|
|
|
call malloc_calc_offsets
|
|
|
|
not al
|
|
|
|
and byte [bx+di], al
|
|
|
|
ret
|
2019-08-31 00:18:05 +02:00
|
|
|
|
|
|
|
malloc_dump:
|
|
|
|
call malloc_get_table
|
|
|
|
mov si, di
|
|
|
|
mov cx, 0x200
|
|
|
|
xor dx, dx
|
|
|
|
.loop:
|
|
|
|
lodsb
|
|
|
|
call print8
|
|
|
|
xor ah, ah
|
|
|
|
call popcnt
|
|
|
|
add dx, ax
|
|
|
|
loop .loop
|
|
|
|
mov cl, 6
|
|
|
|
shr dx, cl
|
|
|
|
push dx
|
|
|
|
call printf
|
|
|
|
db " data %UkB", 0x0A, 0x0D, 0x00
|
|
|
|
pop dx
|
|
|
|
ret
|