News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

substr() function in MASM?

Started by SCHiM, October 10, 2010, 10:16:59 AM

Previous topic - Next topic

SCHiM

Hello everyone,
I've just joined this form in to learn assembly
I'm currently coding a few task set out for me (by myself...)
And I need to be able to take a substring (this being an array of bytes) and copy that substring into another buffer
But I've no idea on how to split a string in two. Does anyone have any tips for me, or even a working example to illustrate the idea?
Here's how it should look in pseudo code:

string str "This is some string"  ; initialize our string
string buffer (?)    ; out buffer
int someloc = find("some", str)  ;find and copy the location of "some" inside string str and save it in someloc
buffer = copy substr(str, someloc, sizeof str); copy the contents of str, starting at someloc, with the size of str into buffer
 

Thanks in advance
-SCHiM







Vortex

Hi SCHiM,

Here is a quick example for you. To get information about MASM32 library functions :

\masm32\help\masmlib.chm


.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\masm32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib

START_POS   equ 1   ; Starting position to search for the substring = 1

.data

szMainStr   db 'The MASM forum',0   ; String to be searched
szSubStr    db 'MASM',0             ; Substring to search
                                    ; for within the main string

capt        db 'Copied substring',0


.data?

buffer      db 100 dup(?)

.code

start:

    invoke  InString,START_POS,ADDR szMainStr,ADDR szSubStr

                                    ; search for the substring "MASM"

    ; If the function succeeds
    ; it returns the 1 based index of the start
    ; of the substring or 0 if no match is found.

    dec     eax                     ; 1 based index -> 0 based index

    add     eax,OFFSET szMainStr

    invoke  lstrcpyn,ADDR buffer,eax,10 + 1

                                    ; 10 = sizeof(MASM forum) , 1 = sizeof(NULL)

    invoke  MessageBox,0,ADDR buffer,ADDR capt,MB_OK
         
    invoke  ExitProcess,0

END start

SCHiM

Whoa, thank you Vortex!
That was exactly what I was looking for!!

But I have a question
What does this mean:

Quote    ; it returns the 1 based index of the start
    ; of the substring or 0 if no match is found.

Nevermind, It returns a string starting at the string that was to be found +1 right?
So if I deleted the line, dec eax, it yould return ASM forum right?

oex

Quote from: SCHiM on October 10, 2010, 11:55:54 AM
So if I deleted the line, dec eax, it yould return ASM forum right?

Yep that's right
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Vortex

Hi SCHiM,

You are right. Removing the line dec eax will return ASM forum. In daily programming practice, you use 0 based indexes to manipulate NULL terminated strings. This is why eax is decremented by one.