Main Menu

Home
MASM
C
C/MASM
Java
Google
 
Home arrow MASM arrow Open, Read, Print and Close Test File
Open, Read, Print and Close Test File
;Opens, reads, and closes a text file.  Prints the contents of the file 50
;characters per line, characters read, contents of the file without punctuation
;50 characters per line, the number of alphabetic characters and words in the file.
;Displays the average word size

TITLE Open, Read, and Close File (waldusp1.asm)

INCLUDE Irvine16.inc
.data
    inBuffer                BYTE 2048 DUP (0)
    countBuffer                BYTE 2048 DUP (0)
    fileName                BYTE "words.txt"
    fileHandle                WORD ?
    errorStr                BYTE "Error code ",0
    errorOpen                BYTE " at File Open, terminating.",0
    errorRead                BYTE " at File Read, terminating.",0
    errorClose                BYTE " at File Close, terminating.",0
    charsRead                BYTE " characters read",0
    inBuffContStr            BYTE "Contents of input buffer:",0
    countBuffContStr        BYTE "Contents of count buffer:",0
    charRemStr                BYTE " characters remaining",0
    nonBlankStr                BYTE " non-blank characters",0
    totWordsStr                BYTE " words",0
    avgWordSizeStr            BYTE "average word size is ",0
    bytesReadSaved            WORD ?
    bytesReadTemp            WORD ?
    inBufferRunner            WORD ?
    countBufferRunner        WORD ?
    buff2Ct                    WORD 0
    buff2CtTemp                WORD ?
    spaces                    WORD 0
    numberOfWords            WORD ?
    avgSize                    WORD ?
    chars                    WORD ?
    
.code
main PROC  
    mov ax,@data
    mov ds,ax

    mov ah,3Dh                        ;open file
    mov dx,OFFSET fileName
    mov al,0
    int 21h
    
    mov bx,OFFSET errorOpen            ;check for error opening file
    call ErrorCheck
    
    mov fileHandle,ax
    
    mov ah,3Fh                        ;read file into inBuffer
    mov bx,fileHandle
    mov cx,LENGTHOF inBuffer
    mov dx,OFFSET inBuffer
    int 21h
    
    mov bx,OFFSET errorRead            ;check for error reading file
    call ErrorCheck
    
    call WriteDec                    ;print number of characters read
    
    mov bytesReadSaved,ax            ;save for later use
    mov bytesReadTemp,ax
    
    mov ah,3eh                        ;close file
    mov bx,fileHandle
    int 21h
    
    mov bx,OFFSET errorClose        ;check for error closing file
    call ErrorCheck                    
    
    mov dx,OFFSET charsRead            ;print " characters read"
    call WriteString
    call crlf
    call crlf
    
    mov dx,OFFSET inBuffContStr        ;print "Contents of input buffer:"
    call WriteString
    call crlf
    
    mov di,OFFSET inBuffer            ;di will be used to copy inBuffer to countBuffer
    mov inBufferRunner,di            ;move offset of inBuffer to inBufferRunner

    Again1:                            ;print contents of inBuffer 50 character per line
        cmp bytesReadTemp,51
        jb GetOut                    ;jump out if less than 51 characters
        mov ah,40h
        mov bx,1
        mov cx,50
        mov dx,inBufferRunner
        int 21h
        call Crlf
        add inBufferRunner,50
        sub bytesReadTemp,50
    jmp Again1
    
    GetOut:
    
    mov ah,40h                        ;print last line of inBuffer with less than 51 characters
    mov bx,1
    mov cx,bytesReadTemp
    mov dx,inBufferRunner
    int 21h
    
    call Crlf
    call Crlf
                         
    mov si,OFFSET countBuffer        ;copy inBuffer to countBuffer without punctuation    
    mov cx,bytesReadSaved            
    Again2:
    mov al,[di]
    cmp al,20h                        ;comparisons to discard punctuation
    je Kept
    cmp al,41h
    jb notKept
    cmp al,7Ah
    ja NotKept
    Kept:
    mov [si],al
    inc si
    inc buff2Ct
    NotKept:
    inc di
    loop Again2
        
    mov ax,buff2Ct                    ;copy buff2Ct to buff2CtTemp to save buff2Ct for later
    mov buff2CtTemp,ax

    mov dx,OFFSET countBuffContStr    ;print "Contents of count buffer:"
    call WriteString
    call crlf
    
    mov dx,OFFSET countBuffer        ;move offset of countBuffer to countBufferRunner        
    mov countBufferRunner,dx

    mov cx,buff2Ct                    ;print contents of countBuffer 50 character per line
    Again3:
        cmp buff2CtTemp,51            ;jump out if less than 51 characters
        jb GetOut1
        mov ah,40h
        mov bx,1
        mov cx,50
        mov dx,countBufferRunner
        int 21h
        call Crlf
        add countBufferRunner,50
        sub buff2CtTemp,50
    jmp Again3
    
    GetOut1:
    
    mov ah,40h                        ;print last line of inBuffer with less than 51 characters
    mov bx,1
    mov cx,buff2CtTemp
    mov dx,countBufferRunner
    int 21h
    call crlf
    call crlf
    
    mov ax,buff2Ct                    ;print characters remaining
    call WriteDec

    mov dx,OFFSET charRemStr        ;print " characters remaining"
    call WriteString
    call crlf
    call crlf

    mov di,OFFSET countBuffer        ;pass information to registers
    call passToRegs                    
    
    mov numberOfWords,bx            ;print number of non-blank characters
    mov chars,ax
    add chars,1
    call WriteDec
    
    mov dx,OFFSET nonBlankStr        ;print " non-blank characters"
    call WriteString
    call crlf
    call crlf
    
    mov ax,numberOfWords            ;print number of words
    call WriteDec

    mov dx,OFFSET totWordsStr        ;print " words"
    call WriteString
    call crlf
    call crlf

    mov dx,OFFSET avgWordSizeStr    ;print "average word size is "
    call WriteString

    mov dx,0                        ;calculate average word size
    mov ax,chars
    mov cx,numberOfWords
    div cx
    mov avgSize,ax
    cmp dx,5                        ;control rounding up or down
    jb noRoundUp
    add avgSize,1
    noRoundUp:
    
    mov ax,avgSize                    ;print average word size
    call WriteDec
    call crlf
    call crlf

    exit
