By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

practical usage of _SP in turbo C

P: n/a
I want to understand the usage of _SP in turbo C. Is _SP the stack
pointer register of the CPU? That means _SP should point to the
top of the procedure call stack when we make function calls? I assume
_SP is only useful when making function calls here.

Here's the program I wrote to test out how _SP behaves, but the result
is

main sp=-10
func1 sp=-14
func2 sp=-18

Why the result has negative values?

But if I put _SP = 10; in the program, then I got the following result,
the interesting part is main sp is still -10. And I got illegal
instruction pop up error message.

main sp=-10
func1 sp=6
func2 sp=2

So does it mean _SP allows us to manipulate where is the top of the
call stack manually?? Please advice for the practical usage??

thanks...
-------------------------------------------------
#include <stdio.h>
#include <conio.h>

void func1(void);
void func2(void);

void main(void)
{ //_SP = 10;
printf("main sp=%d\n",_SP);
func1();
}

void func1(void)
{ printf("func1 sp=%d\n",_SP);
func2();
}

void func2(void)
{ printf("func2 sp=%d\n",_SP);
}

Nov 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"John" <ja*****@gmail.comwrites:
I want to understand the usage of _SP in turbo C. Is _SP the stack
pointer register of the CPU? That means _SP should point to the
top of the procedure call stack when we make function calls? I assume
_SP is only useful when making function calls here.
_SP is non-standard and off-topic in comp.lang.c. Followups
redirected (for all the good it will do).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 3 '06 #2

P: n/a
Hi John,

Have you googled a bit before posting this question ?

If you look at this page :
http://dclh.electricalandcomputereng...b/teachos.html
you'll see relevant informations about this matter.

Turbo C has added those pseudo variables in order to simplify the
access to registers.

And as it has already been said, it's not a standard C way of doing
this.

Cheers
K

Nov 3 '06 #3

P: n/a

"KiLVaiDeN" <ki*******@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Hi John,

Have you googled a bit before posting this question ?

If you look at this page :
http://dclh.electricalandcomputereng...b/teachos.html
you'll see relevant informations about this matter.

Turbo C has added those pseudo variables in order to simplify the
access to registers.

And as it has already been said, it's not a standard C way of doing
this.
Messing with this register access has always been tricky. It was useful for
some of the _dos_interrupt calls (a few of which were, IIRC, 'macros' which
only output the correct INT xx opcode), but with any other function the
registers could be (a) trashed or (b) saved and restored (thus, doing
nothing :). My guess is _SP is added just to get a complete set. Even in
assembly, it's dangerous to mess around with it, and in C there may just be
too many variables to use it in a useful way. Example? Suppose you figured
out for a specific function where one of your variables resides on the
stack. Just about any change to that one function may alter that position!
Even a rather simple compiler optimization, such as optimizing out a local
variable, changes the C stack.

[Jongware]
Nov 3 '06 #4

P: n/a
John wrote:

....
Why the result has negative values?
....
printf("main sp=%d\n",_SP);
Looks like you're asking for a signed int. Try "%X" and the results
might make more sense.

Best,
Frank
Nov 3 '06 #5

P: n/a
KJH

John kirjoitti:
I want to understand the usage of _SP in turbo C. Is _SP the stack
pointer register of the CPU? That means _SP should point to the
top of the procedure call stack when we make function calls? I assume
_SP is only useful when making function calls here.

Here's the program I wrote to test out how _SP behaves, but the result
is

main sp=-10
func1 sp=-14
func2 sp=-18

Why the result has negative values?

But if I put _SP = 10; in the program, then I got the following result,
the interesting part is main sp is still -10. And I got illegal
instruction pop up error message.

main sp=-10
func1 sp=6
func2 sp=2

So does it mean _SP allows us to manipulate where is the top of the
call stack manually?? Please advice for the practical usage??

thanks...
-------------------------------------------------
#include <stdio.h>
#include <conio.h>

void func1(void);
void func2(void);

void main(void)
{ //_SP = 10;
printf("main sp=%d\n",_SP);
func1();
}

void func1(void)
{ printf("func1 sp=%d\n",_SP);
func2();
}

void func2(void)
{ printf("func2 sp=%d\n",_SP);
}

I would refrain from using _SP in Turbo C code... I cannot think of any
meaningful use for it. But then again, I think it's superficial to use
direct register access in C code anyway. Inline asm is something which
always made my mind boggle. Go figure...

Nov 3 '06 #6

P: n/a
It's interesting that I get main sp=F000:FFF6. However, I got Null
pointer assignment error

Am I supposed to cast like (int *)_SP; or (char *)_SP;

please advice. thanks...
KJH wrote:
John kirjoitti:
I want to understand the usage of _SP in turbo C. Is _SP the stack
pointer register of the CPU? That means _SP should point to the
top of the procedure call stack when we make function calls? I assume
_SP is only useful when making function calls here.

Here's the program I wrote to test out how _SP behaves, but the result
is

main sp=-10
func1 sp=-14
func2 sp=-18

Why the result has negative values?

But if I put _SP = 10; in the program, then I got the following result,
the interesting part is main sp is still -10. And I got illegal
instruction pop up error message.

main sp=-10
func1 sp=6
func2 sp=2

So does it mean _SP allows us to manipulate where is the top of the
call stack manually?? Please advice for the practical usage??

thanks...
-------------------------------------------------
#include <stdio.h>
#include <conio.h>

void func1(void);
void func2(void);

void main(void)
{ //_SP = 10;
printf("main sp=%d\n",_SP);
func1();
}

void func1(void)
{ printf("func1 sp=%d\n",_SP);
func2();
}

void func2(void)
{ printf("func2 sp=%d\n",_SP);
}


I would refrain from using _SP in Turbo C code... I cannot think of any
meaningful use for it. But then again, I think it's superficial to use
direct register access in C code anyway. Inline asm is something which
always made my mind boggle. Go figure...
Nov 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.