Solved

Stack System (Turbo C memory model : Small)

Posted on 1997-04-19
3
340 Views
Last Modified: 2012-06-27
I try to know the number of arguments i receive in a function. The declaration of this is PCB fork(void (*function)(), ... ) and when im calling it I write running=fork(DrawBox,colour).

The probles is about the pointers SP and BP, because when I call the function , the pointer SP moves and I cant know where are the arguments in the stack system.

If i could know the number of arguments im sending, I could move SP to the begging when Im inside the function.

Thank you in advanced... :)
0
Comment
Question by:smediodia
  • 2
3 Comments
 

Author Comment

by:smediodia
ID: 1249971
Adjusted points to 100
0
 
LVL 2

Accepted Solution

by:
Slarti earned 100 total points
ID: 1249972
There is no internal way in C to know how many parameters are passed to a function declared with the elipsis (...) operator. The simplest solution is to add a parameter which contains the number of arguments (ugly but it works). Then, (in small model), [sp-2] contains the value of this parameter. If you use this system you need to make sure that:
1. The compiler generates a near function call. (This is almost always the case in small memory model, unless you're calling a library function).
2. The function is declared in C calling convention, otherwise the parameters are pushed to the stack in the wrong order. In C and C++, C calling convention is the default.

If you do not want to use this method, then you can access the parameters, but you can't know when you've reached the last parameter and are actually reading parts of the stack which don't belong to you. Your other option is therefore to include an identifying signal which will say "this is the last parameter." You can then access all of the parameters normally, and just see when you get to the last one.

In most cases, the preferred solution is the first one.
0
 

Author Comment

by:smediodia
ID: 1249973
Thats one of the ideas I got, but I cant use it, because I have
the prototype of the function and I cant change it.
Another solution I thought is to add a new global variable which
contains the SP-BP. Thats the number of reserved memory I got in
main() for local variables. Then , when im in the fork() sub sp-2
(oh add sp+2) and I know the BP in main function and then add sp
the value of the global variable i declareted. Then I control the
return address in the stack and I got the arguments.
But , thanks Slarti.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now