Assembly Language Help (alang.hlp) (Table of Contents; Topic list)
WriteFile
 Map                                       Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
;* WriteFile - Write ASCIIZ string to file. If Handle = 0, the string is
;* written to STDOUT (console). See the CopyFile procedure for another
;* example of using DOS Function 40h to write to files.
;*
;* Shows:   DOS Function - 40h (Write File or Device)
;*          Instructions - inc      dec
;*
;* Params:  Handle - File handle
;*          SPtr - Pointer to ASCIIZ string
;*
;* Return:  Short integer with error code
;*          0 if successful
;*          1 if write error
;*          2 if number of bytes written not equal to string length
 
WriteFile PROC USES ds di,
        Handle:WORD, Sptr:PBYTE
 
        LoadPtr es, di, Sptr            ; Point ES:DI to string
        push    di                      ; Hold on to string pointer
        mov     cx, -1                  ; Set CX to maximum
        sub     al, al                  ; AL = 0
        repne   scasb                   ; Scan string for NULL
        pop     dx                      ; Recover string pointer
        dec     di
        sub     di, dx                  ; Get string length (w/o NULL)
        mov     cx, di                  ; Put it into CX
        mov     bx, Handle              ; Load BX with handle
        push    es                      ; Set DS to ES to ensure
        pop     ds                      ;   DS:DX points to string
        mov     ah, 40h                 ; Request DOS write
        int     21h                     ; Write File or Device
        mov     bx, ax                  ; Get number of bytes written
        mov     ax, 0                   ; Set error code, preserve carry
        .IF     carry?                   ; If carry:
        inc     ax                      ; Increment once for write error
        .ENDIF  ; carry
        .IF     bx != cx                ; If bytes not all written:
        inc     ax                      ; Increment twice
        .ENDIF  ; bx ! cx
        ret
 
WriteFile ENDP
                                    -♦-