• C

Stack System (Turbo C memory model : Small)

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... :)
smediodiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

smediodiaAuthor Commented:
Adjusted points to 100
0
SlartiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smediodiaAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.