⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.asm

📁 这是一些例程
💻 ASM
📖 第 1 页 / 共 3 页
字号:
        INVOKE  SetDTA,
                Finfo

        mov     ah, 4Fh                 ; AH = function number
        int     21h                     ; Find Next File

        pushf                           ; Preserve flags

        ; Restore DTA address, pass pointer
        INVOKE  SetDTA,
                OldDta

        sub     ax, ax                  ; Set error code
        popf                            ; Recover flags
        .IF     carry?
        inc     ax                      ; Set error code to 1
        .ENDIF
        ret

FindNext ENDP


;* RenameFile - Renames specified file.
;*
;* Shows:   DOS Function - 56h (Rename File)
;*
;* Params:  Fspec1 - Pointer to old ASCIIZ file specification
;*          Fspec2 - Pointer to new ASCIIZ file specification
;*
;*          The drive must be the same for both arguments, but the path
;*          does not. This allows files to be moved between directories.
;*
;* Return:  Short integer with error code
;*          0 if successful
;*          1 if error

RenameFile PROC USES ds di,
        Fspec1:PBYTE,
        Fspec2:PBYTE

        LoadPtr ds, dx, Fspec1          ; Point DS:DX to old file spec
        LoadPtr es, di, Fspec2          ; Point ES:DI to new file spec
        mov     ah, 56h                 ; AH = function number
        int     21h                     ; Rename File
        mov     ax, 0                   ; Clear error code, keep flags
        .IF     carry?
        inc     ax                      ; Set error code to 1
        .ENDIF
        ret

RenameFile ENDP


;* GetFileTime - Gets date/time for open file specified by handle.
;*
;* Shows:   DOS Function - 57h (Get or Set File Date and Time)
;*          Instructions - shl     shr
;*
;* Params:  Handle - Handle of open file
;*          Sptr - Pointer to 18-byte buffer to receive date/time
;*
;* Return:  Short integer with error code
;*          0 if successful
;*          1 if error

GetFileTime PROC USES di,
        Handle:WORD,
        Sptr:PBYTE

        mov     ax, 5700h               ; AH = function number
                                        ; AL = get request
        mov     bx, Handle              ; BX = file handle
        int     21h                     ; Get File Date and Time
        mov     ax, 1                   ; Set error code, keep flags
        .IF     !carry?                 ; If not carry, continue
        mov     bx, cx                  ; Else save time in BX
        mov     al, bl                  ; Get low byte of time
        and     al, 00011111y           ; Mask to get 2-second incrs,
        shl     al, 1                   ;   convert to seconds
        push    ax                      ; Save seconds
        mov     cl, 5
        shr     bx, cl                  ; Shift minutes into low byte
        mov     al, bl                  ; Get new low byte
        and     al, 00111111y           ; Mask to get minutes
        push    ax                      ; Save minutes
        mov     cl, 6
        shr     bx, cl                  ; Shift hours into low byte
        push    bx                      ; Save hours

        mov     bl, dl                  ; Get low byte of date
        and     bl, 00011111y           ; Mask to get day in BX
        mov     cl, 5
        shr     dx, cl                  ; Shift month into low byte
        mov     al, dl                  ; Get new low byte
        and     al, 00001111y           ; Mask to get month
        mov     cl, 4
        shr     dx, cl                  ; Shift year into low byte
        add     dx, 80                  ; Year is relative to 1980
        push    dx                      ; Save year
        push    bx                      ; Save day
        push    ax                      ; Save month

        LoadPtr es, di, Sptr            ; Point ES:DI to 18-byte
        mov     cx, 6                   ;   string

        .REPEAT
        pop     ax                      ; Get 6 numbers sequentially in AL
        aam                             ; Convert to unpacked BCD
        xchg    al, ah                  ; Switch bytes for word move
        or      ax, '00'                ; Make ASCII numerals
        stosw                           ; Copy to string
        mov     al, '-'                 ; Separator for date text
        cmp     cl, 4                   ; First 3 iters are for date
        jg      @F                      ; If CX=6 or 5, insert hyphen
        mov     al, ' '                 ; Separator date and time
        je      @F                      ; If CX = 4, insert hyphen
        mov     al, ':'                 ; Separator for time text
        .IF     cl != 1
