TheJuggler
TheJuggler

Reputation: 78

Find the occurrences of a specified letter in a string?

org 100h

   mov si, 0
   mov di, 0    

back: 

   cmp string[si], 'a'
   inc si

   jz found

   jmp nfound

   found:
   inc di

   nfound:

loop back

ret
  string db 'n','a','n','b','o','N','a','n','a','w','a','g','o','s','h','t','b','o','Q','a','s','a','b'

I have also tried to define the string array like this:

string db "nan bo Nanawa gosht bo Qasab"

the jz coindition is never true, what seems to be the problem? If I remove the si increment instruction the condition does return true!

Upvotes: 0

Views: 52

Answers (2)

Fathah Rehman P
Fathah Rehman P

Reputation: 8741

Comparison result is calculating by checking zero flag.

After cmp string[si], 'a' if both are same, then zero flag will be set. So you should use jz found immediately after comparison.

In your code you are using inc si after comparison, and you lost the comparison result, in this case if the value of si is zero after increment then only zero flag will set.

Upvotes: 1

Michael
Michael

Reputation: 58447

If you read the description of INC in Intel's manual you'll see:

Flags Affected
The CF flag is not affected. The OF, SF, ZF, AF, and PF flags are set according to the result.

So your jz found instruction is branching based on the result of inc si rather than the result of the cmp. I suggest that you move the inc si instruction to right before loop back.

Upvotes: 1

Related Questions