Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

memory management

octavio2octavio2 Member Posts: 16
;autor: Octavio Vega 933191278@terra.es
;16 bits memory management routines
;use a86 to assemble this code
;these routines expect a number of bytes in CX and or a ptr in DI (pointers are not checked)
;and return a new ptr. in DI
;is asumed that ds=ss and df=0
;ncr=preserve registers
ret ; put here your program
minimstack equ 256 ;memory not used by alloca or malloc

free: ;di=valid ptr from malloc or alloca
;puntero en di
;no se comprueban los punteros
;retorna di=0 , ncr
pusha
pop di
mov dx,[di-2]
dec dx
and dx,-2 ;redondeo a 2,dx=dx-2
cmp di,sp
jc >l1
add di,dx
push di
popa
pop [di] ;ret
mov sp,di
xor di,di
ret
pusha
pop di
l1:
mov bp,2
push 0
mov si,iniciomemoria+2
;buscar puntero libre anterior
l3:
mov bx,si
lodsw
add si,ax
cmp si,di
jc l3
;enlazar
mov cx,si
sub cx,di
sub ax,cx
sub cx,bp
mov [bx],ax
;fusionar con el anterior
cmp ax,[bx-2]
jne >l1
cmp bx,iniciomemoria+2 ;no unir con el primero
je >l1
mov di,bx
add cx,ax
add dx,ax
add cx,bp
add dx,bp
l1:
;fusionar con bloques de tamao 0
mov bx,dx
l1:
add bx,bp
test [w di+bx],-1
jz l1
cmp bx,cx
jne >l1
;fusionar con el siguiente
add bx,[si-2]
add cx,[si]
add bx,bp
add cx,bp
cmp si,[iniciomemoria+4]
jne >l1
mov [iniciomemoria+4],di
l1:
mov [di-2],bx
mov [di],cx
popa
ret

realloc: ;puntero en di ncr retorna nuevo puntero en di
test di,di
jz malloc
jcxz free
pusha
pop si
mov ax,[di-2] ;msize
call malloc
push di
cmp cx,ax
jc >l1
mov cx,ax
l1:
push si
call memmove
pop di
call free
popa
ret

alloca: ; cx ret di ncr
mov [tmp+0],ax
mov ax,cx
inc ax
and ax,-2 ; redondeo a 2
jz >l2
mov di,sp
sub di,minimstack
sub di,ax
jc >l2
sub di,[iniciomemoria+4]
jc >l3
pop di
sub sp,ax
push cx
push di
mov di,sp
add di,4
mov ax,[tmp+0]
l1:
ret
l3:
mov ax,[tmp+0]
jmp malloc

l4: ;MEMORIA INSUFICIENTE
popa
l2:
xor di,di ;null ptr
ret

mresize: ; puntero en di ncr retorna nuevo puntero en di
test di,di
jz malloc
cmp cx,[di-2]
je l1 ;ret
call free
malloc: ; nbytes en cx ncr retorna nuevo puntero en di
mov di,cx
jcxz l1
pusha
mov bx,cx
inc bx
mov si,offset iniciomemoria+2
and bx,-2 ;redondeo a 2
l3:
mov di,si
add si,[si]
jc l4
lodsw
cmp ax,bx
jc l3
;encontrado
cmp si,[iniciomemoria+4]
jne >l3
mov cx,sp
lea dx,[bx+si+2]
sub cx,minimstack
cmp cx,dx
jc l4
mov [iniciomemoria+4],dx
l3:
pop [si-2]
push si
sub ax,bx
je >l8
sub ax,2
mov [si+bx],ax
jz >l8
l5: ;fragmentar
add bx,2
add [di],bx
mov ax,[si]
sub ax,bx
mov [si+bx],ax
popa
ret
l8:
lodsw
add ax,2
add [di],ax
popa
ret

checkmem: ;busqueda de errores en la memoria
;retorna dx nbloques o 0 si error,cx memoria libre
;ncr: bp,bx,ax
mov di,iniciomemoria ;bloques de memoria libres
mov si,di
xor dx,dx
mov cx,[di] ;el primer bloque no cuenta
neg cx
l2:
add cx,[di]
add di,2
add di,[di]
l3:
add si,[si]
inc dx
jc >l4
add si,3
and si,-2 ;r
jz >l5
cmp si,di
jc l3
je l2
;ERROR
l4:
xor dx,dx
stc
ret
l5:
test di,di
jnz l4
add cx,sp ;restar la memoria ocupada por el stack
sub cx,minimstack
clc
ret

memmove: ;ds:si ds:di cx cr: si,di,cx
push ax
mov es,ds
cmp si,di
jnc >l1
mov ax,cx
add ax,si
cmp di,ax
jnc >l1
std
dec ax
dec di
xchg ax,si
add di,cx
rep movsb
cld
pop ax
ret

