0

This is my main function

    #include<stdio.h>
    #include<stdlib.h>
    int name1(int g);
    char* getStringFromC(void);
    int main(){

int a=2;
char* g="this is called from assembly";
printf("%d\n",name1(a));
return 0;
    }

    .text
    .globl name1
  name1:
    push %ebp
    movl %esp, %ebp      
    movl 8(%ebp),%eax      
    movl %ebp,%esp
    popl %ebp
    ret//This function name1(int a) in assembly

Problem is how to access char* g from assembly ?? I though was 12(%ebp) or 16(%ebp) should contain that values which are like char* g and so on..

5
  • What is it exactly what you want to do with it? If you don't use it in your C code then an optimising compiler will not include it in the object file. Commented Feb 27, 2014 at 15:32
  • How is getStringFromC related? Commented Feb 27, 2014 at 15:34
  • You need to pass g as an argument to name1 or make it a global. Commented Feb 27, 2014 at 15:34
  • by definition local variables are local. name1 cannot access mains local variables directly (unless you pass a reference). Assembly language has absolutely nothing to do with it. Commented Feb 27, 2014 at 15:37
  • Can you just elaborate how stack works on this situation? And I want to write getStringFromC(void) also from assembly Commented Feb 27, 2014 at 15:39

2 Answers 2

1

You're not supposed to. If it wasn't included as parameter and is not global, you shouldn't try to access it.

But for the sake of completeness, I'll answer anyway: Use the frame pointer. %ebp points to its previous value. Copy it to a scratch register and you can index the caller's local variables through it.

Sign up to request clarification or add additional context in comments.

4 Comments

What makes you say the variable g exists in the compiled program at all? If the compiler sees g is completely unused, the compiler will almost certainly optimise it away, and no amount of frame tracking will get you to something that no longer exists.
Indeed. Plus, you don't know whether your function was actually called by main(). Or whether the caller uses the "omit frame pointer" optimization. So many things that can go wrong, so little time...
really thanks for point it out and if char* g is located outsize of the main function it means as a global variable can it be accessed by assembly ?
A global variable can be simply accessed by name, I think.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.