stack query


 
Thread Tools Search this Thread
Top Forums Programming stack query
# 1  
Old 09-16-2005
stack query

can anyone explain how the local variables are acessed from a stack frame of that particular function..since stacks can only push or pop values and stack pointer always points to top of the stack and the frame pointer always points to the end of the previous stack frame..how local variables are acessed?
for eg:
suppose for func temp
int temp(int a,int b);
{
int d=10;
int e,f,g;
e = d;
g = e+d;
f=g;
return(1);
}

first b will be pushed then a then the return address and the then stack frame for the func temp will start..and d,ef,g will be pushed in..but then how are these local variables acesses for assignment when only way they can come out by popping..
may be im not able to explain in proper way but can anyone if possible explain this...
# 2  
Old 09-16-2005
MySQL

Alok,

To my understanding for each function call a stack is maintained not for each variable reference. Variable will be referenced directly via address ptr.

Hope it clarifies the doubt.

rishi
# 3  
Old 09-16-2005
This varies from system to system...there isn't one particular way. Here is an imaginary implementaion....

Our stack grows toward higher addresses. Our computer uses decimal arithemetic.

Code:
# temp needs 32 bytes of  stack
#            -28 = where to store return address
#            -24 = location of return code
#            -20 = function's private copy of a
#            -16 = function's private copy of b
#            -12 = function's auto variable d
#             -8 = function's auto variable e
#             -4 = function's auto variable f
#              0 = function's auto variable g

To call temp, we add 32 to the stack pointer. Then we store a, b, the address of our return code, and the return addess in temp's stack frame.

When temp starts to run, it knows that d is located at (-12 + stack pointer). So it will store 10 there. The stack pointer will be in a register and there will be instructions available to load and store indexed by the register. The important point is that all of temp's autometic space gets pushed when temp gets called. And all of it is popped off when temp returns. During temp's run, it is used on the stack with no pushing or popping.
# 4  
Old 09-16-2005
Thanks Rishi,Perderabo for ur replies
with the help of ur replies and searching on the net i now understand how stack frames are created,the sequence in which variables are pushed in...but still i have some confusions..
"When temp starts to run, it knows that d is located at (-12 + stack pointer). So it will store 10 there. The stack pointer will be in a register and there will be instructions available to load and store indexed by the register."
I still do not get that how or what (some special pointer??)acesses these local variables.what instructions do u mention?can u please explain?
may be my doubts are very silly but if u can help me understand it,i will be very grateful...
# 5  
Old 09-16-2005
Quote:
Originally Posted by aloksave
"When temp starts to run, it knows that d is located at (-12 + stack pointer). So it will store 10 there. The stack pointer will be in a register and there will be instructions available to load and store indexed by the register."
I still do not get that how or what (some special pointer??)acesses these local variables.what instructions do u mention?can u please explain?
There might be a special register called an index register which has the stack pointer in it. Then you would have a some kind of load/store based on it.
Code:
            loadi     10
            storex   -12

the loadi would load a 10 into a working register (load immediate). The storex would not try to store at location -12. Rather it would add the index register's value to -12 to get the real address. Today's real computers have much more complex instructions than these. These instructions are actually pretty close to the IBM 1130 from the 1960's.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

What happens to the stack?

Consider this bad code, edited in Windows and run via CygWin after dos2unix so be aware of any hidden "\r" charatcers... #!/bin/bash n=0 stackit() { eval $1 if then exit 0 fi n=$ echo "$n" stackit stackit } stackit stackit Run under CygWin:- AMIGA:~> cd /tmp AMIGA:/tmp>... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. UNIX for Dummies Questions & Answers

Kernel Stack vs User Mode Stack

Hi, I am new to the linux kernel development area. I want to know what is the difference between kernel mode stack and user mode stack? Does each process has a user mode stack and a kernel mode stack?? Or Each process has a user mode stack and there is only one kernel mode stack that is shared by... (4 Replies)
Discussion started by: saurabhkoar
4 Replies

3. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

4. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

5. Shell Programming and Scripting

add the output of a query to a variable to be used in another query

I would like to use the result of a query in another query. How do I redirect/add the output to another variable? $result = odbc_exec($connect, $query); while ($row = odbc_fetch_array($result)) { echo $row,"\n"; } odbc_close($connect); ?> This will output hostnames: host1... (0 Replies)
Discussion started by: hazno
0 Replies

6. Programming

Heap and stack

Hi, I have a basic doubt here. Consider the following code snippet: main() { int *a; . . } Here the memory for a gets allocated in heap or stack. (5 Replies)
Discussion started by: naan
5 Replies

7. Programming

what is stack winding and stack unwinding

helo can u tell me what do you mean by stack winding and stack unwinding Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies
Login or Register to Ask a Question