• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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... :)
0
smediodia
Asked:
smediodia
  • 2
1 Solution
 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now