|
| practical usage of _SP in turbo C |
 |
3 Nov 2006 01:05:18 -0800 |
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);
}
|
| Post Reply
|
| Re: practical usage of _SP in turbo C |
 |
Fri, 03 Nov 2006 04:58:59 -080 |
John wrote:
>I want to understand the usage of _SP in turbo C. Is _SP the stack
>pointer register of the CPU?
Yes
> That means _SP should point to the
>top of the procedure call stack when we make function calls?
It points to the bottom of the call stack, at the return address, or
the address of the last of the local variable if any).
Stacks grow Down from the end of their array towards 0..
> I assume
>_SP is only useful when making function calls here.
_SP has very limited uses, mostly associated with using assembly code.
>main sp=-10
>Why the result has negative values?
The stack pointer is an address..
It doesn't have a sign.
The usual display method for addresses is to use heX output.
>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.
Yes. When you mess with _SP, you affect the program.
Don't do it.
>So does it mean _SP allows us to manipulate where is the top of the
>call stack manually?? Please advice for the practical usage??
_SP allows you to view and/or manipulate the stack.
"Practical usage" is nearly non-existant.
Probably less than 1 in 1000000 commercial programs make use of it
directly.
If you want to understand it, study Assembly Language.
> printf("main sp=%d\n",_SP);
printf("main sp=%X:%X\n",_ES,_SP);
|
| Post Reply
|
|
|
|
|
|
|
|
|
|