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. How do I do that?(might be simple, but I'm not seeing it)
Next, how do I read from the file one byte prior to where I finished. Meaning: if I read a space and stop the read(because its the start of a new word) how do I re-read that space to add it to the word next time around?
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.
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
handle1 DWORD ? ;Uninitialized variable for handle1
handle2 DWORD ? ;Uninitialized variable for handle2
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
.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
mov edi, OFFSET destinationFile
mov eax, ".drl"
mov ecx, SIZEOF destinationFile
cld
repne scasb
jnz Compressb
dec edi
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 message9 ;moves the offset of message9 into edx
call WriteString ;displays message9
exit ;properly exits
QUIT2:
mov edx, OFFSET message6 ;moves the offset of message6 into edx
call WriteString ;calls writestring
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
Read: ;beginning of read label
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
First:
call ReadFromFile
cmp eax, 0
je Quit2
cmp eax, " "
je Next
jmp New
NEXT:
mov bytesread, eax ;moves the data into bytesread
New:
call ReadFromFile ;reads data from file
cmp eax, 0 ;compares eax to 0
je Quit2 ;if eax = 0 then jump to Quit2
cmp eax, " "
jne NEXT
push eax ;push eax onto stack
pop eax ;pops eax off stack
cmp eax, 5 ;compares eax to 5
jb Quit ;jump to quit if carry
call clear ;new line
jmp Read ;jump back to Read
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
ret ;return
readFromFile1 ENDP ;end of readFromFile1 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