News:

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

Find the longest sentence in a string.

Started by nikola99, May 19, 2010, 05:22:26 PM

Previous topic - Next topic

nikola99

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?

clive

Yes, several.

Post the code you have so far, we don't do homework assignments.
It could be a random act of randomness. Those happen a lot as well.

oex

:lol nice 1 clive I nearly got suckered into that one

EDIT: I wrote the proc if you have a good start :lol
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

qWord

.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
FPU in a trice: SmplMath
It's that simple!

oex

We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

dedndave

if he hands that in, qWord, his instructor will know she(he?) didn't write it   :P

joemc

i am confused. why lea esi, [esi+1],  is it faster ?

edit: Thinking about it, it actually makes more sense than an add.

clive

Plus it can be really useful if you don't want to effect the flags, especially the carry ie lea esi,[esi+4].
It could be a random act of randomness. Those happen a lot as well.

dedndave

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

qWord

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
FPU in a trice: SmplMath
It's that simple!