flat assembler
Message board for the users of flat assembler.

Index > Main > Counting 'X' and 'x' in string?

Author
Thread Post new topic Reply to topic
TmX



Joined: 02 Mar 2006
Posts: 824
Location: Jakarta, Indonesia
TmX
I have a string "X12xxMXF", and I'd like to count the total occurences of 'X' and 'x'. Of course the expected result is 4

Code:
format PE console 4.0 

include 'win32a.inc' 

entry start 

section '.data' data readable 
blah db "X12xxMXF",0 
fmt db "%d",0 

section '.code' readable executable 
start: 
        mov ecx, 0 
        mov edx, 0

theLoop: 
        cmp ecx, 8
        jne process  
        je done
         
process: 
        lea  eax, [ecx + blah]
        cmp eax, 88 ; is eax = 'x' ? 
        je addOne 
        cmp eax, 120 ; is eax = 'X' ?
        je addOne
        inc ecx
        jmp theLoop 

addOne:
        inc edx
        jmp theLoop
         
done: 
        cinvoke printf, fmt, edx
        invoke  ExitProcess, 0 

section '.idata' data import readable writeable 
library kernel32, 'kernel32.dll',  msvcrt, 'msvcrt.dll' 
import kernel32, ExitProcess, 'ExitProcess' 
import msvcrt, printf, 'printf' 
    


But my code yields 0. What's wrong here?
Post 02 Feb 2013, 08:38
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17667
Location: In your JS exploiting you and your system
revolution
I think you might want to use:
Code:
movzx eax,byte[ecx+blah]    
Instead of:
Code:
        lea  eax, [ecx + blah]    
Or you could use:
Code:
cmp byte[eax],88    
Or:
Code:
cmp byte[ecx+blah],88    
Post 02 Feb 2013, 08:41
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Or even cmp byte [ecx+blah], 'X'. Wink
Post 02 Feb 2013, 11:18
View user's profile Send private message Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 824
Location: Jakarta, Indonesia
TmX
@revolution
The program gets stuck if either "lea eax, [ecx + blah]" or "movxz eax,byte [ecx + blah]" is used

@baldr
That yields 0. Weird Confused
Post 02 Feb 2013, 14:29
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17667
Location: In your JS exploiting you and your system
revolution
Oh yeah. You'll have to add "inc ecx" in the addOne branch also.

That code needs to be rewritten, it jumps about too much and is difficult to follow.
Post 02 Feb 2013, 14:35
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Let me try:
Code:
        mov     esi, bla
        cld
        xor     eax, eax        ; EAX=0 -- To load characters from 'bla'
        xor     edx, edx        ; EDX=0 -- To count 'x' and 'X'

@@:
        lodsb                   ; AL<-[ESI] and ESI = ESI+1
        test    eax, eax
        jz      .print_counter  ; String ended with zero byte

        cmp     al, 'x'
        je      .count_me
        cmp     al, 'X'
        jne     @r

.count_me:
        inc     edx
        jmp     @r

.print_counter:
        ;
        ; ... print value in EDX here
        ;
    
Post 02 Feb 2013, 15:43
View user's profile Send private message Send e-mail Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 824
Location: Jakarta, Indonesia
TmX
@revolution
Still doesn't work. And why call "inc ecx" once again?
And sorry for the spaghetti code. I'll try rewrite it Embarassed
Post 02 Feb 2013, 15:47
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: Paradise Falls
Picnic
TmX hi,

The program enters an infinite loop when it meets x or X because in that case the string index (ecx) is not updated.
Post 02 Feb 2013, 16:15
View user's profile Send private message Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 824
Location: Jakarta, Indonesia
TmX
Picnic wrote:
TmX hi,

The program enters an infinite loop when it meets x or X because in that case the string index (ecx) is not updated.


Ah, yes you are right.
Thanks. Now it works. Smile
Post 02 Feb 2013, 16:55
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.