Author Topic: strcmp with wildcards  (Read 488 times)

Queue

  • Regular Member
  • *
  • Posts: 13
strcmp with wildcards
« on: August 13, 2017, 08:35:33 AM »
A recent thread in here reminded me of a time I needed simple wildcard support (specifically * and ?) for a rough lstrcmpA equivalent. While this doesn't handle all sorts of special circumstances, it did its job, but it always bugged me that I had a jump and label when the rest of the function was able to use .if/.repeat/etc. Anyone have any suggestions? Or is it just screwed because MASM doesn't let you break out of multiple loops (or does it and I've just never figured out the syntax)?

Code: [Select]
include \masm32\include\masm32rt.inc

_lstrcmpA_wildcard PROTO :DWORD,:DWORD

; int 3 align
align_int3 macro _:REQ
local $$, $$$
$$ equ $
align _
$$$ equ $ - $$
if $$$
org $$
db $$$ dup(0CCh)
endif
endm

; strip prologue
prologue_none macro
local $$
$$ db 0CCh
nop
org $$
endm

.data

align_int3 16
sResult db "0 == match",0Ah,"1 == no match",0Ah,0Ah
sValue dd "0"

align_int3 16
sString1 db "This is the string to search.",0

align_int3 16 ; expect no match
sString2a db "*the string x*",0

align_int3 16 ; expect match
sString2b db "*the?string?t*",0

align_int3 16 ; expect no match (*is matches too early)
sString2c db "*is the strin*",0

align_int3 16 ; expect match
sString2d db "*is*the strin*",0

align_int3 16 ; expect no match (* escapes ?)
sString2e db "*?",0

align_int3 16 ; expect match
sString2f db "*.",0

align_int3 16

;~========================================================================================

.code

align_int3 16
EntryPoint proc <forceframe> uses ebx esi edi
invoke _lstrcmpA_wildcard, offset sString1, offset sString2a
call showmsg

invoke _lstrcmpA_wildcard, offset sString1, offset sString2b
call showmsg

invoke _lstrcmpA_wildcard, offset sString1, offset sString2c
call showmsg

invoke _lstrcmpA_wildcard, offset sString1, offset sString2d
call showmsg

invoke _lstrcmpA_wildcard, offset sString1, offset sString2e
call showmsg

invoke _lstrcmpA_wildcard, offset sString1, offset sString2f
call showmsg

exit
ret
EntryPoint endp

;~........................................................................................

align_int3 16
showmsg proc
.if eax == 0
mov sValue, "0"
.else
mov sValue, "1"
.endif
invoke MessageBoxA, NULL, offset sResult, NULL, MB_OK
ret
showmsg endp

;~........................................................................................

align_int3 16
_lstrcmpA_wildcard proc lpString1:DWORD, lpString2:DWORD
prologue_none
mov edx, [esp + DWORD * 2] ; lpString2
mov ecx, [esp + DWORD * 1] ; lpString1
.while TRUE
mov al, byte ptr [edx]
.if al == "*"
.repeat
inc edx
mov al, byte ptr [edx]
.until al != "*"
.break .if !(al & al) ; al == 0
.repeat
mov ah, byte ptr [ecx]
; .break .if !(ah & ah) ; ah == 0
test ah, ah
jz break
inc ecx
.until ah == al
.else
mov ah, byte ptr [ecx]
.break .if !(ah & ah) ; ah == 0
.if !(al & al) ; al == 0
xchg al, ah
.break
.endif
.break .if ah != al && al != "?"
inc ecx
.endif
inc edx
.endw
break: movzx eax, al
retn DWORD * 2
_lstrcmpA_wildcard endp

;~........................................................................................

align_int3 16

end EntryPoint

The example strings aren't comprehensive, but do cover quirks like "*?". I realize naming it _lstrcmpA_* isn't great since it doesn't match lstrcmpA behavior, but in my defense, when it was written, it matched behavior in the specific circumstance it was designed for.

Queue

felipe

  • Member
  • ***
  • Posts: 378
  • assemble the unassembled.
Re: strcmp with wildcards
« Reply #1 on: August 13, 2017, 10:03:38 AM »
but it always bugged me that I had a jump and label when the rest of the function was able to use .if/.repeat/etc. Anyone have any suggestions?

You can use only jumps and labels.

 :biggrin:
Felipe.

jj2007

  • Member
  • *****
  • Posts: 7764
  • Assembler is fun ;-)
    • MasmBasic
