flat assembler
Message board for the users of flat assembler.

Index > Windows > Direction flag problem

Author
Thread Post new topic Reply to topic
marcinzabrze12



Joined: 07 Aug 2011
Posts: 60
marcinzabrze12
Hello All. Everything is ok when i use direction flag to scan string forward, but when i change diection program is crashed:
there is an example:
Code:
        proc    pathToName      uses ebx esi edi, ppath
            mov         edi, [ppath]
            xor         eax, eax
            mov         ecx, -1
            cld
            repne       scasb
            mov         al, '\'      ; edi = end of path so gona find first '\'
            std                        ; change direction
            repne       scasb    
            mov         eax, edi ; here edi shoul be show on '\'  or not ???
            inc         eax
            ret
        endp  
    


now i'm fixed problem in this way:
Code:
        proc    pathToName      uses ebx esi edi, ppath
            mov         edi, [ppath]
            xor         eax, eax
            mov         ecx, -1
            cld
            repne       scasb
            mov         al, '\'
           @@:
            dec         edi
            cmp         [edi], al
            je          @f
            jmp         @b
           @@:
            mov         eax, edi
            inc         eax
            ret
        endp    
    


... but i prefer first one metod because it faster.
someone know what's wrong ?
Post 06 Apr 2013, 08:32
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3043
Location: vpcmipstrm
bitRAKE
There are a couple things to note: first is that EDI will move one byte lower than you expect. This is because the order of operations are reversed from the forward direction method: compare, de-increment - verses - increment, compare.

It is also important to prevent buffer under-run. Commonly, this is done like so:
Code:
    db "\"
pMyPath db "PathFileName",0    
... or simply: mov byte[edi],"\" ; at start of routine. Another method would be to update ECX once string length is known.

Otherwise, algorithm seemed to work in debugger.

_________________
¯\(°_o)/¯ unlicense.org
Post 06 Apr 2013, 11:13
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
Isn't the lines:
Code:
@@:
...
je @f
jmp @b

@@:
...
    

same as these lines?
Code:
@@:
...
jne @b

; --> no need for that --> @@:
...
    
Post 07 Apr 2013, 01:48
View user's profile Send private message Send e-mail Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 709
Location: Adelaide
sinsi
Don't forget that Windows expects the direction flag to be clear, so if you call a Windows function after that code it may crash. If you use STD you should always finish with CLD.
Post 07 Apr 2013, 03:00
View user's profile Send private message Reply with quote
marcinzabrze12



Joined: 07 Aug 2011
Posts: 60
marcinzabrze12
sinsi wrote:
Don't forget that Windows expects the direction flag to be clear, so if you call a Windows function after that code it may crash. If you use STD you should always finish with CLD.


THANK YOU. I just do not know it and i was meed this problem many times before.
Realy problems with optimization are important too but it doesn't meaning when function crasheed.
Post 07 Apr 2013, 11:32
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.