473,837 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2855
"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_Keit h) 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*******@gmai l.comwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.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
13857
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, but that was a long time ago; even the 4.52 compiler is old. This is a requirement for the 4 year BS degree in comp sci, which is a shame as I feel the students shouldnt be taught on old crap. Is it very important to learn C++ on the newest...
14
363
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 under consideration for the ANSI C and C++ standards? Are they likely to be accepted into the standards? Which compilers support those types, and which do not?
6
8984
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 programming in straight C, the Win32 API sends windows messages to applications with various settings stored in bit fields, in which case changing the settings of the message is a matter of using the bitwise operators. This was a good practical example of...
1
3716
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 turbo 4.5 because i heard that it is running under windows ,so when i wanted to compile it this eror appeared: bgi graphic does not support under windows; so my question is: 1.should i use another header file except graphics.h or should i use...
16
3497
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
3874
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 turbo 4.5 because i heard that it is running under windows ,so when i wanted to compile it this eror appeared: bgi graphic does not support under windows; so my question is: 1.should i use another header file except graphics.h or should i use...
3
5932
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 and not on programming language C/C++' Moreover can you tell me that how do i give dos based commands use a c program.
37
9531
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
6361
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 smaller one because I want to put the compiler to 1 floppy disk with some other marginal space in it.
0
9837
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10870
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10621
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9392
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6998
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4038
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.