I have this problem, I am recursively calling a function in C and C is lexically scoped, so I can only access the current stack frame. I want to extract the arguments and the local variables from the previous stack frame which was created under the previous function call while im on the current stack frame
I know that the values from the previous recursive call are still on the stack, but I cant access access these values because they're "buried" under the active stack frame?
I want to extract the arguments and local variables from the previous stack and copy them to copy_of_buried_arg and copy_of_buried_loc;
It is a requirement to use inline assembly using GAS to extract the variables, this is what I have so far, and I tried all day, I cant seem to figure it out, I drew the stack on paper and did the calculations but nothing is working, I also tried deleting calls to printf so the stack will be cleaner but I cant figure out the right arithmetic. Here is the code so far, my function halts on the second iteration
#include <stdio.h> char glo = 97; // just for fun 97 is ascii lowercase 'a' int copy_of_buried_arg; char copy_of_buried_loc; void rec(int arg) { char loc; loc = glo + arg * 2; // just for fun, some char arithmetic printf("inside rec() arg=%d loc='%c'\n", arg, loc); if (arg != 0) { // after this assembly code runs, the copy_of_buried_arg and // copy_of_buried_loc variables will have arg, loc values from // the frame of the previous call to rec(). __asm__("\n\ movl 28(%esp), %eax #moving stack pointer to old ebp (pointing it to old ebp)\n\ addl $8, %eax #now eax points to the first argument for the old ebp \n\ movl (%eax), %ecx #copy the value inside eax to ecx\n\ movl %ecx, copy_of_buried_arg # copies the old argument\n\ \n\ "); printf("copy_of_buried_arg=%u copy_of_buried_loc='%c'\n", copy_of_buried_arg, copy_of_buried_loc); } else { printf("there is no buried stack frame\n");// runs if argument = 0 so only the first time } if (arg < 10) { rec(arg + 1); } } int main (int argc, char **argv) { rec(0); return 0; }