Simple string search function for finding 'wrdf' in 'sentc', copying from 'sentc' + 'fromoffs' to 'copyto'. This function copies only 'copylen' amount of bytes into 'copyto' or until it finds one of 5 bytes from 'until' in 'sentc' whilst copying. It will then put a 0 byte at the end to terminate the string and the value of how many byte offsets from the beginning of 'sentc' it found the matching word will be in eax. I'm a bit new to asm so tell me what u think or also any alternative or shorter ways of doing this

substrext proc STDCALL wrdf:dword, sentc:dword, copyto:dword, until:dword, fromoffs:dword, copylen:dword
mov ebx, sentc
cmp wrdf, NULL
je @@gofound
invoke _strlen, wrdf
mov , eax
mov esi, wrdf
mov ebx, sentc
mov cl, ; wrdf[0] to cx
mov edi, 0
mov eax, 0
jmp @@lop

@@gofound:
mov , 0
mov , ebx
jmp @@foundmatch

@@lop:
mov dl, ; sentc to dx
cmp cl, dl ; compare wrdf[0] to sentc
je @@found
inc ebx
inc eax
cmp dl, 0 ; if at end of string
je @@done2
jmp @@lop

@@found:
mov , ebx
mov , eax
mov eax,
inc ebx
inc esi

@@lop2:
mov cl, byte ptr
mov dl, byte ptr
cmp cl, 0
je @@foundmatch
cmp cl, dl
jne @@nomatch
inc ebx
inc esi
jmp @@lop2

@@nomatch:
mov esi, wrdf
mov ebx,
mov eax,
inc ebx
inc eax
mov edi, 0
mov cl, ; move wrdf[0] back into cx
jmp @@lop

@@foundmatch:
mov ebx,
mov esi, 0
mov edi,
mov ecx,
mov edx,
cmp ecx, 0
je @@done
cmp edx, 0
je @@done

@@copychars:
inc esi
cmp esi, ecx
je @@appo
dec esi
mov al, byte ptr ; sentc to ax

@@compare:
push ebx
mov ebx, until
cmp al, byte ptr
je @@appo2
cmp al, byte ptr
je @@appo2
cmp al, byte ptr
je @@appo2
cmp al, byte ptr
je @@appo2
cmp al, byte ptr
je @@appo2

@@continue:
mov ebx, edx
mov , al ; copy from sentc to copyto
cmp al, 0
je @@done

pop ebx
inc edi
inc esi
jmp @@copychars

@@appo:
pop ebx
mov al, 0
dec esi
mov , al
jmp @@done

@@appo2:
pop ebx
mov ebx, edx
mov al, 0
mov , al

@@done:
mov eax,
ret

@@done2:
mov eax, -1
ret

substrext endp
Posted on 2001-08-30 15:51:49 by nin
nin,

you have asked so dont complain: if your code works never touch it again. Its what was called "spaghetti" code some years ago (dont know how its called in these days).

japheth
Posted on 2001-09-02 07:52:29 by japheth
Thanx for replying! Can you just elaborate a bit on what u mean by spagetti code in my code! I just wanna get more of an idea of how things 'should' be coded in asm. Thanx!
Posted on 2001-09-02 12:30:39 by nin
spaghetti coding is usually when you use a lot of "gotos" or in this case jmps, instead of making it use a loop to make it more structured and easier to read. It's spaghetti code because it's hard to go from beginning to end, you get lost in a maze.

Of course, in assembly, there is nothing wrong with jmp, but your code is a bit tough to follow..
Posted on 2001-09-02 14:43:34 by vcv
nin,

I would not worry too much about the claim of spaghetti code, this is assembler where jumps are normal so if it works well it does not matter, the alternative is high level languages with arbitrarily structured loops that do not perform in most instances.

Just a hint to make you code more readable, using labels that have an intelligible name is a good thing, also try and side comment your code so that you dont have to try and remember the logic some months down the track.

With a bit of practice, you may be able to optimise the code a bit more and reduce the number of jumps in it as this will usually make the code faster.

One thing to stay away from is any of ther older pseudo high level instructions like LOOP, LOOPNZ, JCXZ as they are very slow and can be easily improved on with manual jumps and labels.

Regards,

hutch@pbq.com.au
Posted on 2001-09-02 18:30:20 by hutch--
Thanx everyone! One last question. What would you say are the main components to good asm coding? Should there be less jumps etc.
Posted on 2001-09-03 12:59:27 by nin
I think small pieces of code are best. You should be able to look at it in ten years and know in an instant how it does what it does. If you can't, then it needs more documentation. I am sorry to say that I don't always follow this, but I just posted a reminder on my monitor to that effect - so, maybe I can do better in the future. :)
Posted on 2001-09-03 23:32:06 by bitRAKE
nin,

when saying "spaghetti" code I dont mean jumps should be avoided (although it is possible in MASM with the "dot" commands .while, .repeat,...). But one should be able to recognize the control flow of the code without too much effort. May be I'm preaching but

- avoid to jump in a loop from outside
- jumps out of a loop only to the start or end of it (break/continue)

And if you really like spaghetti, avoid "pushing" regs inside your code, for as it can easily be seen you forgot to "pop" ebx when control flow goes from @@compare to @@continue to @@done (which is no real problem here because the "ret" cleans the stack).

japheth
Posted on 2001-09-04 03:50:40 by japheth
ret cleans the stack? I never tried it, but that sure would suck if it did...
Posted on 2001-09-04 03:59:06 by Kenny
Kenny,

look at the listing file (use command line option /Sg to create a listing with macro expansion). You will see that "ret" is expanded by masm (at least) to:



mov esp,ebp
pop ebp
ret


so the stack is in fact "clean" afterwards. But thats only true in this concrete situation. When the proc would be defined with "uses esi", the previous value of esi will be lost when doing a "ret" with "pops" outstanding.

japheth
Posted on 2001-09-04 06:15:38 by japheth
ohhh ok.... *whew*

I don't program in MASM, so I don't have to worry about it :)
Posted on 2001-09-04 06:46:07 by Kenny