Re: strcmp with wildcards
« Reply #2 on: August 13, 2017, 10:23:55 AM »
There are no "multiple breaks", but you can always jump to a label, as you have done in your code. The stack should be balanced, of course.

You show some interesting programming techniques :t

The official way of skipping the stack frame is this one, though:
Code: [Select]
OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE
align_int3 16
_lstrcmpA_wildcard proc lpString1:DWORD, lpString2:DWORD
; prologue_none
...
retn DWORD * 2
_lstrcmpA_wildcard endp
OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

If you use UAsm, you don't even need that step: UAsm determines automatically that no stack frame is needed.

Btw can you give a short real life example where to use this wildcard comparison? I have never thought of it, just curious how it could be applied.

Queue

  • Regular Member
  • *
  • Posts: 13
Re: strcmp with wildcards
« Reply #3 on: August 13, 2017, 11:24:35 AM »
Quote from: jj2007
The official way of skipping the stack frame is...
Right, notice the macro is named to mimic the option. Sometimes declaring and then redeclaring the prologue option is a pain in the butt. Note that this macro causes issues if there's a relocatable address in an instruction in the proc.
Quote from: jj2007
Btw can you give a short real life example where to use this wildcard comparison? I have never thought of it, just curious how it could be applied.
Any time you want to check if part of a string matches your search criteria without needing a complete match. I guess an example could be: use with EnumWindows and GetWindowTextA to find a window whose title contains both the program's name and a filename. It's an imperfect example because there'd be better ways to find said window, and you'd be better off using unicode functions if you resorted to a text search, but that's beside the point. It'd arguably be better (more precise) than using "strstr" and way less expensive than a RegExp implementation.

