The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: nikola99 on May 19, 2010, 05:22:26 PM

Title: Find the longest sentence in a string.
Post by: nikola99 on May 19, 2010, 05:22:26 PM
Hello everybody. I need to find the longest sentence in a string, for example "Abc. Abcd. Abcde". Does anyone have any idea how to do this?
Title: Re: Find the longest sentence in a string.
Post by: clive on May 19, 2010, 05:37:22 PM
Yes, several.

Post the code you have so far, we don't do homework assignments.
Title: Re: Find the longest sentence in a string.
Post by: oex on May 19, 2010, 05:46:48 PM
:lol nice 1 clive I nearly got suckered into that one

EDIT: I wrote the proc if you have a good start :lol
Title: Re: Find the longest sentence in a string.
Post by: qWord on May 19, 2010, 05:50:20 PM
.data
szTest db "Abc. Abcd. Abcde",0
.code

lea esi,szTest
movzx eax,BYTE ptr [esi]
mov ebx,esi
mov edi,esi
xor edx,edx
xor ecx,ecx
.while 1
lea esi,[esi+1]
.if eax == '.' || !eax
.if edx > ecx
mov ecx,edx
mov edi,ebx
.endif
xor edx,edx
mov ebx,esi
.break .if !eax
.else
lea edx,[edx+1]
.endif
movzx eax,BYTE ptr [esi]
.endw
; edi = pointer to largest sentence
; ecx = size of sentence
Title: Re: Find the longest sentence in a string.
Post by: oex on May 19, 2010, 05:51:26 PM
:lol
Title: Re: Find the longest sentence in a string.
Post by: dedndave on May 20, 2010, 03:03:25 PM
if he hands that in, qWord, his instructor will know she(he?) didn't write it   :P
Title: Re: Find the longest sentence in a string.
Post by: joemc on May 20, 2010, 07:46:38 PM
i am confused. why lea esi, [esi+1],  is it faster ?

edit: Thinking about it, it actually makes more sense than an add.
Title: Re: Find the longest sentence in a string.
Post by: clive on May 20, 2010, 09:19:34 PM
Plus it can be really useful if you don't want to effect the flags, especially the carry ie lea esi,[esi+4].
Title: Re: Find the longest sentence in a string.
Post by: dedndave on May 20, 2010, 10:28:57 PM
inc esi might be better
if the step were more than one, i might use lea
inc does affect the flags - but always leaves the carry flag unchanged
inc reg32 is a single byte instruction - this can make the overall loop smaller - which is the real advantage
Title: Re: Find the longest sentence in a string.
Post by: qWord on May 21, 2010, 12:35:38 AM
Quote from: dedndave on May 20, 2010, 03:03:25 PM
if he hands that in, qWord, his instructor will know she(he?) didn't write it   :P
what would his instructor then said to this this (obfuscated) code ? :cheekygreen::

lea esi,szTest
movzx eax,BYTE ptr [esi]
mov ebx,esi
mov edi,esi
xor edx,edx
xor ecx,ecx
xor ebp,ebp
.while 1
lea esi,[esi+1]
xor eax,0ffffffd1h
add eax,1
adc ebp,0
xor eax,02dh
add eax,1
adc ebp,0
mov ebp,0
lea edx,[edx+1]
cmovnp edx,ebp
cmovnp ebx,esi
cmp edx,ecx
cmova ecx,edx
cmova edi,ebx
test eax,eax
.break .if ZERO?
movzx eax,BYTE ptr [esi]
.endw
; edi = pointer to largest sentence
; ecx = size of sentence

qword