practical usage of _SP in turbo C

J

John

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);
}
 
K

Keith Thompson

John said:
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).
 
J

[Jongware]

KiLVaiDeN said:
Hi John,

Have you googled a bit before posting this question ?

If you look at this page :
http://dclh.electricalandcomputerengineering.dal.ca/csteaching/oslab/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]
 
F

Frank Kotler

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
 
K

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...
 
J

John

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top