main ENDP

;----------------------------------------------------------------------------------------------------
ErrorCheck PROC
; Check for existence of error code when file handling
; Receives AX, error code or file handle
;          BX, offset of string containing possible error event
; Returns nothing, if no error code, or termates program
;----------------------------------------------------------------------------------------------------
    jnc noError                        ;check carry flag
    mov dx,OFFSET errorStr            ;if error code, then print it
    call WriteString
    call WriteHex
    mov dx,bx
    call WriteString
    call crlf
    mov ah,4ch                        ;if error code, then terminate program
    mov al,0
    int 21h
    
    noError:
    
    ret                                ;if no error code, then return to main
ErrorCheck ENDP

;----------------------------------------------------------------------------------------------------
passToRegs PROC
; Passes specific data to the registers
; Receives buff2Ct, the number of characters in countBuffer
;          DI, the offset of countBuffer
; Returns SI, the offset of countBuffer
;         CX, the number of characters in countBuffer
;         AX, the number of non-blank characters in countBuffer
;         BX, the number of words in countBuffer
;----------------------------------------------------------------------------------------------------
    mov si,di                        ;move offset of countBuffer to SI
    
    mov cx,buff2Ct                    ;count spaces in countBuffer
    Again4:
        mov al,[di]
        cmp al,20h
        jne nospaces
        inc spaces
        nospaces:
        inc di
    loop Again4
    
    mov cx,buff2Ct                    ;move number of characters in countBuffer to CX
    
    mov ax,buff2Ct                    ;move the number of non-blank characters in countBuffer to AX
    sub ax,spaces
    
    mov bx,spaces                    ;move the number of words in countBuffer to BX
    add bx,1
    
    ret
passToRegs ENDP

END main

 
< Prev   Next >
© 2009 SourceRip
Joomla! is Free Software released under the GNU/GPL License.