@@:     stosb                           ; Copy separator to string
        .ENDIF
        .UNTILCXZ

        sub     ax, ax                  ; Clear return code
        stosb                           ; Terminate string with null
        .ENDIF                          ;   to make ASCIIZ
        ret

GetFileTime ENDP


;* UniqueFile - Creates and opens a new file with a name unique to the
;* specified directory. The name is manufactured from the current time,
;* making it useful for temporary files. For DOS versions 3.0 and higher.
;*
;* Shows:   DOS Function - 5Ah (Create Temporary File)
;*
;* Params:  Attr - Attribute code (see header comments for CreateFile)
;*          Pspec - Pointer to ASCIIZ path specification
;*
;* Return:  Short integer with file handle or -1 for error

UniqueFile PROC USES ds,
        Attr:WORD,
        Pspec:PBYTE

        ; Get DOS version
        INVOKE  GetVer

        cmp     ax, 300                 ; 3.0 or higher?
        jb      e_exit                  ; No?  Quit with error
        LoadPtr ds, dx, Pspec           ; Point DS:DX to path spec
        mov     cx, Attr                ; CX = attribute
        mov     ah, 5Ah                 ; AH = function number
        int     21h                     ; Create Temporary File
        .IF     carry?
e_exit: mov     ax, -1                  ; Set error code
        .ENDIF
        ret

UniqueFile ENDP


;* CreateNewFile - Creates a new file with specified attribute. Differs
;* from the CreateFile procedure in that it returns an error if file
;* already exists. For DOS versions 3.0 and higher.
;*
;* Shows:   DOS Function - 5Bh (Create New File)
;*
;* Params:  Attr - Attribute code (see header comments for CreateFile)
;*          Fspec - Pointer to ASCIIZ file specification
;*
;* Return:  Short integer with file handle or -1 for error

CreateNewFile PROC USES ds,
        Attr:WORD,
        Fspec:PBYTE

        LoadPtr ds, dx, Fspec           ; Point DS:DX to file spec
        mov     cx, Attr                ; CX = attribute
        mov     ah, 5Bh                 ; AH = function number
        int     21h                     ; Create New File
        .IF     carry?
        mov     ax, -1                  ; Set error code
        .ENDIF
        ret

CreateNewFile ENDP


;* StrCompare - Compares two strings for equality. See StrWrite, StrFindChar,
;* WinOpen, and WinClose procedures for other examples of string instructions.
;*
;* Shows:   Instructions - cmpsb     cmpsw     repe     jcxz
;*
;* Params:  Sptr1 - Pointer to first string
;*          Sptr2 - Pointer to second string
;*          Len  - Length in bytes for comparison. Strings need not be of
;*                 equal length; however if len is an even number, comparison
;*                 is made on a word-by-word basis and thus is more efficient.
;*
;* Return:  Null pointer if strings match; else pointer to string #1 where
;*          match failed.

StrCompare PROC USES ds di si,
        Sptr1:PBYTE,
        Sptr2:PBYTE,
        Len:WORD

        LoadPtr es, di, Sptr1           ; ES:DI points to string #1
        LoadPtr ds, si, Sptr2           ; DS:SI points to string #2
        mov     cx, Len                 ; Length of search in bytes
        and     al, 0                   ; Set ZR flag in case CX = 0
        .IF     cx != 0                 ; If length is not 0:
        .IF     !(cl & 1)               ; If not even number,
        repe    cmpsb                   ;   compare byte-by-byte
        .ELSE                           ; Else compare word-by-word
        shr     cx, 1                   ; Decrease count by half
        repe    cmpsw                   ; Compare word-by-word
        sub     di, 2                   ; Back up 2 characters
        sub     si, 2
        cmpsb                           ; Match?
        .IF     zero?                   ; No?  Then failure
        cmpsb                           ; Compare last characters
        .ENDIF  ; zero
        .ENDIF  ; cl & 1
        .ENDIF  ; cx != 0

        mov     ax, 0                   ; Set null pointer without
        mov     dx, 0                   ;   disturbing flags
        .IF     !zero?                  ; If no match,
        dec     di                      ;   point to failure
        mov     ax, di
        mov     dx, es
        .ENDIF
        ret

