This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Please help. GNU As syntax for pointers with ANSI C binding in platform independent way.


On 2010-11-10 20:21, Andreas Schwab wrote:
Oleksandr Gavenko<gavenko@bifit.com.ua> writes:

Also I don't understand way I can not directly point str_data in
printf() call. Why I need wrote '&' before 'str_data' in .c:

Because str_data is an array of characters, so you should declare it as such.

Thnaks for suggestion. So I don't need reserve pointer to data
because I can directly access it after declaration:

extern const char str[];

Finally I wrote example which show how ANSI C works with
assembler:

====================str.S============================================
    .global var
    .global pvar
    .global str

    .text
var:
    .int    0xcafebebe
pvar:
    .int    var
str:
    .string "Hello world!"
====================str.S============================================

====================main.c============================================
extern int var;
extern int *pvar;

extern const char str[];

int main()
{
    printf("var: %x\n", var);
    printf("&var: %p\n", &var);
    printf("pvar: %p\n", pvar);
    printf("&pvar: %p\n", &pvar);

assert(pvar == &var);

puts(str);

    return 0;
}
====================main.c============================================

  $ gcc -o main main.c str.S
  $ ./main
var: cafebebe
&var: 0x401254
pvar: 0x401254
&pvar: 0x401258
Hello world!

Also I begin understand that

printf("&&var: %p\n", &(&var));

illegal because 'var' is address of the value (gcc warn: lvalue
required as unary '&' operand). C lang expressions

  var += 1;
  x = var * 2;

meen update or get value on 'var' address, '&var' get this address.

Some hard understand why:

  extern const char *str;
  puts(str);

crash executable.

I think so (and may be incorrect): 'str[1]' in this case meens
get address of 'str' name, get value on that address, adds to
value 1, result treats as new address (which doesn't make sense)
and get value by that address (which cause "Segmentation fault"
with "core dump").

As I understand now expression

extern const char str[];

means in case of access to 'str[1]': 'str' is a address to
string (constant/non-changeable value), to this address was
added 1, and loaded value from resulted address.

--
Best regards!


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]