The example below does what you seem to want, and also shows how to
access a global variable. This works on my Ubuntu 13.10 x64 system.
As noted by Medinoc and hvd, looking at the stack in the way you're
trying to do isn't robust.
Makefile:
CFLAGS=-Wall -Wextra
all: main
main: main.o lib.o
main.c:
#include <stdio.h>
void* stack_peek(int g);
void* get_global_str(void);
char const *global_str="Global";
int main(){
int a=2;
char const * g="this is called from assembly";
printf("stack_peek(a): %p\n",stack_peek(a));
printf("g: %p\n",(void*)g);
printf("global_str: %p\n",(void*)global_str);
printf("get_global_str(): %p\n",(void*)get_global_str());
return 0;
}
lib.s:
.text
.globl global_str
.type global_str, @common
.globl stack_peek
.type stack_peek, @function
stack_peek:
pushq %rbp
movq %rsp, %rbp
movq 24(%rbp),%rax
movq %rbp,%rsp
popq %rbp
ret
.globl get_global_str
.type get_global_str, @function
get_global_str:
movq global_str,%rax
ret
Output:
$ make -B
cc -Wall -Wextra -c -o main.o main.c
as -o lib.o lib.s
cc main.o lib.o -o main
$ ./main
stack_peek(a): 0x40067b
g: 0x40067b
global_str: 0x400674
get_global_str(): 0x400674
getStringFromCrelated?gas an argument toname1or make it a global.