The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: stapper on March 20, 2010, 11:01:50 AM

Title: length of a string
Post by: stapper on March 20, 2010, 11:01:50 AM
Hi, Iam new in assembler and a trying to learn ASM and have a basic Question.

.Data
sztext1   DB "text1",0
sztext2   DB "texttwo",0


labels DD offset sztext1
DD offset sztext2
.
.
mov ecx,0
mov eax,labels[ecx*4]

I want to know the length from the were eax is pointing to. In need it for use with TextOut

thanks
Title: Re: length of a string
Post by: BogdanOntanu on March 20, 2010, 11:26:52 AM
Quote from: stapper on March 20, 2010, 11:01:50 AM
Hi, Iam new in assembler and a trying to learn ASM and have a basic Question.

.Data
sztext1   DB "text1",0
sztext2   DB "texttwo",0


labels DD offset sztext1
DD offset sztext2
.
.
mov ecx,0
mov eax,labels[ecx*4]

I want to know the length from the were eax is pointing to. In need it for use with TextOut

thanks

Use one of the StrLen kind of functions available in MASM32  (or write your own for learning).

As a conceptual example:

mov ecx,0
mov eax,labels[ecx*4]
invoke StrLen, eax
; now eax contains the length of the string, but you have lost the pointer to the string


However beware that normal functions will return the result in EAX register (in this case the length). In your case this will / would overwrite your EAX that contains the pointer to your string.

In order to avoid such problems it is better to keep the string pointer into another register like this:

mov ecx,0
mov ebx,labels[ecx*4]
invoke StrLen, ebx


In this example EAX contains the length of the string, and EBX still contains the pointer to the string
because by convention functions do not change EBX,ESI and EDI.

In order to use StrLen you might need to include the MASM32 inc file and libs if you did not do that already.

Title: Re: length of a string
Post by: Farabi on March 20, 2010, 11:49:05 AM
There are 2 ways to determined string length.
First you scan the memory until it found byte that have value 0.
Second, use "sizeof" command, but it only works for defined string.
Title: Re: length of a string
Post by: stapper on March 20, 2010, 01:46:25 PM
It works.

Thanks to everybody.

Title: Re: length of a string
Post by: jj2007 on March 20, 2010, 03:31:08 PM
Be cautious when using ebx, edi and esi - see The "register gets trashed" trap (http://www.webalice.it/jj2006/Masm32_Tips_Tricks_and_Traps.htm)