;‘¥à¢¨á­ë¥ ¯à®æ¥¤ãàë
;(c) RedArc

;FormatFile - ¯à®¢¥àª  ¢­ãâà¥­­¥£® ä®à¬ â  ä ©« 
;CmpExtFile - ¯à®¢¥àª  à áè¨à¥­¨ï ä ©«  ­  á®®â¢¥âáâ¢¨¥ â ¡«¨æ¥
;concatenat_string_in_memory - ¯®¨áª áâà®ª¨ ¢ ¯ ¬ïâ¨
;ZerroBuffer - ®ç¨áâª  ¡ãä¥à 

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
;‚å®¤: ax - ¯¥à¢ë¥ ¡ ©âë ä ©« 
;‚ëå®¤:
;      CF = 1 - EXE-ä ©«
;      CF = 0 - COM-ä ©«
FormatFile proc near
           cmp ax,'MZ'
           je ExeFormat
           cmp ax,'ZM'
           je Exeformat
           clc
           ret
ExeFormat:
           stc
           ret
FormatFile endp

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
;‚å®¤: dx - ¨¬ï ä ©« 
;      di - â ¡«¨æ  à áè¨à¥­¨©
;‚ëå®¤:
;      CF = 1 - ä ©« ¨¬¥¥â ¤àã£®¥ à áè¨à¥­¨¥
;      CF = 0 - ä ©« ­ ¬ ¯®¤å®¤¨â
CmpExtFile proc near
           push ax bx cx dx si di
           mov si,di
@CEF1:
           xor ax,ax
           mov cx,13
           lodsb
           cmp al,0
           je ZerroByte
           xchg bx,ax
           mov di,dx
           call concatenat_string_in_memory
           cmp di,0ffffh
           jne ExtPresent
           add si,bx
           jmp @CEF1
ZerroByte:
           stc
           jmp short @CEFEnd
ExtPresent:
           clc
@CEFEnd:
           pop di si dx cx bx ax
           ret
CmpExtFile endp

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
;‚•Ž„:
;DS:SI - €„…‘ ˆ‘ŠŽŒŽ‰ ‘’ŽŠˆ
;ES:DI - €„…‘ „‹Ÿ Žˆ‘Š€
;CX - ŠŽ‹ˆ—…‘’‚Ž €‰’ „‹Ÿ ‘€‚…ˆŸ
;BX - „‹ˆ€ ˆ‘ŠŽŒŽ‰ ‘’ŽŠˆ
;‚›•Ž„:
;DI    - ‘Œ…™…ˆ… ‚ ‘…ƒŒ…’… …‘‹ˆ €˜‹ˆ, ‚ Ž’ˆ‚ŽŒ ‘‹“—€… 0FFFFH
concatenat_string_in_memory proc near
             push ax bx cx dx
             cld
             mov al,byte ptr ds:[si]
next_find_string:
             repne scasb
             je found_first_char
             mov di,0ffffh
             jmp end_find_string
found_first_char:
             push cx
             push di
             push si
             mov cx,bx
             dec di
             repe cmpsb
             je found_string
             pop si
             pop di
             pop cx
             jmp next_find_string
found_string:
             pop si
             pop di
             dec di
             pop cx
end_find_string:
             pop dx cx bx ax
             ret
concatenat_string_in_memory endp

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
CloseFile proc near
       mov ax,5701h
       mov cx,word ptr ds:[96h]
       mov dx,word ptr ds:[98h]
       int 21h
       jc CFError
       mov ah,3eh
       int 21h
       jc CFError
       xor cx,cx
       mov cl,byte ptr ds:[95h]
       mov ax,4301h
       int 21h
       jc CFError
       clc
       ret
CFError:
       stc
       ret
CloseFile endp

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
ZerroBuffer proc near
            push ax cx di
            cld
            lea di,[Buffer]
            mov cx,512
            xor ax,ax
ZB0:
            stosw
            loop ZB0
            pop di cx ax
            ret
ZerroBuffer endp
