flat assembler
Message board for the users of flat assembler.
Index
> Windows > InStr, InStrRev & NumberToString |
Author |
|
r22 19 Feb 2005, 21:00
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 |
|||
19 Feb 2005, 21:00 |
|
r22 19 Feb 2005, 21:06
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 |
|||
19 Feb 2005, 21:06 |
|
cod3b453 19 Feb 2005, 21:54
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 Thanks again, cod3b453 |
|||
19 Feb 2005, 21:54 |
|
r22 20 Feb 2005, 04:49
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 |
|||
20 Feb 2005, 04:49 |
|
JohnFound 20 Feb 2005, 08:15
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. |
|||
20 Feb 2005, 08:15 |
|
cod3b453 20 Feb 2005, 12:18
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 |
|||
20 Feb 2005, 12:18 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.