473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

practical usage of _SP in turbo C

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
6 2822
"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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Developwebsites | last post by:
I am taking advanced C++ at college and we use Borland Turbo C++ 4.5 compiler. How different is Turbo C++ from the standard C++? I know Borland used to call their versions of C++ and Pascal Turbo,...
14
by: Matt | last post by:
Hi folks. Can you help with some questions? I gather that some types supported by g++ are nonstandard but have been proposed as standards. Are the long long and unsigned long long types still...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
1
by: anonymous | last post by:
i wrote a graphical program in turbo c 3.0 ,it runs safe on turbo 3.0,(however i'm using xp) so when i wanted to run it for my teacher,both turbo c & program crashed; so i decided to compile it in...
16
by: scott | last post by:
I am looking for a copy of Turbo C 1.5 from 1987 for some historical research I'm doing into computing from that time period.
0
by: anonymous | last post by:
i wrote a graphical program in turbo c 3.0 ,it runs safe on turbo 3.0,(however i'm using xp) so when i wanted to run it for my teacher,both turbo c & program crashed; so i decided to compile it in...
3
by: postrishi | last post by:
Hello Everybody , I am a new user. I am currently using Turbo C++ 3.0 editor in my engg.Can you tell me or post me a ebook on turbo c++ and NOT on c or C++.MInd it I want a book on TURBO C++ editor...
37
by: cman | last post by:
Could you point me to the practical uses of XOR in assembly and algorithms? I understand XOR to be "true if and only if p is true or q is true". Where is this used? I draw a blank on usage. Tilak
4
by: Abe Kobei | last post by:
Hello. Is there an official website to obtain Turbo C 1.0? I know that I can download Turbo C 2.0 from Borland, but I want a smaller version, because I expect that older is smaller. I want...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.