3

In assembly You can store data in registers or on the stack. Only the top of the stack can be accessed at any given moment (right?). Consider the following C code:

main(){
    int x=2;
    func();
}

func( int x ){
    int i;
    char a;
}

Upon calling func() the following is pushed onto the stack (consider a 32bit system):

variable x (4 bytes, pushed by main)
<RETURN ADDRESS> (4 bytes pushed by main?)
<BASE POINTER> (4 bytes pushed by func())
variable i (4 bytes, pushed by func())
variable a (1 byte, pushed by func())

I have the following questions:

  1. In C code you can access the local variable from anywhere inside the function, but in assembly you can only access the top of the stack. The C code is translated into assembly (in machine code but assembly is the readable form of it). So how does assembly support the reading of variables that are not on top of the stack?

  2. Did I leave out anything that would also be pushed to the stack in my example?

  3. In assembly if you push a char on the stack or an int, how can it determine whethere it needs to push 4 bytes or 1 byte? Because it uses the same operation (push) right?

Thanks in advance Gr. Maricruzz

4 Answers 4

3

The stack pointer at the beginning of the function is put into a register, and then the variables/arguments are accessed via this base address plus the offset for the variable.

If you want to see the code, instead of creating object files, let the compiler stop at creating assembler files. Then you can see exactly how it works. (Of course, that requires you to have a valid C program, unlike the one you have in the question now.)

2
  • Thanks! Is it also possible to see the current content of the stack somewhere? Any tools for that?
    – Maricruzz
    Feb 28, 2014 at 11:26
  • @Maricruzz Any decent debugger should have that functionality. Feb 28, 2014 at 11:27
2

The compiler is generating the assembly, each instruction set may differ but at the end of the day the stack is just a register holding an address to memory. The compiler is creating and knows the whole scope of the function it is creating and knows how far down to find each data item on the stack for local data items, so it will then create the appropriate code based on that instruction set to access those local items.

Some instruction sets you need to make a copy of the stack pointer and/or do math with the stack pointer as an operand but some other register as a result of that math, then based on that math (stack pointer + 8 words for example) you access that memory address. Some instruction sets have an addressing mode where you can in the load or store apply an offset to the stack pointer the math is done as part of the instruction execution and you dont have to use an intermediate result and a register.

1

Only the top of the stack can be accessed at any given moment (right?)

No, generally the ISA has instructions to access other elements on the stack as well. That is, accessing elements on the stack is not limited to push and pop like operations; typically you can just mov things back and forth between a stack location and a register.

1

Assembly can accesss any memory by address (just like C).

Simple, not optimized programs would put all local variables on stack before method execution, so variables addresses are address of execution frame plus some shift.

Then program can simple use pop and push method to store additional variables (i.e. subresults of some expression) on the top of the stack.

Summary:

  1. There is register (ESP in x86) pointing to the top of the stack
  2. Calling push is moving variable to the top of the stack and increasing this register
  3. Calling pop is moving variable from the top of the stack and decreasing this register
  4. Calling mov is moving variable between memory and registers and do nothing to stack register (ESP).
1
  • push decrements esp by the size of the operand, the stores the operand at [esp]. pop loads the operand from [esp], then increments esp by the size of the operand. With "frame pointer" disabled, C produces code that uses offsets from esp to locate variables. With "frame pointer" enabled, a function does a push esp | mov ebp,esp, and bases most offsets from ebp.
    – rcgldr
    Feb 28, 2014 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.