Hello~my dear friends
I have a problem about "Frequency Table"
How to solve the following problem :


Write a procedure named "Get_frequencies" that construct a character frequency
table.Input to the procedure should be a pointer to a string , and a pointer to an
array of 256 doublewords.Each array position is indexed by its corresponding ASCII code.When the procedure return , each entry in the array contains a count
of how many times that character occurred in the string.



Please help me ~ I have no idea to do that
Thanx~ :confused:
Posted on 2004-06-06 01:08:09 by Angus


FreqTable proc uses edi str1,pTable
mov edi,pTable
mov edx,str1
; first, cleanup the pTable
mov ecx,256
xor eax,eax
rep stosd
mov edi,pTable
; now, for each character, until 0...
@@:
mov al,[edx] ; get character
inc edx ; increase the string pointer
inc dword ptr[edi+eax*4] ; increase the number of occurences
or eax,eax
jnz @B ; if we didn't get the 0 char, we loop to the label above
ret
FreqTable endp
Posted on 2004-06-06 04:26:25 by Ultrano
Sounds like more homework... Huffman I suppose.
It's very simple. Create an array of 256 dwords. Each dword corresponds to the character with the respective code. Set all dwords to 0.
Then scan the string, for each character, increase its corresponding array element by 1.
Posted on 2004-06-06 04:31:51 by Scali
Thanks for your help , Ultrano

but when I write a main program to test it
it seems that it doesn't work
please tell me where the problem is , Thanks a lot ^^


here it is:

INCLUDE Irvine32.inc

.data

str1 BYTE "DCECFBBCAAA",0
pTable DWORD 256 DUP(0)


.code


FreqTable proc
mov edi,pTable
mov dl,str1
; first, cleanup the pTable
mov ecx,256
xor eax,eax
rep stosd
mov edi,pTable
; now, for each character, until 0...
@@:
mov al, ; get character
inc edx ; increase the string pointer
inc dword ptr ; increase the number of occurences
or eax,eax
jnz @B ; if we didn't get the 0 char, we loop to the label above
ret
FreqTable endp


main PROC
invoke FreqTable
exit
main ENDP
END main
Posted on 2004-06-06 11:33:57 by Angus
A little googling reveals that 'irvine32' might be the stuff that cae with the book "Assembly Language for Intel-Based Computers" by Kip R. Irvine...

Are you trying to learn assembly from this book, or is it what your teacher is using, and thus asking us to do your homework? ;)

There's a couple of problems with your botched-up version of Ultrano's code. You'll need to add "offset" in the two first mov statements, and you need to use edx and not dl for str1.

And what's with the exit? A macro from irvine? If not, do "invoke ExitProcess, 0".

Btw Ultrano, why "or eax, eax" instead of "test eax, eax"?
Posted on 2004-06-06 11:41:09 by f0dder
OT:
I remember a post about or vs test for testing 0, but cannot find it right now. (Better search keyword combination?)
Anyhow, one measured time between or and test, and found or was faster. (it was logically puzzling, but he got numbers.) And, aside, or-method is what some people's beloved comipler generates.
Posted on 2004-06-06 19:52:13 by Starless
I wonder if there's any logical explanation in the manuals (too tired + busy checking), and whether the tests were 100% valid - alignment or such. I prefer test, since test "logically" doesn't modify the register (of course OR doesn't do it in practice, as dst=src, so it's purely aesthetics). The opcodes are equal in length, so... *shrug*.

Testing something like this is pretty delicate anyway - cache issues, lookahead instruction decoding, and whatnot.
Posted on 2004-06-06 20:04:24 by f0dder
By "logically puzzling", I meant that OR writes back to the dest register, whereas TEST dose not. (as you reiterated.) So, the common sense of optimization may suggest that TEST should be faster, and I personally use that, too. But, who knows? When MS compiler generates some code, it makes me think, unlike 16 bit days.
Posted on 2004-06-06 23:43:13 by Starless
I don't really know this:
inc dword ptr

please someone nice tell and explain me what it means?
Does it mean the number of occurences?
if so , what register is it stored?
and if I want to output it , what to do that?

Thanx for yours help^^
Posted on 2004-06-06 23:47:51 by Angus
And this is exactly why you shouldn't paste complete, badly documented code for people.
Obviously he didn't understand anything of it.
Do not help him to 'fix' the code. Instead, let him try to understand the code and find the problem himself.
So Angus, it is up to you to come up with the right questions.
So far you're off... It doesn't count in registers (obviously, since there are not enough registers), it counts in an array in memory.
Posted on 2004-06-07 02:19:18 by Scali
Sorry about that~
but there's always something against what you think
Maybe you think I asked someone to do my job
that's big mistake
I think you are a little bit pretentious
I just a new one in the field of code world
and I not merely want to know that I don't know
I always do my best level to learn
If you still think that I'm like a drunking fool
Thanks a lot for that
You are really a "somebody"!
Posted on 2004-06-07 11:08:18 by Angus


inc dword ptr[edi+eax*4]

Look at it, and think for a bit. INC == increment, or add one.
denote a memory reference
do you know maths? edi+eax*4 means exactly that... so, the CPU stores EAX in a temporary place, mutiplies it with four, and adds EDI - this is used as an address referencing a memory address, and that memory address (a DWORD) is incremented by one.

Try to think a bit for yourself ;)

www.madwizard.org - get his "win32asm basic tutorial"
Posted on 2004-06-07 11:11:56 by f0dder