News:

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

local string variables

Started by warchieft, January 02, 2011, 01:38:11 PM

Previous topic - Next topic

warchieft

hi all ,

i heard global string variables  writing to object module in .DATA SEGMENT  ,  also local string variables is writing too ? ,  and global integer variables writed to .DATA SEGMENT and local integer variables writed in to .DATA SEGMENT  ?  , please can u answer me



void func(void)
{
       char *ptr = " hello world\n";
       printf(ptr);
}

at this function my "hello world\n" string writed to .DATA segment?  or not?  Thanks ...


hutch--

warchieft,

A local string is no more than allocated space on the stack, it differes from an initialised GLOBAL string in that it does not by default have any content. You can emulate a LOCAL string by making a DWORD variable that has the address of string data stored in the initialised data section. Its usually done as a macro.


    localstr macro quoted_text
      LOCAL txt
    .data
      txt db quoted_text,0
    .code
    <exitm OFFSET txt>
    ENDM


This is effectively how a high level language creates a local string, it creates a pointer to string data that is stored in the .DATA section
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

warchieft

thanks  mate , you say , global string are  stored to .DATA SEGMENT , and local strings are not using stack segment , they are using .DATA SEGMENT too but they dont initialized ,

so , if i use much of local string in my programs .DATA SEGMENT area will grow too ? are they in same DGROUP 'DATA' (with local and global strings ) ?  why we cant reach one function to another function's local string if they are in same .DATA segment?    global strings and local string share same .DATA segment area ?    i asked many question , sorry

clive

In the 32-bit Flat MODEL the address space is linear and not segmented in the manner it was with 16-bit code.

Code and Data are placed in different SECTIONS, with different read, write, and execute attributes.

If you want to pass a global or local variable down to another subroutine, you should use the address, not the name. Pass it on the stack or in a variable.

ie

  LEA  EAX,localstring
  PUSH  EAX

or

  LEA  ESI,localstring
It could be a random act of randomness. Those happen a lot as well.

warchieft

thanks m8 ,  localstrings are filling by runtime or they are stored in compiling/linking time before  ?  runtime or compile/link time?  , why all c books are saying u cant change string in function parameter

for example ,

void function(char *ptr)
{
     ptr[0]='x'
     ptr[1]='x'
     ptr[2]='x'
}

void main()
{
      char *ptr = "hello";
      function(ptr);
}

i can reach them why i cant change them ,  totally when i double click to .exe file and my "hello world" string loaded to RAM with my program , why it isnt possible change them .


clive

I'm not convinced that is true.

You wouldn't want to alter a 'const char *' string, because depending on what compiler/system you're using the data could reside in a CODE section or in ROM, and is expected to be left alone.

You'd also need to be careful not to over run the bounds of the string.

Also, variables passed down to a subroutine don't change the values in the calling routine. Thus changing 'ptr' won't effect it's value in main (ie ptr++), but changing the data pointed to by ptr will effect it globally.

void function(char *ptr)
{
     ptr[0]='x'
     ptr[1]='x'
     ptr[2]='x'
}

void main()
{
      char *ptr = "hello";
      function(ptr);
      puts(ptr);
}
It could be a random act of randomness. Those happen a lot as well.