Solved

Stack System (Turbo C memory model : Small)

Posted on 1997-04-19
3
339 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 nested-loops in the C programming language.

762 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

28 Experts available now in Live!

Get 1:1 Help Now