diff --git a/main.asm b/main.asm index 32f7a51..b6debfe 100644 --- a/main.asm +++ b/main.asm @@ -4,10 +4,14 @@ org 0x0100 jmp main main: - call mm_print + mov ax,0x1000 + mov bx,0x0000 + mov cx,0x0800 + call mtab_set + call mtab_dump ret %include "kprintf.asm" -; mem MUST be last because it will write after it +; mem MUST be last because it will write after itself %include "mem.asm" diff --git a/mem.asm b/mem.asm index 4137a39..e01ef5e 100644 --- a/mem.asm +++ b/mem.asm @@ -5,79 +5,108 @@ ; 03 - reserved, like BIOS code or tables for the cpu ; 04 - unusable -; search memory table for a item matching a segment addr -; in: ax: segment -; out: bp: table start address -; si: table item offset -; ax: offset from begin of memory block -mm_findtab: - push bx - push cx +; return the addr of the mtab +; out: es:bx +mtab_ref: push cs - pop ds - mov cx,0x0000 - mov bp,memtab - mov si,0x0000 -.loop: - add cx,[bp+si+0x00] - cmp cx,ax - jnbe .end - mov bx,[bp+si+0x02] - mov si,bx - jmp .loop -.end: - sub cx,[bp+si+0x00] - sub ax,cx - pop cx - pop bx + pop es + mov bx,mtab ret ; finds a place for a new item in the mm table -; out: si -mm_taballoc: +; out: bx +mtab_alloc: push ax - mov bp,memtab - mov si,0x0000 + call mtab_ref .loop: - cmp WORD [bp+si],0x0000 + cmp WORD [es:bx],0x0000 je .end - mov ax,si - add ax,0x0010 - mov si,ax + add bx,0x0010 jmp .loop .end: pop ax ret -; force-sets the type of a memory block (dangerous) ; in: ax: segment -; bl: memory type -; cx: length in 16 byte blocks -mm_setup_range: - push dx - call mm_findtab ; i'll refer the item returned in si as 'base' - mov dx,[bp+si] ; get size from base - push si - ; duplicate the base - call mm_taballoc - mov di,si - pop si - +; bh: task id (0 for non-task memory) +; bl: memory type (see enum at top of file) +; cx: number of 16-byte blocks +mtab_set: push ds - pop es - push cx - mov cx,0x0010 - rep movsb ; copies 0x10 bytes from ES:SI to DS:DI + push cx ; length of current item + push dx ; cumulative position + push bx ; pointer to mtab item + + call mtab_ref + mov dx,0x0000 + push bx ; use as iterator, restoring base value afterwards +.findbase: ; we pick the mtab item where our segment lies in + mov cx,[es:bx] + add dx,cx + cmp dx,ax + jnbe .dupbase + add bx,0x0010 + jmp .findbase +.dupbase: ; we duplicate it, so we potentially have one after and one before our new item + mov si,bx + call mtab_alloc + mov di,bx + pop bx + + push si ; backup because movsw increments them + push di ; ^ + push cx ; counter for movsb + mov cx,0x08 ; a mtab item is 8 words = 16 bytes + push es + pop ds + rep movsw pop cx + pop di + pop si +.resizebase1: + push ax ; size = [Segment ax] - ([End addr dx] - [Length cx]) + sub ax,dx + add ax,cx + mov [si],ax + pop ax +.resizebase2: + push dx + sub dx,ax + mov [di],dx + pop dx +.insert: + push bx + call mtab_alloc + mov dx,bx + pop bx +.base1_link: + push ax + mov ax,dx + sub ax,bx ; addr are relative to mtab start + mov [si+02],ax + pop ax +.new_link: + push ax + mov ax,di + sub ax,bx ; addr are relative to mtab start + mov di,dx + mov [di+02],ax + pop ax + + pop bx + mov [di+04],bx pop dx + pop cx + + pop ds ret -mm_print: +mtab_dump: push cs pop ds mov cx,0x0000 - mov bp,memtab + mov bp,mtab mov si,0x0000 .loop: @@ -132,7 +161,7 @@ mm_print: ; pre-filled table for memory management align 16 -memtab: +mtab: ; interrupt vector table dw 0x0040 dw 0x0010