flat assembler
Message board for the users of flat assembler.

Index > Main > explain how work pcmpestri

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1878
Roman 10 Jul 2013, 17:38
This code work. But i need compare like this (Txt="Text ok next bla bla" and NeedFound="Text ok",NULL)
I want found NeedFound in Txt.
Code:
align 16                
s1    db "This is a string!ss",0
s2    db "This is a string!ss",0

Call CmpStrings
invoke  ExitProcess,0

proc CmpStrings
        mov     eax, s2    
        mov     edx, s1
StringCMP: 
        sub     eax, edx         
        sub     edx, 16         
 
strcmpLoop:
        add     edx, 16
        movups  xmm0, [edx]       
        pcmpistri       xmm0, [edx+eax],0011000b       
        ja      strcmpLoop
        jc      strcmpDiff  
        invoke  MessageBox,0,"Text !","Found !",0        
        sub     eax, eax        
        ret
 
strcmpDiff:     
        invoke  MessageBox,0,"Text","Not found",0 
        ret
endp
    


Last edited by Roman on 10 Jul 2013, 17:47; edited 1 time in total
Post 10 Jul 2013, 17:38
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1878
Roman 10 Jul 2013, 17:44
I do not understand how they work EDX and EAX registers in pcmpistri .
Maybe someone can describe steps the procedure ptsmpistri , using simple commands?
Post 10 Jul 2013, 17:44
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 10 Jul 2013, 18:19
Check out this link:
http://www.strchr.com/strcmp_and_strlen_using_sse_4.2

This section describes what you want:
Equal ordered (imm[3:2] = 11). Substring search (strstr). The first operand contains a string to search for, the second is a string to search in. The bit mask includes 1 if the substring is found at the corresponding position:

operand2 = "WhenWeWillBeWed!", operand1 = "We"
IntRes1 = 000010000000100

There's also code posted to do just that:
Code:
; compile with FASM
; Immediate byte constants
EQUAL_ANY           = 0000b
RANGES              = 0100b
EQUAL_EACH          = 1000b
EQUAL_ORDERED       = 1100b
NEGATIVE_POLARITY = 010000b
BYTE_MASK        = 1000000b

; ==== strstr ====
strstr_sse42:
  ; ecx = haystack, edx = needle

  push esi
  push edi
  MovDqU xmm2, dqword[edx] ; load the first 16 bytes of neddle
  Pxor xmm3, xmm3
  lea eax, [ecx - 16]

  ; find the first possible match of 16-byte fragment in haystack
STRSTR_MAIN_LOOP:
    add eax, 16
    PcmpIstrI xmm2, dqword[eax], EQUAL_ORDERED
    ja STRSTR_MAIN_LOOP

  jnc STRSTR_NOT_FOUND

  add eax, ecx ; save the possible match start
  mov edi, edx
  mov esi, eax
  sub edi, esi
  sub esi, 16

  ; compare the strings
@@:
    add esi, 16
    MovDqU    xmm1, dqword[esi + edi]
    ; mask out invalid bytes in the haystack
    PcmpIstrM xmm3, xmm1, EQUAL_EACH + NEGATIVE_POLARITY + BYTE_MASK
    MovDqU xmm4, dqword[esi]
    PAnd xmm4, xmm0
    PcmpIstrI xmm1, xmm4, EQUAL_EACH + NEGATIVE_POLARITY
    ja @B

  jnc STRSTR_FOUND

  ; continue searching from the next byte
  sub eax, 15
  jmp STRSTR_MAIN_LOOP

STRSTR_NOT_FOUND:
  xor eax, eax

STRSTR_FOUND:
  pop edi
  pop esi
  ret
    
Post 10 Jul 2013, 18:19
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.