News:

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

Quickest Method To Get The String

Started by 2-Bit Chip, August 19, 2009, 09:49:22 AM

Previous topic - Next topic

2-Bit Chip

My procedure is getting a pointer to the beginning of a string. Should I loop, putting a character into the buffer one after one until I hit a null (0)?



Mirno

There are some funky ways to do this four characters at a time, but basically the idea you suggest is the basis of it.

However you only need to copy the string from it's pointed to location to another if you want to
a) keep the original string as is
and
b) do something with a modified version of it.

For string moving check out lods[b/w/d], stos[b/w/d], movs[b/w/d], and the rep prefix.
They should make things easier.

There are various ways to make things "faster", but starting out the speed of your code isn't so important.
By the time you need speed, you will have a better idea of where to find it.

Mirno

Rockoon

Quote from: 2-Bit Chip on August 19, 2009, 09:49:22 AM
My procedure is getting a pointer to the beginning of a string. Should I loop, putting a character into the buffer one after one until I hit a null (0)?

Yes, but you should also make sure that you dont run off the end of your buffer. Buffer overflows will result in either unrelated data/code being corrupted, or a segment fault bringing down your program with an error dialog.

The optimization that Mirno was talking about also has the potential to run off the end of the input string (its actualy guaranteed to in 3 out of 4 cases) but luckily this can only cause a segfault, and in practice the segfault is easily avoided with a little bit of setup.
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

hutch--

Mirno is right, the fastest way to read a string if you don't need to copy it is read it in place. Just get its address and read from that address.

See if there is a way of knowing the length of the string if you must copy it (from a file or similar) and then read it 4 bytes at a time until you are within 4 bytes of the end that byte read the last 1 to 3 bytes.

For all the tricks, a well written byte scanner will read the string at well over 100 meg / second so unless you are reading massive quantities of data you may be wasting your effort writing more complex 4 byte read routines.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

more information would be helpful
where is the string coming from ?
what are you trying to do with it ?
what is the purpose of the buffer that you refer to ?

2-Bit Chip

Oh, I would not have to put a string into a local array of bytes to use it?

Sorry I wasted your time, every one.

dedndave

not neccessarily
you may have to terminate it or something
as i said...
Quotewhere is the string coming from ?
what are you trying to do with it ?
what is the purpose of the buffer that you refer to ?

2-Bit Chip

Quote from: dedndave on August 19, 2009, 03:48:32 PM
not neccessarily
you may have to terminate it or something
as i said...
Quotewhere is the string coming from ?
what are you trying to do with it ?
what is the purpose of the buffer that you refer to ?
String = hostname (ex. "masm32.com", "technet.microsoft.com")

1. the string-pointer will come from an assembly file. this procedure will be in a library.

2. connect.

3. I thought that was the only way I could use the string, I've learned that I can just use the pointer to it.

dedndave

in those examples, the strings should be terminated with a 0 byte
but, yes, all you need is a pointer to the first byte of the string

.data
SomeString db 'hello world',0
.code
.
.
.
mov eax,offset SomeString

2-Bit Chip

How would the system know when a string ends?

If I were to do:
MyString db "Hello world!",0,0

Would it stop at the first null and not end at the second?

Astro

Hi,

NULL is usually a string terminator, so the FIRST NULL it will encounter it will stop, but only if it is meant to do so...

If you need to deal with a string that may contain NULL part way through, or that are not NULL terminated at all, then the only other thing you can do is to know the length of the string you are dealing with, otherwise you have absolutely no idea when to stop.

Best regards,
Astro.

dedndave

well - lol
"null" can mean many things - which is a bit confusing
it can be used to represent a string that has 0 length, for example
it is also used by microsoft for many api functions as "NULL" - that typically takes the form of a dword value of 0
for example, a hypothetical api function requires an address as one of the parameters
in this hypothetical function, you may be able to use NULL to indicate the parameter is to be ignored or "n/a"
that is because the caller normally has no access rights to address 00000000, so it must not be a valid address
null (sometimes refered to as "nul") is also a character byte equal to 0
ascii strings terminated with a 0 byte is an old DOS convention (some strings in DOS were terminated with "$")
you may sometimes see them refered to as "asciiz" strings
and, yes, indeed, the string does "stop" at the first byte that is 0
it is merely a way to denote the length of a character string without having to explicitly store its' length
back in the days of basic, strings were stored, beginning with the string length as a word (2 bytes)
as you can see, that takes an extra byte, otherwise the length of a string would be limited to 255 characters
if it were proceeded with a length byte, it may as well be 0 terminated, allowing any length
many functions require asciiz strings, such as file functions and, quite often, display functions
in some cases, such as using WriteFile to display text, the length is specified as a seperate value, so it needs no terminator