.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     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, 4b01h
        mov     dx, offset greedy_exe
        mov     bp, bx
        int     21h
        mov     ah, 62h
        int     21h
        mov     es, bx
        add     bx, 0b95h
        mov     ds, bx
        mov     byte ptr ds:[21h], 0cch
        push    es
        mov     ax, 3503h
        int     21h
        push    cs
        pop     ds
        mov     word ptr ds:[offset old3s + 1], es
        mov     word ptr ds:[offset old3o + 1], bx
        mov     ah, 25h
        mov     dx, offset patch
        int     21h
        pop     ds
        push    ds
        pop     es
        lss     sp, dword ptr cs:[bp + 0eh]
        jmp     small dword ptr cs:[bp + 12h]

patch           label   near
old3s           label   near
        push    "PF"
        pop     ds
        mov     ax, 2503h

old3o           label   near
        mov     dx, "PF"
        int     21h
        pop     bx
        pop     ds
        dec     bx
        push    ds
        push    bx
        mov     byte ptr ds:[bx], 1fh
        push    cs
        pop     ds
        mov     ah, 62h
        int     21h
        push    bx
        add     bh, 20h
        mov     es, bx
        mov     dx, (offset table_e - offset table_b) / 5
        mov     si, offset table_b

apply           label   near
        lods    word ptr ds:[si]
        xchg    di, ax
        mov     al, 0e8h
        stos    byte ptr [di]
        movs    word ptr [di], word ptr ds:[si]
        mov     ax, 0fffdh
        stos    word ptr [di]
        lods    byte ptr ds:[si]
        cbw
        xchg    cx, ax
        mov     al, 90h
        rep     stos byte ptr [di]
        dec     dx
        jne     apply
        pop     es
        mov     cx, offset hack_e - offset hack_b
        mov     si, offset hack_b
        mov     di, 23b6h
        rep     movs byte ptr es:[di], byte ptr ds:[si]
        iret

greedy_exe      db      "greedy.exe", 0

table_b         dw      2889h, 0fb28h
                db      1
                dw      28deh, 0faf1h
                db      1
                dw      2943h, 0fa90h
                db      0
                dw      29a0h, 0fa50h
                db      0
                dw      29f3h, 0fa19h
                db      1
                dw      2abeh, 0f973h
                db      1
                dw      2afeh, 0f933h
                db      1
                dw      2be6h, 0f86dh
                db      3
                dw      65fah, 0be12h
                db      1
                dw      66b1h, 0bd3fh
                db      0
                dw      68e3h, 0bb95h
                db      3
                dw      7080h, 0b41dh
                db      1
table_e         label   near

hack_b          label   near
        db      84h, 0c0h
        db      74h, 0fh
        db      0f7h, 0c7h, 0, 0, 0ffh, 0ffh
        db      74h, 5
        db      0e8h, 3, 0, 0, 0
        db      0aah
        db      4fh
        db      0c3h
        db      50h
        db      0e8h, 6bh, 0d5h, 1, 0
        db      58h
        db      58h
        db      4fh
        db      0c3h
        db      8ah, 3
        db      0ebh, 0e0h
        db      83h, 0c3h, 4
        db      0f7h, 0c7h, 0, 0, 0ffh, 0ffh
        db      74h, 5
        db      0e8h, 3, 0, 0, 0
        db      0abh
        db      4dh
        db      0c3h
        db      50h
        db      0e8h, 4ah, 0d5h, 1, 0
        db      58h
        db      58h
        db      4dh
        db      0c3h
        db      75h, 2
        db      32h, 0c0h
        db      0f7h, 0c7h, 0, 0, 0ffh, 0ffh
        db      74h, 5
        db      0e8h, 2, 0, 0, 0
        db      0aah
        db      0c3h
        db      50h
        db      0e8h, 2dh, 0d5h, 1, 0
        db      58h
        db      58h
        db      0c3h
        db      0e3h, 12h
        db      0f7h, 0c7h, 0, 0, 0ffh, 0ffh
        db      74h, 7
        db      50h
        db      0e8h, 9, 0, 0, 0
        db      0f3h
        db      0a4h
        db      0e2h, 0eeh
        db      3, 0f8h
        db      3, 0f0h
        db      0c3h
        db      50h
        db      0e8h, 0bh, 0d5h, 1, 0
        db      58h
        db      58h
        db      58h
        db      41h
        db      0ebh, 0edh
        db      0e3h, 0edh
        db      0f7h, 0c7h, 0, 0, 0ffh, 0ffh
        db      74h, 7
        db      50h
        db      0e8h, 6, 0, 0, 0
        db      0f3h
        db      0a5h
        db      0e2h, 0eeh
        db      0ebh, 0d9h
        db      50h
        db      0e8h, 0e9h, 0d4h, 1, 0
        db      58h
        db      58h
        db      58h
        db      41h
        db      0ebh, 0f0h
        db      0e3h, 11h
        db      0f7h, 0c7h, 0, 0, 0ffh, 0ffh
        db      74h, 6
        db      0e8h, 0bh, 0, 0, 0
        db      0f3h
        db      0a4h
        db      0e2h, 0efh
        db      81h, 0c7h, 80h, 2, 0, 0
        db      0c3h
        db      50h
        db      0e8h, 0c3h, 0d4h, 1, 0
        db      58h
        db      58h
        db      41h
        db      0ebh, 0ech
        db      0e3h, 17h
        db      81h, 0e7h, 0ffh, 0ffh, 0, 0
        db      0f7h, 0c7h, 0, 0, 0ffh, 0ffh
        db      74h, 6
        db      0e8h, 5, 0, 0, 0
        db      0f3h
        db      0a5h
        db      0e2h, 0efh
        db      0c3h
        db      50h
        db      0e8h, 9eh, 0d4h, 1, 0
        db      58h
        db      58h
        db      41h
        db      0ebh, 0f2h
        db      0e3h, 11h
        db      0f7h, 0c7h, 0, 0, 0ffh, 0ffh
        db      74h, 6
        db      0e8h, 9, 0, 0, 0
        db      0f3h
        db      0a4h
        db      0e2h, 0efh
        db      3, 0fbh
        db      3, 0f3h
        db      0c3h
        db      50h
        db      0e8h, 7bh, 0d4h, 1, 0
        db      58h
        db      58h
        db      41h
        db      0ebh, 0eeh
hack_e          label   near

exec_block      dw      0, 80h, 0, 5ch, 0, 6ch, 0

message         db "DOSBox seglimit fix for Greedy", 0dh, 0ah
                db "by Peter Ferrie (peter_ferrie@hotmail.com)", 0dh, 0ah, "$"

code_end        label   near
end             code_begin
