0

I am trying to learn and better understand FP(BP), which according to my textbook is "FP(BP): point to the stack frame of current active function". I have learned that the stack frame link list ends in 0. Therefore, in order to print out the stack frame link list, I have written this function:

int a = 1;
int b = 2;
int c = 3;

asm("movl %ebp, FP"); //Sets FP
int *i = FP;

while(i != 0)
{
    printf("Value: %d \n", i); //Not working
    printf("Hex Address: %#X \n", i);
    i = *i;
}

So I think the hex line address is working. It prints 3 hex addresses. I may be wrong but what I'm trying to do is print the values of all 3 variables in the "active function" and also their hex addresses. My value line is not working. Maybe I'm not understanding how function stack is working? I've tried printing i like i, *i, &i, tried all difference things, doesn't work.

Any help clarifying?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Birdman
  • 1,404
  • 5
  • 22
  • 49
  • 3
    "Frame pointers" are really a platform- and compiler-specific thing. It's not something you should examine yourself unless you know *exactly* how your current compiler have it set up. It might not even *exist*. – Some programmer dude Jan 16 '18 at 04:34
  • 6
    You *don't* work with FP in C. – user253751 Jan 16 '18 at 04:35
  • 1
    This depends *a lot* on which compiler you have and the settings you use for the compilation. For example, with just the slightest optimization turned on the compiler might see that a, b, and c are never used, so it doesn't bother to store them in memory at all. – Bo Persson Jan 16 '18 at 04:36
  • It's for an operating systems course I'm taking @immibis The line that sets FP is provided to use in the book. We have to create the code to print. – Birdman Jan 16 '18 at 04:43
  • You must know what your compiler does, and/or the ABI for the platform on which you're working. Without you identifying the compiler, the platform, the chipset, etc, and probably the compilation options (with GCC, `-fomit-frame-pointer`, for example, would mess up code that relies on frame pointers, but the [documentation](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options) says it's turned on by `-O` or `-O1`, the basic level of optimization). The question at the moment is nowhere near specific enough to be answerable; it is 'too broad' or "unclear what you're asking". – Jonathan Leffler Jan 16 '18 at 04:57
  • Note that one of your colleagues asked [Print the stack frame link list and hex contents of stack from FP to stack frame](https://stackoverflow.com/questions/48271127/print-the-stack-frame-link-list-and-hex-contents-of-stack-from-fp-to-stack-frame) which is very closely related to this. It is as under-specified as this question. – Jonathan Leffler Jan 16 '18 at 05:49
  • What does "not working" mean? – Art Jan 16 '18 at 06:52
  • What I was referring to "working with" FP is on the Stack frame. One each side of the FP exists local variables and parameters, and you can navigate from FP by adding or subtracting to access them. That is what I have found out and used to make this work. – Birdman Jan 16 '18 at 07:51
  • 1
    @Scheff: consider it a semi-educated guess. Two people ask very similar questions about frame pointers on the same day. There haven't been questions about frame pointers like this before that I recall, and certainly not for quite a while. There's a decent chance that they're attending the same educational institution and got the same homework assignment. Nothing conclusive; just the similarity in the question and the timing suggests to me that they're colleagues (whether they know it or not; it might be an online course and they're living on different continents). – Jonathan Leffler Jan 16 '18 at 08:09
  • @JonathanLeffler So, I didn't miss something. Your arguments seem reasonable. On the other hand, sometimes accidents happen. No rule, no reason, it's just how universe works. :-) But, thanks for your patience. – Scheff's Cat Jan 16 '18 at 08:13
  • @Scheff, a friend of mine is going through this exact assignment right now, a year later. This question is underspecified, but in OP's defense it's almost everything specified by the class and assignment. – E.D. Jan 15 '19 at 04:57

0 Answers0