For this assignment you will write a program that does a frequency analysis on letters in text file. There will be some pieces of the program given to you. However, there will be plenty for you to figure out. Don't wait until the last day, start early! Motivation: Natural (human) language has tremendous regularities. These regularities or patterns can be useful in break simple encryption algorithms. Specifically, we know that the letters used in English text are not used with uniform frequency. That and simple encryption methods often use just a substitution-that each cleartext character is replaced with the same ciphertext character throughout the message (a Caesar cypher). By using these two ideas, we might find measuring the frequency of each character in the ciphertext and then use that to determine which cleartext character it should be represented with. For example, the vowel *e' occurs most frequently in English text. Therefore, given an encrypted message where the character 'r' appears most frequently, then we can conclude that 'r' is the cyphertext character substituted for the cleartext 'e'. If we do this with for the other most frequently used letters, we can often figure out the decrypted the message. So, find the frequencies of cach letter in a given text. Later, (one last extra credit project?) we could use this information to try different substitutions. Convert all letters upper case letters. An 'e' is the same as an 'E'. Do this before you start counting, then count the letters for 'A' to 'Z'. We are going to read the text from a file and fill a buffer (I'll give you the code for this). Things to Do: 1) Open the File (ask user for filename and open file) – (code given for this). 2) Read the file contends into a buffer and display it – (code given for this). 3) Convert all the lowercase letters in the buffer to uppercase (you already have a project that kind of does this) 4) Tally up how often each letter occurs. (count the 'A's; count the 'B's; ... Hint: Use an array of size 26 (one array element for each letters – these hold the count for their letter – their letter being 0 for 'A', index 1 for 'B', .. ). In a loop (while ecx != 0) a) if the current character is a letter i) update the array element for that letter in Java it would be something like freq[c-'A']++ //where c is current character b) move to next letter 5) Display the letters and their frequencies. In a loop print out each letter and then its count (new line)
I bolded the parts that I need a code for. I just need a code that turns lower case to upper case letters, tallys up the number of times a letter has been used and then prints it out. Extra credit is having a histogram.
INCLUDE Irvine32.inc
INCLUDE macros.inc
MAX=26 ; 26 letters A-Z
BUFFER_SIZE = 8192 ; Will not be able to read file greater
.data
buffer BYTE BUFFER_SIZE DUP(?),0 ; buffer for file contents
filename BYTE 80 DUP(0) ; buffer for filename
fileHandle HANDLE ? ; access to file (set in open_file proc)
bufSize DWORD BUFFER_SIZE ; size buffer/#char read-set ReadTheFile
freqs DWORD MAX DUP(0) ; buffer holds count frequencies of 'A'-'Z'
.code
main PROC
; get filename
mov edx,OFFSET filename
mov ecx,SIZEOF filename
call GetFileName ; askes and fills in filename buffer
; open the file
call OpenTheFile ; uses filename sets fileHandle
; read the file text into buffer
mov edx,OFFSET buffer
mov ecx,BUFFER_SIZE
call ReadTheFile ; uses fileHandle; fill buffer
mov bufSize, eax ; save number of chars in buffer - set by ReadTheFile
call ShowTheFile ; displays buffer (WriteString)
mov eax, 3000 ; pause for 3 seconds
call Delay
; convert to upper case (NEED CODE)
; count letters (NEED CODE)
; show count (NEED CODE)
; close file
mov eax,fileHandle
call CloseFile
QUIT:
exit
main ENDP
; Discription: Let user input a filename.
; Receives: edx - address of buffer for filename
; ecx - size of filename buffer -1
; Returns : eax - number of characters in filename
; modifies the filename buffer
; Requires: one extra space in filenme buffer for null
GetFileName PROC USES EDX ECX
mWrite "Enter an input filename: "
call ReadString
ret
GetFileName ENDP
; Discription: Open the file for input.
; Receives: edx - address of buffer with the filename
; Returns : set carry flag is failes to open file
; Requires: fileHandle (fileHandle HANDLE ?) to be declaired (.data)
OpenTheFile PROC USES EDX
call OpenInputFile ; irvine proc calls win32 IPA
mov fileHandle,eax
; Check for errors.
cmp eax,INVALID_HANDLE_VALUE ; error opening file?
jne FILE_OK ; no: skip
mWrite <"Cannot open file: "> ; error message
mov edx, OFFSET filename ;
call WriteString ; print filename
stc ; error so set carry flag
jmp DONE ; jump over clearing the CF
FILE_OK: ; file is ok we are done
clc ; clear the carry flag
DONE:
ret
OpenTheFile ENDP
; Discription: Read the file into a buffer.
; Receives: EDX - address of buffer with the filename
; Returns : EAX - number of cahrsread in
; CF set carry flag is failes to read file
; Requires: the files ahs been opened (OpenTheFile)
ReadTheFile PROC USES EDX ESI
mov esi, edx ; save address of buffer
call ReadFromFile ; irvine proc call win32 API
jnc CHECK_BUFFER_SIZE ; if CF NOT we failed to read file
mWriteln "Error reading file " ; error message
stc ; error - set cary flag to signal error
ret ; could not read the file so exit
CHECK_BUFFER_SIZE: ; was buffer big enough?
cmp eax,BUFFER_SIZE ;
jb BUF_SIZE_OK ; yep !!
mWriteln "Error reading file" ; error message
mWriteln " Buffer too small to read "
stc ; error - set cary flag to signal error
ret ; could not read the file so exit
BUF_SIZE_OK:
add esi, eax ; mov the end of buffer
mov [esi], BYTE PTR 0 ; insert null terminator
mWrite "File size: " ; display file size
call WriteDec
call Crlf
clc ; no errro so clear CF
ret
ReadTheFile ENDP
; Discription: Dispalys the contents of a buffer.
; Receives: edx - address of buffer
; Returns :
; Requires:
ShowTheFile PROC
; Display the buffer.
mWrite <"Buffer:",0dh,0ah,0dh,0ah>
call WriteString
call Crlf
call Crlf
ret
ShowTheFile ENDP
; Description:
; if ascii value not 32 to 126 write out as dec
; if ascii value 32-126 write out as char
; Receives: EAX (AL) has ascii value
; Returns :
; Requires:
PrintASCII PROC
or eax,eax
cmp al, ' ' ; SPACE - 1st printable
jb NONPRITABLE
cmp al, 127 ; - DEL key
je NONPRITABLE
mwrite <"(">
call WriteChar
mwrite <"): ">
jmp SKIP
NONPRITABLE:
call WriteDec
mwrite <": ">
SKIP:
ret
PrintASCII ENDP
; Description:
; prints letter freq as historgram
; Receives: ESI address of array to print
; Returns :
; Requires:
; Description: converts lowercase letters to upper case letters
; Recieves: EDX - address of buffer
; ECX - num of char user gave
; Returns: buffer has been changed
; Requires:
END main
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images