assembly - frequency table with ascii index and decimal value -
i write code computer each character in string's frequency, , array index 0 255, ascii index , value frequency character appear.
i compare each character in string , add 1 in array each time.
but cannot find error count seem not right(too large)?
the result should
but result's count large.
the error comes in l1 loop , don't know why loop cause large number.
please give me guidance wrong.
thank reading.
include irvine32.inc .data teststring byte "aaebdcfbbc",0 freqtable dword 256 dup(0) prompt byte 0dh, 0ah, 0 prompt1 byte ": ", 0 .code freq proc uses edi, tstring:ptr byte, ftable:ptr dword mov eax,0 cld mov edi,ftable mov ecx,256 rep stosd;initialize ftable 0 mov edi,ftable;reset edi position mov edx,tstring mov ecx,sizeof tstring;element number dec ecx;remove null character l1: mov al,[edx] ;character value inc edx ;index ++ inc dword ptr[edi+eax] ;value ++ loop l1 ret freq endp main proc main endp invoke freq, addr teststring, addr freqtable mov ecx, 256 mov ebx, 0 mov edi,offset freqtable mov eax, 0 l1: call writehex;index mov edx,offset prompt1 call writestring;": " mov ebx, [edi + eax] xchg eax,ebx call writeint xchg eax,ebx mov edx,offset prompt call writestring;endline inc eax;index ++ loop l1 ;ret end main
from first glance code seems right. find line mov ecx,sizeof tstring
little suspicious, make sure returns size of string - fear returns size of pointer. (but should make count less not more.)
also, during write loop, make sure various functions call don't change registers depend on, particularly eax
in danger.
otherwise should use debugger check whether produced result correct can tell whether it's freq
function or printing loop that's faulty. having found that, should step through part see goes wrong.
update
your freqtable
apparently made of dwords (4 bytes each) indexing uses 1-byte units. you'll have change inc dword ptr[edi+eax]
inc dword ptr[edi+eax*4]
in counting loop, , mov ebx, [edi + eax]
mov ebx, [edi + eax*4]
in printing loop.
Comments
Post a Comment