News:

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

From EndofString

Started by ic2, May 22, 2007, 06:34:48 PM

Previous topic - Next topic

ic2

Is there an opcode that can tell the processor to point to the end of a string and start reading from back to front.  I think i once saw a code that use a combination of clc, cld or something like that  to  make it  possible. I tried many things but nothing worked. Here's a snippet to give you an idea of what i am trying to do.


        mov esi, offset RepacementText
        mov edi, offset MyString
L1:
;  clear a flag to tell processor to start reading FORWARD
    inc edi
;  Set a flag to tell processor to start reading BACKWARD





evlncrn8


Tedd

CLD (CLear Direction-flag = forward) and STD (SeT Direction-flag = backward) are specifically for use with the CMPS/MOVS/STOS instructions, but only affect what happens to esi and edi on each iteration (either add 1, or subtract 1). You still need to find the start/end of the string you're reading from before you start the instruction (so when copying backwards, esi and edi need to already point to the end before you start copying/searching.)

Sometimes it may actually be better to do it out yourself the 'long' way, depending on what you're doing.
No snowflake in an avalanche feels responsible.

ic2

Thanks Ted,

QuoteSometimes it may actually be better to do it out yourself the 'long' way, depending

Would one of the reason be that a calls like STOS can be replaced with faster written code or is it because STD can be problematic and not to use STD or it better (and can be just as fast) to find the endofstring with your own code.

When setting STD it don't just JUMP to the end of the string.  Does it?.  If it does that saves a trip unless the processor is using something to walk it instead of POPPING it to the endofstring.  If so, i see no true gain of usage, only less typing.

My guest is you can do it yourself or you can let STD (underlaying walker) walk it to the end, than only caller can walk the pointer backward.

I hope setting STD will at lease JUMP to the endofstring.

(sorry for the  added -- why do the sky not ever fall type question.  After all of this i'm now coming up with an answer,)

I ask because i like to know how to STD really end up at the endofstring and see an example of how to do it yourself.



tenkey

First question is "How do you (or the processor) know where the end of string is?"
The processor has no preconceptions of what determines the end of string.
STD and CLD simply set flags in the EFLAGS register. There is no "default" string pointer.

Because the OS was developed for C programs, many API use 0-terminated strings. You need to search for this character to find the end of string.

Other languages, such as VB, store a string length somewhere. In such languages, the end of string (or more precisely, the location after the last character) is found by adding the length to the beginning address of the string. 0-characters are treated as ordinary characters in such languages.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

ic2

QuoteOther languages, such as VB, store a string length somewhere. In such languages, the end of string (or more precisely, the location after the last character) is found by adding the length to the beginning address of the string. 0-characters are treated as ordinary characters in such languages.

I get it now.  So, there is no MAGIC :(  To get to the endofstring we must use or write code like STOS, strLen, or the method i now understand from this link listed below.  dsouza123 latest example  demonstrates that perfectly.

Nothing like hearing facts from people with much greater experience.

Thanks tenkey

didn't know all of what i was working with...  Was doing  it all the time long ...

http://www.masm32.com/board/index.php?PHPSESSID=ea33a2ed1deaff1f46f78f8bc3928616&topic=7335.0

donkey

Well, in order to find the end of the string you mush first scan ahead from the beginning to the null terminator, the string functions do this quite well.

cld ; explicitly set the direction to forward just in case
mov edi, offset BaseOfString
mov ecx, MaxLen
repnz scasb


EDI now points to one past the NULL terminator so back it up two bytes to the address of the last character

sub edi, 2

ECX holds MaxLen - (the length of the string plus one) so subtract MaxLen from it, add 1 and change it's sign

add ecx, 1
sub ecx, MaxLen
neg ecx


You now have the length of the string and the address of the last character, so change the direction counter and begin to scan the string. I assume you are looking for a delimiter such as TAB or something like it so load that into AL...

std ; set the direction to backward
mov al, 9 ; the character to search for TAB in this case
repne scasb ; Since EDI and ECX are already loaded we leave them as is and start our scan.


Don't forget to clear the direction flag...

cld

The scan will continue until a TAB is found or ECX reaches zero (1 character before the beginning of the string), you can test ECX to find out if a tab was found...

test ecx,ecx
jnz >.TABFOUND


Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Tedd

Quote from: donkey on May 24, 2007, 02:10:26 AM
cld ; explicitly set the direction to forward just in case
mov edi, offset BaseOfString
mov ecx, MaxLen
repnz scasb


EDI now points to one past the NULL terminator so back it up two bytes to the address of the last character
not unless you set al = 0 first :wink
No snowflake in an avalanche feels responsible.

hutch--

Basically any zero terminated string length procedure will do this job. Depending on the need some are faster than other but even a simple incremented pointer byte scanner will probably run over 100 meg/sec so its not like its a big deal to do.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

donkey

Quote from: Tedd on May 24, 2007, 12:26:34 PM
Quote from: donkey on May 24, 2007, 02:10:26 AM
cld ; explicitly set the direction to forward just in case
mov edi, offset BaseOfString
mov ecx, MaxLen
repnz scasb


EDI now points to one past the NULL terminator so back it up two bytes to the address of the last character
not unless you set al = 0 first :wink


You are correct sir, I missed that line when I was copy/pasting the code from RadASM.

Quote from: HutchBasically any zero terminated string length procedure will do this job. Depending on the need some are faster than other but even a simple incremented pointer byte scanner will probably run over 100 meg/sec so its not like its a big deal to do.

Hi Hutch,

That wasn't meant for speed just to demonstrate that the end of the string must be found and one method for finding it before the reverse scan, it also serves as an example of scasb usage, there are plenty of faster methods though for a short string (under say 100 characters) it doesn't make much difference.

Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

ic2

Quotenot unless you set al = 0 first


Now you tell me... :)  I can't sleep playing with this stuff. I did good up until last morning.  I dug a hold so deep that it un-explainable...   But I'm getting the hang of it.  I learned more than i ever knew in these few days. It just keep getting better.

Thanks donkey for the example and comments .  All of these words helped me out a lot.  It's get rough the deeper you go.  It's not hard to lose a WORD.  Good night. 

Thanks again everybody

kaithy

In which language these codes are written ???
:(   :dazzled:

Tedd

Quote from: kaithy on May 31, 2007, 10:55:22 AM
In which language these codes are written ???
English :bdg

..assembler? This is an assembler (ASM) language board :wink
What are you looking for?
No snowflake in an avalanche feels responsible.