1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Writing a (text)File Compressor in MASM-need some help.

Discussion in 'Assembly Language Programming (ALP) Forum' started by leftovas17, Nov 7, 2009.

  1. leftovas17

    leftovas17 New Member

    Joined:
    Nov 7, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Here is the deal. I am writing a program in assembly that compresses and decompresses text based files. The user should be able to input a text file, and declare the name of what that file should be name when turned into a .drl file (just a random extension for my prog).

    Right now I am working on reading the file to be compressed. First off I am reading the file one byte at a time and looking for " "(the space character). I want to search the text files for " word" and replace each instance of that word preceded with a space with a "key." This key will be designed using Huffman's Algorithm.

    Anyways I want to read the character and as long as its not null, append it to the end of a string. (this part is done)

    Next, I want to add my new found word to an array of words to be used later in determining the frequency of words, and/or converting them to symbols for compression. However, i am stuck on the label starting at line 215. When I try to add the word to the array, all I get is some symbols. NOT MY WORD. How do I easily add my word found to the array?

    Those are all the questions I am at the point of asking right now, but as I have seen plenty of times before, you guys are pretty strict on not giving out help on homework without effort shown. So here is my incomplete code. Feel free to make any and all helpful criticism.

    Thank You.

    Code:
    ;AUTHOR: DANIEL LUCAS
    
    Include Irvine32.inc  ;includes Master Irvine's Library
    
    .data
    message1 BYTE "Please type the file name of the file to be compressed",0     ;initiates the variable message1 and assigns it "Please type the file name of the file to be compressed", 0
    message2 BYTE "Please type the filename to save the compression to(must end in .drl):",0       ;initiates the variable message2 and assigns it "Please type the filename to save the compression to:", 0
    message3 BYTE "Cannot compress an empty file!",0                             ;initiates the variable message3 and assigns it "Cannot compress and empty file", 0
    message4 BYTE "Please select one of the options:",0dh, 0Ah,
                    "1. Compress a file",0dh,0ah,
                    "2. DeCompress a file",0dh,0ah,0               ;initiates the variable message4 and assigns it "PLease select one 1. Compress 2. DeCompress", 0
    message5 BYTE "Please type the file name of the file to be DeCompressed(must be a .drl file):",0     ;initiates the variable message1 and assigns it "Please type the file name of the file to be Decompressed", 0
    message6 BYTE "Please type the filename to save the DeCompressed File to:",0       ;initiates the variable message2 and assigns it "Please type the filename to save the DeCompression to:", 0
    message7 BYTE "Cannot compress an empty file!",0                             ;initiates the variable message7 and assigns it "Cannot compress and empty file", 0
    message11 BYTE "Here3", 0dh, 0Ah,0                                                 ;initiates the variable message8 and assigns it "Complete!", 0
    message9 BYTE "Here1", 0dh, 0Ah,0   
    message10 BYTE "Here2", 0dh, 0Ah,0   
    message8 BYTE "Complete!",0   
    handle1 DWORD ?                                    ;Uninitialized variable for handle1
    handle2 DWORD ?                                    ;Uninitialized variable for handle2
    handle3 DWORD ?                                    ;Uninitialized variable for handle3
    BUFFER_SIZE = 1                                    ;sets buffer size to 1
    buffer BYTE BUFFER_SIZE DUP(0)                            ;Duplicates buffer to buffer size and sets to 0
    bytesRead DWORD ?                                ;Unintialized variable for bytesread
    bytesWritten DWORD ?                                ;Unintialized variable for byteswritten
    sourceFile BYTE 20 DUP(0),0                            ;initiates the variable for the sourceFile
    destinationFile BYTE 20 DUP(0),0                        ;initiates the variable for the destinationFile
    newword byte 46 DUP(?),0
    fresh byte 46 DUP(0),0
    store dword ?
    wordArray DWORD ?
    fakeword BYTE "thisWord",0
    
    .code
    main PROC                    ;beginning of main proc
    call ShowPrompt                ;calls showprompt proc
    call checkForEmptyFile        ;calls checkforemptyfile proc
    call getFileHandles            ;calls getfilehandles proc
    call ReadFromFile1            ;calls read from file1 proc
    call closeFiles                ;calls the closeFiles proc
    exit                        ;properly exits
    main ENDP                    ;end of main proc
    
    ;--------------------------------------------------------------------------------------------------------------;
    showPrompt PROC                ;beginning of showPrompt proc
    L1:                            ;loop one
    mov edx, OFFSET message4    ;moves the offset of message 4 into edx
    call WriteString            ;displays message4
    call crlf                    ;new line
    call ReadChar                ;reads user input of a char
    cmp al, "1"                    ;compares input to the char '1'
    je Compress                    ;if equal then jump to Compress
    cmp al, "2"                    ;compares user input to the char '2'
    je DeCompress                ;if equal then jump to DeCompress
    jmp L1                        ;start loop over again if neither '1' nor '2'
    
    Compress:                    ;beginning of Compress label
    mov edx, OFFSET message1    ;moves the offset of message1 into edx
    call WriteString            ;displays message1
    call crlf                    ;new line
    mov edx, OFFSET sourceFile    ;moves the offset of sourceFile into edx
    mov ecx, SIZEOF sourceFile    ;moves the sizeOf sourceFile into ecx
    call ReadString                ;reads the user input of a string
    call crlf                    ;new line
    Compressb:                    ;destination
    mov edx, OFFSET message2    ;moves the offset of message2 into edx
    call WriteString            ;displays message2
    call crlf                            ;new line
    mov edx, OFFSET destinationFile        ;moves the offset of destinationFile into edx
    mov ecx, SIZEOF destinationFile        ;moves the sizeOf destinationFile into ecx
    call ReadString                        ;reads the user input of a string
    call crlf                            ;new line
    push eax
    mov edi, OFFSET destinationFile
    mov eax, ".drl"
    mov ecx, SIZEOF destinationFile
    cld
    repne scasb
    jnz Compressb
    dec edi
    pop eax
    ret                                    ;return
    
    DeCompress:                            ;beginning of DeCompress Label
    mov edx, OFFSET message5            ;moves the offset of message5 into ex
    call WriteString                    ;displays message5
    call crlf                            ;new line
    mov edx, OFFSET sourceFile            ;moves the offset of sourceFile into edx
    mov ecx, SIZEOF sourceFile            ;moves the sizeOf sourceFile into ecx
    call ReadString                        ;reads the user input of a string
    call crlf                            ;new line
    
    mov edi, OFFSET sourceFile
    mov eax, ".drl"
    mov ecx, SIZEOF sourceFile
    cld
    repne scasb
    jnz DeCompress
    dec edi
    
    mov edx, OFFSET message6            ;moves the offset of message6 into edx
    call WriteString                    ;displays message6
    call crlf                            ;new line
    mov edx, OFFSET destinationFile        ;moves the offset of destinationFile into edx
    mov ecx, SIZEOF destinationFile        ;moves the sizeOf destinationFile into ecx
    call ReadString                        ;reads the user input of a string
    call crlf                            ;new line
    ret                                    ;return
    showPrompt ENDP                        ;end of ShowPrompt proc
    
    ;--------------------------------------------------------------------------------------------------------------;
    checkForEmptyFile PROC
    mov edx, OFFSET sourceFIle                ; moves the offset of sourceFile into edx
    call OpenInputFile                        ; calls openinputfile proc
    cmp eax, INVALID_HANDLE_VALUE            ;check validity of file handle
    je Quit2
    mov handle3, eax                                    ;jumps to quit label if invalid
    mov edx, OFFSET buffer                        ;moves the offset of buffer into edx
    mov ecx, BUFFER_SIZE
    mov eax, handle3                        ;moves the buffer_Size into ecx
    call ReadFromFile                        ;calls ReadFromFile
    mov bytesRead, eax                        ;move eax into bytesRead
    cmp eax, 0                            ;compare eax to 0
    je QUIT                                ;if eax=0 then jump to quit
    mov eax, handle3                    
    call Closefile                        ;calls closeFiles proc        
    ret                                    ;return
    
    QUIT:                                ;beginning of QUIT label
    mov edx, OFFSET message7            ;moves the offset of message7 into edx
    call WriteString                    ;displays message7                
    call crlf                            ;new line
    exit                                ;properly exits
    
    QUIT2:
    mov edx, OFFSET message6            ;moves the offset of message6 into edx
    call WriteString                    ;calls writestring
    call crlf                            ;new line
    exit                                ;exits
    checkForEmptyFile ENDP                ;end of checkForEmptyFile proc
    
    ;--------------------------------------------------------------------------------------------------------------;
    getFileHandles PROC                        ;beginning of getfilehandles proc
    mov edx, OFFSET sourceFIle                ; moves the offset of sourceFile into edx
    call OpenInputFile                        ; calls openinputfile proc
    cmp eax, INVALID_HANDLE_VALUE            ;check validity of file handle
    je Quit                                    ;jumps to quit label if invalid
    mov handle1, eax                        ;moves eax into handle1
    mov edx, OFFSET destinationFile                    ;moves the offset of the destination file into edx
    call CreateOutputFile                        ;calls the CreatOutputFile proc
    cmp eax, INVALID_HANDLE_VALUE            ;check validity of file handle
    je Quit                                    ;jumps to quit label if invalid
    mov handle2, eax                        ;moves eax into handle2
    ret                                        ;return
    
    QUIT:                                    ;beginning of quit label
    mov edx, OFFSET message6                ;moves the offset of message6 into edx
    call WriteString                        ;writes edx
    call crlf                                ;new line
    call crlf                                ;new line
    call main                                ;calls main proc
    getFileHandles ENDP                            ;end of getFileHandles Proc
    
    ;--------------------------------------------------------------------------------------------------------------;
    readFromFile1 PROC                            ;begining of readFromfile1 proc
    mov store, OFFSET newword
    mov edi, OFFSET newword
    mov esi, OFFSET wordArray
    Read:                                    ;beginning of read label
    
    
    mov edx, OFFSET message9                    ;moves the offset of message9 into edx
    call WriteString                        ;displays message9
    
    First:
    mov eax, handle1                        ;moves handle1 into eax
    mov edx, OFFSET buffer                    ; moves the offset of the buffer into edx
    mov ecx, BUFFER_SIZE                    ;moves the buffer size into ecx
    
    call ReadFromFile
    cmp eax, 0
    je Quit2
    cmp buffer, " "
    je Next
    jmp first
    
    NEXT:
    mov al, buffer
    mov [edi], al                        ;moves the data into bytesread
    inc edi
    
    
    
    New:
    mov eax, handle1                        ;moves handle1 into eax
    mov edx, OFFSET buffer                    ; moves the offset of the buffer into edx
    mov ecx, BUFFER_SIZE                    ;moves the buffer size into ecx
    call ReadFromFile                        ;reads data from file
    cmp eax, 0                            ;compares eax to 0
    je Quit2                            ;if eax = 0 then jump to Quit2
    add eax, bytesread
    mov bytesread, eax
    cmp buffer, " "
    jne NEXT
    
    mov edi, OFFSET newword
    mov eax, " "
    mov ecx, SIZEOF newword
    cld
    repne scasb
    jnz noWorth
    dec edi
    cmp bytesread, 4
    jb NoWorth
    
    Worth:
    mov edi, OFFSET newword
    mov [esi], edi
    add esi, 4
    
    NoWorth:
    call clear
    mov bytesread, 0
    
    mov edx, OFFSET newword                    ;moves the offset of message9 into edx
    call WriteString    
    jmp first
    
    
    Quit:                                ;beginning of Quit label
    call crlf                            ;new line
    mov edx, OFFSET message8                    ;moves the offset of message8 into edx
    call WriteString                        ;displays message8
    ret                                ;return
    
    
    Quit2:                                ;beginning of Quit2 label
    mov edx, OFFSET wordArray                    ;moves the offset of message9 into edx
    call WriteString                        ;displays message9
    ret                                ;return
    readFromFile1 ENDP                        ;end of readFromFile1 proc
    
    ;--------------------------------------------------------------------------------------------------------------;
    clear PROC                            ;beginning of Clear proc    
    mov ecx, SIZEOF newword                        ;moves the sizeOf buffer into ecx
    mov esi,0                            ;moves 0 into esi
    L2:                                ;loop two    
    mov newword[esi],0                        ;moves 0 into buffer at esi
    inc esi                                ;increment esi    
    loop L2                                ;loop back
    ret                                ;return    
    clear ENDP                            ;end of clear proc
    
    ;--------------------------------------------------------------------------------------------------------------;
    closeFiles PROC                            ;beginning of closeFiles proc
    mov eax, handle1                        ;moves handle1 into eax
    call CloseFile                            ;closes the file
    mov eax, handle2                        ;moves handle2 into eax
    call CloseFile                            ;close the file
    ret                                ;return
    closeFiles ENDP                            ;end of the closeFiles proc
    
    
    end MAIN
     

Share This Page