Code: Select all
Procedure.l CountChars(a.s,s.s)
!mov edi,dword[esp] ;pointer to the first character in string (first function parameter)
;firstly we must found the lenght of the string:
!cld ;clear DF (Direction Flag). Could be not necessary.
!xor al,al ;set NULL character in AL register
!xor ebx,ebx ;init counter
!mov ecx,ebx ;lets set 4294967295 ($FFFFFFFF) characters maximum
!dec ecx
!repnz scasb ;repeat comparing AL CPU register content with [edi]
!jecxz go ;if NULL byte is not found within those 4294967295 characters then exit giving 0
!not ecx ;else, some adjusts. Now we have the lenght at ecx register
!mov edi,dword[esp] ;point again to the first character in string (first function parameter)
!mov eax,dword[esp+4]
!mov al,byte[eax] ;al=character to find
!@@:REPNZ scasb ;repeat comparing AL CPU register content with [edi]
!jecxz go ;until ecx value is reached
!inc ebx ;or a match is found
!jmp @r ;continue comparing next character
!go:MOV eax,ebx ;output the matches counter
ProcedureReturn
EndProcedure
st.s="asfhaisu f78a.wetr.q8 fsa su.f789 ay "
ch.s="."
MessageRequester("",Str(CountChars(st,ch)),0)
AL