movedata:
push ax
L1:
xor ax,ax
sub ax,di
and ax,3
xchg cx,ax
sub ax,cx
jle >l1
rep movsb
mov cx,ax
shr cx,2
rep
db 102
movsw
and ax,3
l1:
add cx,ax
l2:
rep movsb
pop ax
ret

mallocset: ;malloc+memset ncr retorna di
call malloc
push di
push cx
call memset
pop cx
pop di
ret


memset: ;ds:di cx,al cr di,cx,eax
mov es,ds
setdata: ;es:di al
mov ah,al
setdataw: ;es:di ax
push ax
db 102 ;pref 32bits
shl ax,16
pop ax
setdataq: ;es:di eax
cmp cx,4
jc >l3
test di,1
jz >l1
stosb
db 102
ror ax,8
dec cx
l1:
test di,2
jz >l1
stosw
db 102
ror ax,16
dec cx
dec cx
l1:
push cx
shr cx,2
jz >l1
rep
db 102
stosw
l1:
pop cx
l3:
test cx,2
jz >l1
stosw
db 102
ror ax,16
l1:
test cx,1
jz >l1
stosb
l1:
xor cx,cx
ret

even 2
tmp: dw ? ;usado por alloca
iniciomemoria:
dw 4 ;block size
dw 4 ;nbytes to next free block ;block 1 reserved
dw iniciomemoria+8 ;ptr to the last block
dw -iniciomemoria-8 ;block size
dw -iniciomemoria-8 ;block 2 free


