flat assembler
Message board for the users of flat assembler.

Index > Windows > InStr, InStrRev & NumberToString

Author
Thread Post new topic Reply to topic
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
I'm trying to use these functions in FASM, but my attemps keep causing major errors. Anyone know how to do any of these?

Many thanks,

cod3b453


Last edited by cod3b453 on 19 Feb 2005, 21:28; edited 1 time in total
Post 19 Feb 2005, 18:25
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
NOT OPTIMIZED INSTR FUNCTION

InStr:
.MainStr equ ebp + 8 ;main string ptr
.Search equ ebp + 12 ;search string ptr
.LenSearch equ ebp + 16;length of search string
push ebp
mov ebp,esp
push esi
push edi
mov edx,[.MainStr]
sub edx,1
xor eax,eax
sub eax,1
.LOOP:
add edx,1
mov esi,edx
cmp byte[esi],0
je .NOMATCH
mov edi,[.Search]
mov ecx,[.LenSearch]
repe cmpsb
cmp ecx,0
jne .LOOP
sub edx,[.MainStr]
mov eax,edx
jmp .END
.NOMATCH:
mov eax,-1
.END:
pop edi
pop esi
mov esp,ebp
pop ebp

EAX = -1 if no match
EAX = string index of start of where search string appears
Post 19 Feb 2005, 21:00
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
For NumberToString use the wsprintf api found in the user32.dll
.data
formatStr db '%lu',0 ; make the number an unsigned long
MainStr rb 0ffh
.code
Main:
push ebp
mov ebp,esp
mov eax,0ffh ;eax holds the binary number 255
push eax
push dword formatStr
push dword MainStr
call [wsprintf]
;MainStr = "255",0
mov esp,ebp
pop ebp
retn 0
Post 19 Feb 2005, 21:06
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Thanks r22!

I understand the NumberToString (I'd never heard of that API), but I don't follow the InStr function - I also need another argument of what position to start searching from:

e.g. proc InStr,StartPos,Search,Find,FindLen

and because I don't understand what's going on I can't adapt it Sad

Thanks again,

cod3b453
Post 19 Feb 2005, 21:54
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
I fixed up my InStr function and added comments for you. This is a full working code example.
Code:
format PE GUI 4.0 
entry Start 

include '%fasminc%\win32a.inc' 

section '.data' data readable writeable 
formatstr db '<1> %lu  <2> %lu  <3> %lu  <4> %lu',0 
searchstr db 'hello world 1 2 3',0 
findstr1 db 'ello',0 
findstr2 db ' 3',0 
findstr3 db '1 2',0 
findstr4 db 'zworld',0 
buffr rd 0ffh 

section '.code' code readable executable 
Start: 
push ebp 
mov ebp,esp 

push 6 
push dword findstr4 
push dword searchstr 
call InStr 
push eax ;save it 

push 3 
push dword findstr3 
push dword searchstr 
call InStr 
push eax 

push 2 
push findstr2 
push searchstr 
call InStr 
push eax 

push 2 
push findstr1 
push searchstr 
call InStr 
push eax 
;on stack 
;findstr1's result 
;...findstr4's result 
push formatstr 
push buffr 
call [wsprintf] 
push 0 
push buffr 
push buffr 
push 0 
call [MessageBox] 
mov esp,ebp 
pop ebp 
retn 0 

InStr: 
.MainStr equ ebp+8 ;main string ptr 
.Search equ ebp+12 ;search string ptr 
.LenSearch equ ebp+16 ;length of search string 
push ebp 
mov ebp,esp 
push esi 
push edi 
;the above just makes the procedure 
;just like the PROC macro would 
     mov edx,dword[.MainStr] 
     ;storing str address 
     dec edx 
     ;subtracting 1 from the address 
.LOOP: 
     add edx,1 
     ;adding 1 to the address 
     mov esi,edx 
     ;copy MainStr address into ESI 
     cmp byte[esi],0 
     ;check the string byte for null 
     je .NOMATCH 
     ;if its NULL we reached the end 
     ;of the search with NO results 
     ;so jump to the failure label 
     mov edi,dword[.Search] 
     ;copy address of the string to find 
     ;into EDI 
     mov ecx,dword[.LenSearch] 
     add ecx,1 
     ;copy length of string to find 
     ;into ECX and add 1 
     repe cmpsb 
     ;use slow string comparison opcode 
     ;dec ecx, cmp byte[esi],byte[edi], 
     ;inc esi,inc edi repeat it while equal 
     cmp ecx,0 
     ;if ecx = 0 success 
     jne .LOOP 
     ;if its not 0 loop again 
     mov eax,edx 
     ;put the offset address of where 
     ;the string was found into EAX 
     mov edx,dword[.MainStr] 
     ;put the address of the MainStr 
     ;into EDX 
     sub eax,edx 
     ;subtract offset from base to 
     ;get the index 
     ;first char = 0 2nd char = 1 etc. 
     jmp .END 
     ;skip the failure 
.NOMATCH: 
     mov eax,-1 
     ;couldnt find the string so 
     ;EAX = -1 failure 
.END: 
;the follow is like the EndProc macro 
pop edi 
pop esi 
mov esp,ebp 
pop ebp 
retn 0ch 

section '.idata' import data readable writeable 

  library kernel32,'KERNEL32.DLL',\ 
          user32,'USER32.DLL' 

      include  "%fasminc%\apia\kernel32.inc" 
      include  "%fasminc%\apia\user32.inc" 


section '.reloc' fixups data discardable 
    
Post 20 Feb 2005, 04:49
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
Check also the library "strlib.asm" from Fresh ( http://fresh.flatassembler.net ) - it is pretty rich string library, that allows working with dynamic strings automatically allocating needed memory. It is documented by Decard in the file: fresh/doc/FreshGuide.chm

Regards.
Post 20 Feb 2005, 08:15
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Thanks r22!

I managed to modify that to do what I wanted:
Code:
proc InStr,Start,Search,Find

        enter
            push esi edi

                mov edx,[Search]
                dec edx
                add edx,[Start]        ; Set start position

            InStr_Do:

                inc edx
                mov esi,edx

                cmp byte [esi],0
                je InStr_Fail

                mov edi,[Find]
                invoke lstrlenA,[Find]
                mov ecx,eax

                inc ecx
                repe cmpsb

                cmp ecx,0
                jne InStr_Do

                mov eax,edx
                sub eax,[Search]
                inc eax                ; 1st char = 1; 2nd char = 2; ...

                jmp InStr_Exit

            InStr_Fail:

                mov eax,0              ; InStr returns 0 if not found

            InStr_Exit:

            pop edi esi
        return 
    
Post 20 Feb 2005, 12:18
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.

Powered by rwasa.