Solved

Stack System (Turbo C memory model : Small)

Posted on 1997-04-19
3
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

740 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