News:

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

program crashing

Started by Emil_halim, December 03, 2010, 04:15:16 PM

Previous topic - Next topic

Emil_halim

hi all

while navigating hla v3.0, i found a procedure called mytrim , i have adapted it and tested it in next code.

but the program crashes when control reach to this line , any help please ?

mov( ebx, (type str.strRec [eax]).length );


the code

program TrimTest;

#include("stdlib.hhf");

?@NoDisplay := true;
?@NoStackAlign := true;

   
procedure mytrim( s:string in eax ); @returns( "eax" ); @noframe;
begin mytrim;

push( ebx );
push( ecx );
mov( (type str.strRec [eax]).length, ebx );
forever

dec( ebx );
breakif((type int32 ebx) < 0 );
mov( [eax+ebx], cl);
breakif( cl not in { ' ', stdio.tab } );

endfor;
inc( ebx );
mov( ebx, (type str.strRec [eax]).length );  // program crashes here
pop( ecx );
pop( ebx );
        ret();
       
end mytrim;

static

     tst : string := "   Emil halim  ";
     
begin TrimTest;
 
     stdout.put("string befor trim is" ,nl);
     
     stdout.put(tst ,nl);
     
     stdout.put("string after trim is" ,nl);
     
     mytrim(tst);
     stdout.put( (type string eax) ,nl);

end TrimTest;

Sevag.K

static strings with attached string value are considered readonly since only the "pointer" is stored in the static section and the actual string is in readonly section.

to modify a string, you should declare it using str.strvar or on the heap or stack, or you can use this custom strvar macro, not to be confused with str.strvar!



    #macro strvar( strconst ): _strVar_, _padding_;

#if( ( @section & hla.inStatic ) != hla.inStatic )

#error( "str.strvar is legal only in the STATIC section" )
string // To prevent cascading errors

#else
string := &_strVar_;
align(4);
dword @length(strconst);
dword @length(strconst);
_strVar_:char; @nostorage;
byte strconst, 0;

?_padding_ := ((4-((@length(strconst)+1)mod 4))mod 4);
#while( _padding_ > 0 )

byte 0;
?_padding_ -= 1;

#endwhile

#endif

#endmacro

static
  tst : strvar( "   Emil halim  " );




the function as you have it is not really a "trim" function, it's more like of a remove trailing spaces function.


Sevag.K


here is an expanded version of the macro that lets you declare a string buffer size as well as initial stiring


#macro strvar2( _strconst_[] ): _strVar_, _curlen_, _maxlen_;

#if( ( @section & hla.inStatic ) != hla.inStatic )

#error( "strvar is legal only in the STATIC section" )
string // To prevent cascading errors

#else
#if( @elements(_strconst_) > 2 )

#error( "arguments : strvar( """string constant""" [,optional string length]" );

#else
?_curlen_ := @length( @text(_strconst_[0]) );
#if( @elements( _strconst_ ) = 2 )
?_maxlen_ := @text( _strconst_[1] );
#else
?_maxlen_ := _curlen_;
#endif

// make sure max length isn't less than string constant
#if( _maxlen_ < _curlen_ )
?_maxlen_ := _curlen_;
#endif

string := &_strVar_;
align(4);

// set maximum length of string with optional second parameter
dword _maxlen_;

// set current length of string
dword _curlen_;

_strVar_:char; @nostorage;
byte @text(_strconst_[0]);

// add the padding up to _maxlen_
?_maxlen := ((_maxlen_ - _curlen_) +4 ) & -4;
byte
?_maxlen_ := _maxlen_ -1;
#while( _maxlen_ > 0 )
?_maxlen_ := _maxlen_ - 1;
0,
#endwhile
0

#endif

#endif

#endmacro


static
  s0 :strvar2( "this is an editable string that has a buffer the same size as this string" );
  s1 :strvar2( "this is a string with buffer size 300", 300 );




Emil_halim

thank you.

i think hla is not clear enough ,because the readonly section you directly declare it by readonly
keyword , so why Hal consider static string with initialzing it as a readonly section ,
strange behaviour.

strvar2 , is nice macro .

Sevag.K

the thing is that hla strings are actually string pointers.  i faced the exact same problem in my early programming.  even in the readonly section, the string doesn't exist exactly where you declare it, only the string pointer does.