Comments

  • ASHLEY4ASHLEY4 Member Posts: 254
    Thank's octavio2,that bit of code was just what i want:-)
    I'm trying to implement ISR of the int 21h function's 48 & 49 in a dos clone OS,thank's for the code.

    \|///
    @)
    ASHLEY4.
  • ASMcatASMcat Member Posts: 10
    This is some quality code. Thanks! I think I'll try to use this and incorporate it into my OS when the time comes. Thanks again!
  • PrzemekG_PrzemekG_ Member Posts: 595
    [b][red]This message was edited by the PrzemekG_ at 2002-5-18 8:8:2[/red][/b][hr]
    I'm not sure it is a good routine for int 21h 48, 49. The original routines allocates segments (num of bytes SHR 4), if a program will want to allocate 10 pages (10 * 16) it will get only 10 bytes not 160 bytes.

    You can always get the routine from an open source dos clone (maby FreeDOS but I'm not sure it is open source)
  • PrzemekG_PrzemekG_ Member Posts: 595
    : [b][red]This message was edited by the PrzemekG_ at 2002-5-18 8:8:2[/red][/b][hr]
    : I'm not sure it is a good routine for int 21h 48, 49. The original routines allocates segments (num of bytes SHR 4), if a program will want to allocate 10 pages (10 * 16) it will get only 10 bytes not 160 bytes.
    :
    : You can always get the routine from an open source dos clone (maby FreeDOS but I'm not sure it is open source)
    :
    Octavio told me he will write a new malloc routine (like C farmalloc of dos int 21h 48h) but I'm not sure it's good for a dos clone. If you realy want a real DOS malloc function try to play with the original MS DOS, Disassemle everything, get the rawrite (windows version) or DiskView to see the boot sector of a system floppy and see which file is loaded first and then step line by line of the code and you will find there all you need to make a DOS CLONE.

    P.S. As you told me, FreeDOS (like all older OSes and Apps) have jumps all over the source (like BASIC goto) now we don't do it, FreeDOS use it because it's a DOS clone.

  • octavio2octavio2 Member Posts: 16
    : : I'm not sure it is a good routine for int 21h 48, 49.
    Correct, these routines don't work as DOS services,if you want to do a dos clone, use "diskcopy" instead :)
    ;autor: Octavio Vega 933191278@terra.es
    ;16 bits memory management routines
    ;use a86 to assemble this code
    ;these routines expect a number of bytes/16 in CX and or a segment ptr in DI (pointers are not checked)
    ;and return a new segment ptr. in DI
    ;is asumed that ds=start of memory and df=0



    push ds
    mov ax,ss ;start memory after the program stack
    add ax,4096
    mov ds,ax
    mov cx,0
    call initmem
    pop ds
    ret

    initmem: ;ds=start of memory
    ;cx=size reserved for ds
    mov [12],cx ;primer bloque
    mov [14],cx ;bytes 0-11 are unused
    add dx,cx
    inc dx
    mov ds,dx
    mov ax,9ffeh ;end of memory, 0a000h=videomem
    sub ax,dx
    neg dx
    dec dx
    mov [12],dx ;segundo bloque
    mov [14],ax
    mov ax,9fffh
    mov ds,ax
    neg ax
    dec ax
    mov [14],ax ;tercer bloque
    ret



    sfree:
    ;puntero en di
    ;inicio de la memoria en ds
    ;no se comprueban los punteros
    ;retorna di=0 , ncr
    push ds
    pusha
    pop di
    push 0
    dec di
    ;buscar puntero libre anterior
    mov si,ds ;iniciomemoria
    l5:
    mov bp,si
    l3:
    mov ds,si
    mov ax,[12]
    mov bx,si
    inc si
    add si,ax
    jz >l1
    jc >l8
    cmp si,di
    jc l3
    l1:
    ;enlazar
    mov cx,si
    sub cx,di
    sub ax,cx
    dec cx
    mov [12],ax
    ;fusionar con el anterior
    cmp ax,[14]
    mov ds,di
    mov dx,[14]
    jne >l1
    cmp bx,bp ;no fusionar con el primero
    je >l1
    mov di,bx
    inc ax
    add cx,ax
    add dx,ax
    l1:
    mov bx,dx
    cmp bx,cx
    jne >l1
    ;fusionar con el siguiente
    test si,si
    jz >l1
    mov ds,si
    add bx,[14] ;[si-2]
    add cx,[12] ;[si]
    inc bx
    inc cx
    l1:
    mov ds,di
    mov [14],bx
    mov [12],cx
    clc
    l8:
    popa
    pop ds
    ret

    srealloc: ;cx=nbytes/16
    ;di=ptr
    test di,di
    jz smalloc
    jcxz sfree
    push ds
    pusha
    dec di
    mov si,ds
    mov ds,di
    mov dx,[14]
    sub dx,cx
    jz >l8
    jc >l2
    mov [14],cx ;reduce size
    add di,cx
    inc di
    mov ds,di
    dec dx
    mov [14],dx
    mov ds,si
    jmp l5 ;sfree
    l2:
    add dx,cx
    mov ds,si
    pop si
    call smalloc
    push di
    jc >l8
    mov cx,dx
    call smovedata
    mov di,si
    call sfree
    l8:
    popa
    pop ds
    ret


    smalloc: ;numero de paragrafos en cx
    ;inicio de la memoria en ds
    ;retorna segmento en di
    push ds
    mov di,cx
    pusha
    stc
    jcxz l8
    mov ax,ds ;iniciomemoria
    mov di,[12]
    l3:
    mov dx,ax
    add ax,di
    jc l8
    inc ax
    mov ds,ax
    mov bx,[14]
    mov di,[12]
    sub bx,cx
    jc l3
    ;encontrado
    pop [14]
    push ax
    mov ds,dx
    je >l8
    ;fragmentar
    inc cx
    dec bx
    add [12],cx
    add ax,cx
    mov ds,ax
    sub di,cx
    mov [14],bx
    mov [12],di
    jmp >l9
    l8:
    inc di
    add [12],di
    l9:
    popa
    inc di
    clc
    pop ds
    ret

    scheckmem: ;busqueda de errores en la memoria
    ;inicio de memoria en ds
    ;retorna dx nbloques o 0 si error,cx memoria libre
    ;retorna en bx el maximo bloque libre
    ;ncr: bp
    push ds
    mov di,ds ;iniciomemoria
    mov si,di
    xor dx,dx
    xor ax,ax
    xor bx,bx
    mov cx,[14]
    neg cx
    l2:
    inc di
    add di,[12]
    mov ax,[14]
    add cx,ax
    cmp bx,ax
    jnc >l3
    mov bx,ax
    l3:
    add si,[14]
    inc dx
    jc >l4
    inc si
    jz >l5
    cmp si,di
    mov ds,si
    jne l3
    jmp l2
    ;ERROR
    l4:
    xor dx,dx
    pop ds
    stc
    ret
    l5:
    test di,di
    jnz l4
    pop ds
    clc
    ret

    smovedata: ;si,di,cx ncr
    push ds
    push es
    pusha
    mov bx,si
    mov bp,di
    mov ax,cx
    mov dx,cx
    l1:
    sub ax,4k-1
    jc >l2
    mov cx,4k-1
    l2:
    xor si,si
    xor di,di
    mov ds,bx
    mov es,bp
    mov ax,cx
    shl cx,2
    rep
    db 102
    movsw
    add bp,ax
    add bx,ax
    sub dx,ax
    mov ax,dx
    mov cx,dx
    jnz l1
    popa
    pop es
    pop ds
    ret


  • Justin BibJustin Bib USAMember Posts: 0

    ________ ( http://forcoder.org ) free ebooks and video tutorials about \ Python, Delphi, MATLAB, Go, Swift, C, JavaScript, PHP, R, Java, C#, Ruby, PL/SQL, Assembly, Visual Basic, Objective-C, Perl, C++, Scratch, Visual Basic .NET Erlang, VBScript, SAS, COBOL, Scheme, Alice, Rust, Bash, Transact-SQL, Kotlin, ML, Prolog, Logo, Hack, Julia, ABAP, Apex, FoxPro, Awk, D, Dart, Fortran, LabVIEW, Scala, Ada, Lisp, F#, Lua, Clojure, Crystal \ ______

Sign In or Register to comment.