Regarding UASM, neat, though I rely on some weirdness of MASM for an everyday macro I use that I'm fairly certain UASM doesn't (and likely wouldn't) support. I'll post about that in a new topic some time in the future (it involves option oldmacros).

Queue

nidud

  • Member
  • *****
  • Posts: 1411
    • https://github.com/nidud/asmc
Re: strcmp with wildcards
« Reply #4 on: August 13, 2017, 12:19:26 PM »
Asmc is an attempt to remove labels. I have a similar function with the following test.

Code: [Select]
cmpwarg PROC uses esi path:LPSTR, wild:LPSTR

    mov esi,path
    mov ecx,wild
    xor eax,eax

    .while 1

        lodsb
        mov ah,[ecx]
        inc ecx

        .if ah == '*'

            .while 1
                mov ah,[ecx]
                .if !ah
                    mov eax,1
                    .break(1)
                .endif
                inc ecx
                .continue .if ah != '.'
                xor edx,edx
                .while al
                    .if al == ah
                        mov edx,esi
                    .endif
                    lodsb
                .endw
                mov esi,edx
                .continue(1) .if edx
                mov ah,[ecx]
                inc ecx
                .continue .if ah == '*'
                test eax,eax
                mov  ah,0
                setz al
                .break(1)
            .endw

        .endif

        mov edx,eax
        xor eax,eax
        .if !dl
            .break .if edx
            inc eax
            .break
        .endif
        .break .if !dh
        .continue .if dh == '?'
        .if dh == '.'
            .continue .if dl == '.'
            .break
        .endif
        .break .if dl == '.'
        or edx,0x2020
        .break .if dl != dh
    .endw
    test eax,eax
    ret

cmpwarg ENDP

Code: [Select]
main proc

    .assert( cmpwarg("file",     "*"        ) == 1 )
    .assert( cmpwarg("file",     "*.*"      ) == 1 )
    .assert( cmpwarg("file",     "f*.*"     ) == 1 )
    .assert( cmpwarg("file",     "file*"    ) == 1 )
    .assert( cmpwarg("file.c",   "file.?"   ) == 1 )
    .assert( cmpwarg("file.c",   "file.??"  ) == 0 )
    .assert( cmpwarg("file.c",   "???.?"    ) == 0 )
    .assert( cmpwarg("file.c",   "????.?"   ) == 1 )
    .assert( cmpwarg("file.c",   "file.c*"  ) == 1 )
    .assert( cmpwarg("file.c",   "file.c?"  ) == 0 )
    .assert( cmpwarg("file.x.c", "*.c"      ) == 1 )
    .assert( cmpwarg("file.x.c", "????.*.c" ) == 1 )
    .assert( cmpwarg("file.x.c", "????.*.b" ) == 0 )
    .assert( cmpwarg("file.x.c", "*.?.b"    ) == 0 )
    .assert( cmpwarg("file.x.c", "*.*.b"    ) == 0 )
    .assert( cmpwarg("file.x.c", "*.*.c"    ) == 0 )
    .assert( cmpwarg("file.x.c", "*.?.c"    ) == 0 )
    .assert( cmpwarg("file.x.c", "*?x.c"    ) == 1 )
    .assert( cmpwarg("file.ext", "*"        ) == 1 )
    .assert( cmpwarg("file.prj", "*.*"      ) == 1 )
    .assert( cmpwarg("file.ext", "x*.*"     ) == 0 )
    .assert( cmpwarg("ab39.ext", "?B39.??T" ) == 1 )
    .assert( cmpwarg("abcd.ext", "?b?.?x?"  ) == 0 )
    .assert( cmpwarg("abcd.ext", "?b*.?x?"  ) == 1 )
    .assert( cmpwarg("abcd.ext", "?b*.?z?"  ) == 0 )

    xor eax,eax
    ret

main endp

aw27

  • Member
  • ****
  • Posts: 863
  • Let's Make ASM Great Again!
Re: strcmp with wildcards
« Reply #5 on: August 13, 2017, 05:47:11 PM »
it always bugged me that I had a jump and label when the rest of the function was able to use .if/.repeat/etc

In this case a function call will alleviate the pain of seeing a jump and label.
Something like this:

Code: [Select]
;~........................................................................................

proc1 proc private
.repeat
mov ah, byte ptr [ecx]
test ah, ah
.if ZERO?
mov edx, 0
ret
.endif
inc ecx
.until ah == al
ret
proc1 endp

align_int3 16
_lstrcmpA_wildcard proc lpString1:DWORD, lpString2:DWORD
prologue_none
mov edx, [esp + DWORD * 2] ; lpString2
mov ecx, [esp + DWORD * 1] ; lpString1
.while TRUE
mov al, byte ptr [edx]
.if al == "*"
.repeat
inc edx
mov al, byte ptr [edx]
.until al != "*"
.break .if !(al & al) ; al == 0

call proc1
.break .if edx==0
.else
mov ah, byte ptr [ecx]
.break .if !(ah & ah) ; ah == 0
.if !(al & al) ; al == 0
xchg al, ah
.break
.endif
.break .if ah != al && al != "?"
inc ecx
.endif
inc edx
.endw
movzx eax, al
retn DWORD * 2
_lstrcmpA_wildcard endp

;~........................................................................................

aw27

  • Member
  • ****
  • Posts: 863
  • Let's Make ASM Great Again!
Re: strcmp with wildcards
« Reply #6 on: August 13, 2017, 06:10:59 PM »
Another (better) way:

Code: [Select]
;~........................................................................................

align_int3 16
_lstrcmpA_wildcard proc lpString1:DWORD, lpString2:DWORD
prologue_none
mov edx, [esp + DWORD * 2] ; lpString2
mov ecx, [esp + DWORD * 1] ; lpString1
.while TRUE
mov al, byte ptr [edx]
.if al == "*"
.repeat
inc edx
mov al, byte ptr [edx]
.until al != "*"
.break .if !(al & al) ; al == 0
.repeat
mov ah, byte ptr [ecx]
; .break .if !(ah & ah) ; ah == 0
test ah, ah
.break .if ZERO?
inc ecx
.until (ah == al)
.break .if  (ah!=al)
.else
mov ah, byte ptr [ecx]
.break .if !(ah & ah) ; ah == 0
.if !(al & al) ; al == 0
xchg al, ah
.break
.endif
.break .if ah != al && al != "?"
inc ecx
.endif
inc edx
.endw
       movzx eax, al
retn DWORD * 2
_lstrcmpA_wildcard endp

;~........................................................................................

jj2007

  • Member
  • *****
  • Posts: 7764
  • Assembler is fun ;-)
    • MasmBasic
Re: strcmp with wildcards
« Reply #7 on: August 13, 2017, 06:18:06 PM »
involves option oldmacros

To my knowledge, this is not implemented in UAsm. But most probably your macro needs only a minor modification to work without that option (I am curious to see the new thread). One reason why UAsm64 is my default nowadays is speed: It makes a real difference if you have to wait 3 (UAsm) or 10 (ML) seconds for your build to complete...

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4936
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: strcmp with wildcards
« Reply #8 on: August 14, 2017, 12:10:36 AM »
Queue,

If you have the MASM32 distribution, have a look at one of the library routines called "partial.asm". It does not do what you are doing but it is doing a similar things, scanning a text for partial matches.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: