flat assembler
Message board for the users of flat assembler.

Index > Main > strstrip Code

Author
Thread Post new topic Reply to topic
pal



Joined: 26 Aug 2008
Posts: 227
pal 10 Jul 2009, 11:49
Hey I made an strstrip code i.e. a code that takes in a string, only allows certain characters to be in the string and outputs the string. I was just wondering if anyone could comment on how I could make it better etc.

I was thinking comparing multiple bytes at once would make it faster, maybe using a rep for the first @@ loop (but I couldn't think of the right one to use) etc.

Code:
proc        strstrip,szAllowed,szIn,szOut
       push    edi
 push    esi
 mov             edx,[szAllowed]
     mov             esi,[szIn]
  mov             edi,[szOut]
 mov             ecx,-1
removeLoop:
   add             ecx,1
       mov             al,byte [esi+ecx]
   test    al,al
       jz              removeEnd
   push    ecx
 mov             ecx,-1
      
    @@: add             ecx,1
               cmp             byte [edx+ecx],0
            jz              @F
          cmp             byte [edx+ecx],al
           jne             @B
          stosb   
@@:     pop             ecx
 jmp             removeLoop
  
removeEnd:
  xor             eax,eax
     xor             ecx,ecx
     pop             esi
 pop             edi
 ret
endp
    


Cheers for any comments, pal.
Post 10 Jul 2009, 11:49
View user's profile Send private message Reply with quote
eskizo



Joined: 22 Nov 2005
Posts: 59
eskizo 10 Jul 2009, 13:40
Code:
; At least, remove the last loop and put this:
add esp, ecx    
Post 10 Jul 2009, 13:40
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 16 Jul 2009, 08:27
pal,

Having strlen(szAllowed) computed beforehand can reduce inner loop to repne scasb:
Code:
        xor     eax, eax
        mov     ecx, -1
        mov     edi, szAllowed
  repne scasb
        not     ecx             ;note that ecx == strlen(szAllowed)+1
        mov     edx, ecx

        mov     esi, szIn
        mov     ebx, szOut

next:   lodsb
        mov     ecx, edx
        mov     edi, szAllowed
  repne scasb                   ;this scans szAllowed with trailing \0
        jnz     next
        mov     [ebx], al       ;here we come if character in szAllowed (\0 too)
        inc     ebx
        test    ecx, ecx        ;ecx will be 0 iff character was \0
        jnz     next

done:    
Probably bit map of allowed characters (32 bytes) and bt instruction will eliminate the need to scan szAllowed for each character in szIn.
Post 16 Jul 2009, 08:27
View user's profile Send private message Reply with quote
pal



Joined: 26 Aug 2008
Posts: 227
pal 16 Jul 2009, 09:14
Cheers baldr. I dunno why I didn't use a repne scasb in the first place, it must have just skipped my mind.
Post 16 Jul 2009, 09:14
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.