MIPs word count in a file, how many of each word
1. Request the name of a file from the user and open it. If it will not open (for example, file does not exist) print an error message and exit.
2. Read a block of text from a file*
3. Break the line into words. The lines in the file may contain spaces, commas, or periods, colons, and semicolons that separate words. One way of looking at this is that anything that is not a letter or a digit marks the end of a word. A letter or a digit would then mark the beginning of a new word.
4. Look up the word in your list.
A. If it is in the list, increment the count for that word.
B. If it is not in the list, add it with a count of 1
5. Once all the words in the block of text have been counted, return for another line.
6. When you reach the end of the file, print the list of words and their counts, one word per line, then exit. There is no requirement for the list to be sorted; that is extra credit.
Upper-case and lower-case are the same, so Word and word must not be counted separately.
*There is no function in MIPS to read a line of text from a file. Thus your program must read 1K blocks from the file. A word may cross a block boundary, so your program needs to be able to handle this. I will show an example in class.
Where do you store the words? Every time you find a new word, allocate memory for it using the sbrk system call, function 9. You can safely assume there will be no more than 1000 unique words, so you can also allocate space for a list of these words. How much space to allocate? The number of letters in the word plus 4, so you have a word for the count. Perhaps add one byte for the length of the word, rather than using zero terminators.
AS I PASS through my incarnations in every age and race,
I make my proper prostrations to the Gods of the Market Place.
Peering through reverent fingers I watch them flourish and fall,
And the Gods of the Copybook Headings, I notice, outlast them all.
We were living in trees when they met us. They showed us each in turn
That Water would certainly wet us, as Fire would certainly burn:
But we found them lacking in Uplift, Vision and Breadth of Mind,
So we left them to teach the Gorillas while we followed the March of Mankind.
We moved as the Spirit listed. They never altered their pace,
Being neither cloud nor wind borne like the Gods of the Market Place,
But they always caught up with our progress, and presently word would come
That a tribe had been wiped off its icefield, or the lights had gone out in Rome.
With the Hopes that our World is built on they were utterly out of touch,
They denied that the Moon was Stilton; they denied she was even Dutch;
They denied that Wishes were Horses; they denied that a Pig had Wings;
So we worshipped the Gods of the Market Who promised these beautiful things.
When the Cambrian measures were forming, They promised perpetual peace.
They swore, if we gave them our weapons, that the wars of the tribes would cease.
But when we disarmed They sold us and delivered us bound to our foe,
And the Gods of the Copybook Headings said: Stick to the Devil you know.
On the first Feminian Sandstones we were promised the Fuller Life
(Which started by loving our neighbour and ended by loving his wife)
Till our women had no more children and the men lost reason and faith,
And the Gods of the Copybook Headings said: The Wages of Sin is Death.
In the Carboniferous Epoch we were promised abundance for all,
By robbing selected Peter to pay for collective Paul;
But, though we had plenty of money, there was nothing our money could buy,
And the Gods of the Copybook Headings said: If you don't work you die.
Then the Gods of the Market tumbled, and their smooth tongued wizards withdrew
And the hearts of the meanest were humbled and began to believe it was true
That All is not Gold that Glitters, and Two and Two make Four
And the Gods of the Copybook Headings limped up to explain it once more.
As it will be in the future, it was at the birth of Man
There are only four things certain since Social Progress began.
That the Dog returns to his Vomit and the Sow returns to her Mire,
And the burnt Fools bandaged finger goes wabbling back to the Fire;
And that after this is accomplished, and the brave new world begins
When all men are paid for existing and no man must pay for his sins,
As surely as Water will wet us, as surely as Fire will burn,
The Gods of the Copybook Headings with terror and slaughter return.