Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6685039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:00:39+00:00 2026-05-26T05:00:39+00:00

I am trying to draw a stack as it would appear just before the

  • 0

I am trying to draw a stack as it would appear just before the “return count” line in the secondCall function. I am trying to draw it so that it would show all three frames (or activation records) for the three active functions, main, firstCall and secondCall.

Will someone help me complete the stack diagram?
I am trying to draw the positions of the base (ebp) and stack (esp) pointers as they were in each stack frame before the call to the next function.

The C code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int secondCall(int a, int b) {
  int count;
  count = write(STDOUT_FILENO, &"hello\n", 6);
  count += write(STDOUT_FILENO, &"jbnd007\n", 8);
  count += a + b;
  return count;
}
int firstCall(void) {
  int local;
  local = secondCall(4, 2);
  return local;
}
int main(int argc, char** argv) {
  int result;
  result = firstCall();
  return (EXIT_SUCCESS);
}

The Assembly code is as follows:

    .file   "A3Program2.c"
    .section    .rodata
.LC0:
    .string "hello\n"
.LC1:
    .string "jbnd007\n"
    .text
.globl secondCall
    .type   secondCall, @function
secondCall:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $40, %esp
    movl    $6, 8(%esp)
    movl    $.LC0, 4(%esp)
    movl    $1, (%esp)
    call    write
    movl    %eax, -12(%ebp)
    movl    $8, 8(%esp)
    movl    $.LC1, 4(%esp)
    movl    $1, (%esp)
    call    write
    addl    %eax, -12(%ebp)
    movl    12(%ebp), %eax
    movl    8(%ebp), %edx
    leal    (%edx,%eax), %eax
    addl    %eax, -12(%ebp)
    movl    -12(%ebp), %eax
    leave
    ret
    .size   secondCall, .-secondCall
.globl firstCall
    .type   firstCall, @function
firstCall:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $40, %esp
    movl    $2, 4(%esp)
    movl    $4, (%esp)
    call    secondCall
    movl    %eax, -12(%ebp)
    movl    -12(%ebp), %eax
    leave
    ret
    .size   firstCall, .-firstCall
.globl main
    .type   main, @function
main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    subl    $16, %esp
    call    firstCall
    movl    %eax, 12(%esp)
    movl    $0, %eax
    leave
    ret
    .size   main, .-main
    .ident  "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5"
    .section    .note.GNU-stack,"",@progbits

The stack drawing right now I have is :

