setfree   proc
          mov  bx,ss
          mov  ax,es
          sub  bx,ax

          mov  ax,sp
          mov  cl,4
          shr  ax,cl
          add  bx,ax
          inc  bx

          mov  ah,4ah
          int  21h

          ret

setfree   endp

Melmac PROC
    MOV  AH,48H
    INT  21H
    JNC  Memory_Available
    push cs
    pop  ds
    MOV  AH,9
    LEA  DX,Memory_Message
    INT  21H
    mov  ax,3
    int  10h
    MOV  AH,4CH
    INT  21H
Memory_Available:
    MOV  cs:[DI],AX
    RET
Memory_Message db 7,"Sorry dude! Your machine hasn't got enough memory.$"
Melmac ENDP

LFTEMC PROC
    MOV  AH,49H
    PUSH ES
    PUSH word ptr cs:[DI]
    POP  ES
    INT  21H
    POP  ES
    RET
LFTEMC ENDP

fll dw 0
flh dw 0

Loadseg PROC
; CS:[DI] <= where to store segment
; CS:[DX] <= Filename
        push ds
        push cs
        pop  ds
        mov  ah,3dh
        mov  al,10010010b
        int  21h
        jnc  fileopen
        mov  si,dx
lodmore:
        lodsb
        cmp  al,0
        jne  lodmore
        mov  byte ptr ds:[si-1],'$'
        push dx
        lea  dx,fileerror1
        mov  ah,9
        int  21h
        pop  dx
        mov  ah,9
        int  21h
        lea  dx,fileerror2
        mov  ah,9
        int  21h
        mov  ah,4ch
        int  21h
fileopen:
        mov  bx,ax
        mov  ah,42h
        mov  al,2
        mov  cx,0
        mov  dx,0
        int  21h
        mov  si,ax                         ; loword
        mov  bp,dx                         ; hiword

        mov     cs:fll,ax
        mov     cs:flh,dx

        mov  ah,42h
        mov  al,0
        mov  cx,0
        mov  dx,0
        int  21h
        push bx
        mov  dx,bp
        mov  bx,si
        rept 4
        clc
        rcr  dx,1
        rcr  bx,1
        endm
        add  bx,1
        call melmac
        push word ptr cs:[di]
        pop  ds
        pop  bx
        cmp  bp,0
        jne  mehrere_segmente
        mov  ah,3fh
        mov  cx,si
        mov  dx,0
        int  21h
        mov  ah,3eh
        int  21h
        pop  ds
        ret
mehrere_segmente:
        mov  ah,3fh
        mov  cx,0FFFFh
        mov  dx,0
        int  21h
        mov  ax,ds
        add  ax,1000h
        mov  ds,ax
        sub  si,0FFFFh
        sub  bp,1
        cmp  bp,0
        jg   mehrere_segmente
        mov  ah,3fh
        mov  cx,si
        mov  dx,0
        int  21h
        mov  ah,3eh
        int  21h
        pop  ds
        ret
fileerror1 db 7,"Error! Can't find the file '$"
fileerror2 db "'",13,10,"$"
Loadseg Endp

