.model  tiny
.386
.code
org     100h

code_begin      label   near
        mov     ah, 9
        mov     dx, offset message
        int     21h
        mov     ah, 4ah
        mov     bx, ((offset code_end - offset code_begin) + 20fh) shr 4
        mov     sp, ((offset code_end - offset code_begin) + 1ffh) and -2
        int     21h
        mov     ax, 3521h
        int     21h
        push    es
        push    bx
        pop     dword ptr ds:[offset jfar_21h + 1]
        mov     ah, 25h
        mov     dx, offset new_21h
        int     21h
        push    ds
        pop     es
        mov     bx, offset exec_block
        mov     word ptr ds:[bx + 4], cs
        mov     word ptr ds:[bx + 8], cs
        mov     word ptr ds:[bx + 0ch], cs
        mov     ax, 4b00h
        mov     dx, offset qbasic_exe
        int     21h
        mov     ah, 4ch
        int     21h

new_21h         label   near
        cmp     ax, 4b00h
        jne     jfar_21h
        push    ds
        pusha
        lds     si, dword ptr es:[bx + 2]
        cmp     dword ptr ds:[si + 1], "r C/"
        jne     pop_21h
        push    es
        push    ds
        push    cs
        pop     ds
        mov     ah, 3bh
        mov     dx, offset datadir
        int     21h
        pop     ds
        add     si, 0dh
        mov     dx, si

find_end        label   near
        lodsb
        sub     al, ' '
        jne     find_end
        mov     byte ptr ds:[si - 1], al

find_mid        label   near
        lodsb
        cmp     al, '.'
        jne     find_mid
        lodsd
        and     eax, 00ffffffh
        xchg    ebp, eax
        mov     ah, 4eh
        mov     cl, 0ffh
        cmp     ax, "PF"
org     $ - 2
find_next       label   near
        mov     ah, 4fh
        int     21h
        jb      find_close
        mov     ah, 2fh
        int     21h
        push    es
        pop     ds
        push    cs
        pop     es
        lea     dx, word ptr [bx + 1eh]
        mov     si, dx
        mov     di, offset message
        push    di

copy_name2      label   near
        lodsb
        stosb
        cmp     al, '.'
        jne     copy_name2
        mov     eax, ebp
        stosd
        mov     ah, 56h
        pop     di
        int     21h
        jmp     find_next

find_close      label   near
        pop     es
        push    cs
        pop     ds
        mov     ah, 3bh
        mov     dx, offset parentdir
        int     21h
        popa
        pop     ds
        retf    2

pop_21h         label   near
        popa
        pop     ds

jfar_21h        label   near
                db      0eah, 'P', 'F', 'P', 'F'

qbasic_exe      db      "qbasic.exe", 0
cmdline         db      offset cmdline_e - offset cmdline - 2, " /run ini", 0dh
cmdline_e       label   near

datadir         db      "data", 0
parentdir       db      "..", 0

exec_block      dw      0, offset cmdline, 0, 5ch, 0, 6ch, 0

message         db      "DOSBox fix for Pirateer", 0dh, 0ah
                db      "by Peter Ferrie (peter_ferrie@hotmail.com)", 0dh, 0ah, "$"

code_end        label   near
end             code_begin