+------------------------------+ high address
| original position of stack pointer
+------------------------------+
| saved value of ebp <- ebp (base pointer when in main)
+------------------------------+
| alignment spacing (don’t really know how big until runtime)
+------------------------------+
|
+------------------------------+
|
+------------------------------+
|
+------------------------------+
...
Each line represents 4 bytes (from lowest address (left) to highest address (right)).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:00:39+00:00Added an answer on May 26, 2026 at 5:00 am

    I’m not going to do the whole thing for you, but here’s a detailed explanation of how to follow through what happens.

    On entry to main the stack looks like this:

        : (whatever)                        :
        +-----------------------------------+
        | return address (in main's caller) | <- %esp
        +-----------------------------------+
    

    Standard prologue code:

    pushl   %ebp
    movl    %esp, %ebp
    
        : (whatever)                        :
        +-----------------------------------+
        | return address (in main's caller) |
        +-----------------------------------+
        | saved %ebp                        | <- new %ebp = %esp
        +-----------------------------------+
    

    This aligns the stack down to a 16-byte boundary by zeroing the bottom 4 bits
    of %esp:

    andl    $-16, %esp
    
        : (whatever)                        :
        +-----------------------------------+
        | return address (in main's caller) |
        +-----------------------------------+
        | saved %ebp                        | <- new %ebp
        +-----------------------------------+
        : some unknown amount of space      :
        : (0, 4, 8 or 12 bytes)             : <- %esp
        +-----------------------------------+
    

    …which is where you got to. Continuing:

    This subtracts 16 bytes from the stack pointer, which creates 16 bytes of reserved space for main to use:

    subl    $16, %esp
    
        : (whatever)                        :
        +-----------------------------------+
        | return address (in main's caller) |
        +-----------------------------------+
        | saved %ebp                        | <- %ebp
        +-----------------------------------+
        : some unknown amount of space      :
        : (0, 4, 8 or 12 bytes)             :
        +-----------------------------------+
        | 16 bytes of reserved  space       |
        |                                   |
        |                                   |
        |                                   | <- %esp
        +-----------------------------------+
    

    Now main calls firstCall; the call instruction pushes the return address, so at the point just after firstCall has been entered, the stack will look like this:

    call    firstCall
    
        : (whatever)                        :
        +-----------------------------------+
        | return address (in main's caller) |
        +-----------------------------------+
        | saved %ebp                        | <- %ebp
        +-----------------------------------+
        : some unknown amount of space      :
        : (0, 4, 8 or 12 bytes)             :
        +-----------------------------------+
        | 16 bytes of reserved space        |
        |                                   |
        |                                   |
        |                                   |
        +-----------------------------------+
        | return address (in main)          | <- %esp
        +-----------------------------------+
    

    The return address will be popped off again when returning to main due to the ret instruction at the end of firstCall.

    …and so on. Just keep tracing through the code in the same way, following what %esp is doing.

    The other thing that perhaps needs explanation is the leave which appears in the
    epilogue code of the various routines. So here’s how that works for main:

    Just before leave near the end of main, the stack looks like this (we’ve returned from firstCall
    and stored a value in the reserved space):

        : (whatever)                        :
        +-----------------------------------+
        | return address (to main's caller) |
        +-----------------------------------+
        | saved %ebp                        | <- %ebp
        +-----------------------------------+
        : some unknown amount of space      :
        : (0, 4, 8 or 12 bytes)             :
        +-----------------------------------+
        | %eax returned by firstCall        |
        | (and 12 bytes that were never     |
        |  used)                            |
        |                                   | <- %esp
        +-----------------------------------+
    

    leave is equivalent to movl %ebp, %esp followed by popl %ebp. So:

    movl   %ebp, %esp   ; (first part of "leave")
    
        : (whatever)                        :
        +-----------------------------------+
        | return address (in main's caller) |
        +-----------------------------------+
        | saved %ebp                        | <- %esp = current %ebp
        +-----------------------------------+ 
        : some unknown amount of space      :  }
        : (0, 4, 8 or 12 bytes)             :  }
        +-----------------------------------+  } all of this stuff is
        | %eax returned by firstCall        |  }  irrelevant now
        | (and 12 bytes that were never     |  }
        |  used)                            |  }
        |                                   |  }
        +-----------------------------------+
    
    popl   %ebp         ; (second part of "leave")
    
        : (whatever)                        :
        +-----------------------------------+
        | return address (in main's caller) | <- %esp  (%ebp has now been restored to the
        +-----------------------------------+            value it had on entry to "main")
          (and now-irrelevant stuff below)           
    

    And finally the ret pops the return address and execution continues inside
    whatever called main.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Postscript I'm trying to create a method for that would draw a
I'm trying to draw a polygon using c# and directx All I get is
I am trying to draw a textured cube using just 8 vertices and one
The cvLine() function can draw a straight line given two points P1(x1,y1) and P2(x2,y2).
Okay, I'm trying to create a simple app that will draw a shape (I'm
Trying to draw a basic stick man in html 5 css that walks. If
I'm trying to draw a graph on an ASP webpage. I'm hoping an API
I'm currently trying to draw shapes with 2D Arrays. In my class there is
I'm trying to draw some simples lines with the iPhone/Touch SDK. I'd like to
I am trying to draw a series of rectangles using OpenGL but some of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.