StrCompare ENDP


;* StrFindChar - Finds first occurrence of character in given ASCIIZ string,
;* searching either from beginning or end of string. See StrWrite, WinOpen,
;* WinClose, and StrCompare procedures for other examples of string
;* instructions.
;*
;* Shows:   Instructions - repne     scasb    cld     std
;*
;* Params:  Ichar - Character to search for
;*          Sptr - Pointer to ASCIIZ string in which to search
;*          Direct - Direction flag:
;*                       0 = search from start to end
;*                       1 = search from end to start
;*
;* Return:  Null pointer if character not found, else pointer to string where
;*          character first encountered

StrFindChar PROC USES ds di si,
        IChar:SBYTE,
        Sptr:PBYTE,
        Direct:WORD

        LoadPtr es, di, Sptr            ; ES:DI points to string
        LoadPtr ds, si, Sptr            ;   as does DS:SI
        mov     cx, -1                  ; Set scan counter to maximum
        mov     bx, cx                  ; BX = max string tail
        cld                             ; Assume head-to-tail search

        .IF     Direct != 0             ; If assumption correct:
        mov     bx, di                  ; Set BX to byte before
        dec     bx                      ;   string head and scan
        sub     al, al                  ;   string for null terminator
        push    cx                      ;   to find string tail
        repne   scasb
        pop     cx                      ; Recover scan counter
        dec     di                      ; Backup pointer to last
        dec     di                      ;   character in string and
        mov     si, di                  ;   begin search from there
        std                             ; Set direction flag
        .ENDIF

        .REPEAT
        lodsb                           ; Get first char from string
        .IF     (si == bx) || (al == 0) ; If at head or tail limit,
        sub     ax, ax                  ;   no match
        sub     dx, dx                  ; Set null pointer
        jmp     exit
        .ENDIF
        .UNTILCXZ al == IChar

        mov     ax, si                  ; Match, so point to first
        dec     ax                      ;   occurrence
        .IF     Direct != 0             ; If head-to-tail search:
        inc     ax                      ; Adjust pointer forward
        inc     ax
        mov     dx, ds                  ; Pointer segment
        .ENDIF
exit:
        ret

StrFindChar ENDP


;* GetStr - Gets a string of up to 128 characters from the user. Since
;* this function uses the DOS input mechanism, it can use the DOS editing
;* keys or the keys of a DOS command-line editor if one is loaded.
;*
;* Shows:   DOS Function - 0Ah (Buffered Keyboard Input)
;*          Directive    - EQU
;*
;* Params:  Strbuf - Pointer to area where input string will be placed
;*          Maxlen - Maximum length (up to 128 characters) of string
;*
;* Return:  0 if successful, 1 if error (Maxlen is too long)

        .DATA
MAXSTR  EQU     128
max     BYTE    MAXSTR
actual  BYTE    ?
string  BYTE    MAXSTR DUP (?)

        .CODE
GetStr  PROC USES si di,
        Strbuf:PBYTE,
        Maxlen:WORD

        mov     ax, 1                   ; Assume error
        mov     cx, Maxlen              ; Copy length to register

        .IF (cx != 0) && (cx <= MAXSTR) ; Error if 0 or too long
        mov     max, cl                 ; Load maximum length
        mov     ah, 0Ah                 ; Request DOS Function 0Ah
        mov     dx, OFFSET max          ; Load offset of string
        int     21h                     ; Buffered Keyboard Input

        mov     bl, actual              ; Put number of characters read
        sub     bh, bh                  ;   in BX
        mov     string[bx], 0           ; Null-terminate string
        mov     cx, bx                  ; Put count in CX
        inc     cx                      ; Plus one for the null terminator

        LoadPtr es, di, Strbuf          ; ES:DI points to destination buffer
        mov     si, OFFSET string       ; DS:SI points to source string
        rep     movsb                   ; Copy source to destination
        sub     ax, ax                  ; Return 0 for success
        .ENDIF

        ret

GetStr  ENDP

        END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -