.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     ds, bx
        mov     es, bx
        mov     byte ptr ds:[348h], 0eah
        mov     word ptr ds:[349h], offset patch
        mov     word ptr ds:[34bh], cs
        lss     sp, dword ptr cs:[bp + 0eh]
        jmp     small dword ptr cs:[bp + 12h]

patch           label   near
        push    cs
        pop     ds
        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, 0ffffh
        stos    word ptr [di]
        mov     cx, 90h
        lods    byte ptr ds:[si]
        xchg    cl, al
        rep     stos byte ptr [di]
        dec     dx
        jne     apply
        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]
        push    es
        pop     ds
        retf

greedy_exe      db      "greedy.exe", 0

table_b         dw      0b5e0h, 6dd1h
                db      1
                dw      0b635h, 6d9ah
                db      1
                dw      0b69bh, 6d38h
                db      0
                dw      0b6f8h, 6cf8h
                db      0
                dw      0b74bh, 6cc1h
                db      1
                dw      0b816h, 6c1bh
                db      1
                dw      0b856h, 6bdbh
                db      1
                dw      0b936h, 6b1dh
                db      3
                dw      0f1f2h, 321ah
                db      1
                dw      0f2a9h, 3147h
                db      0
                dw      0f4dbh, 2f9dh
                db      3
                dw      0fc47h, 2856h
                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, 58h, 67h, 0, 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, 37h, 67h, 0, 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, 1ah, 67h, 0, 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, 0f8h, 66h, 0, 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, 0d6h, 66h, 0, 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, 0b0h, 66h, 0, 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, 8bh, 66h, 0, 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, 68h, 66h, 0, 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
