Solved

Switching Stacks

Posted on 1998-11-06
3
268 Views
Last Modified: 2010-04-15
Inside a Interrupt Service Routine, I need to change the presently running program's Stack to the ISR's Stack. I have two questions.

1. What is the Code for this in Microsoft C? or can I use     inline assembly?
2. How do I determine where to put the Stack Pointer? i.e     how do I determine the amount of stack used?
0
Comment
Question by:ravipal
3 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1254087
to do this you need to use assembler....

for C/C++ the inline asm is like this..

{
  //Code
  asm {
  //asm code goes here,,
  }
}

SP(Stack Pointer) is the assembler register....
0
 
LVL 1

Accepted Solution

by:
bculver earned 50 total points
ID: 1254088
The following code should get you going. Upon entry of the interrupt handler the __interrupt command forces the handler to reload the data segment, thus you have access to you global variables and a place to save the current stack registers. Also remember that if you use an alternate stack, local variables in the interrupt handler are going to be inaccessable because there referenced to the original stack not your new one (there are ways around this but it can get real messy, just use a bit of gloabal space for any needed locals. Any functions called by the interrupt handler are ok to have local variables). As far as the stack size, it depends on what you handler does. Function calls parameters and local variables are all allocated to the stack so count em up if you need and add a few extra (double it) for good luck. If you enable IRQ's in the middle you your handler, you'll also have to account for OS or other interrupt needs also, (maybe a couple of hundred bytes or so.)


// Global
char stack[1024];  
int  ssreg,spreg;

void __interrupt __far IntHandler()
{
_asm mov      ssreg, ss                  // save current ss
_asm mov      spreg, sp                  // save current sp
_asm mov      ax, offset stack[1024-2]      // get ptr to top of stack
_asm mov      sp, ax                        // stack address to sp
_asm mov      ax, ds                        // stack segment to ax
_asm mov      ss, ax                        // stack seg to ss

      // YOUR CODE HERE

_asm mov      ss, ssreg                  // restore ss
_asm mov      sp, spreg                  // restore sp
}

0
 

Author Comment

by:ravipal
ID: 1254089
Thanks bculver, I need few clarifications on your code.

The 1024 you put in the char stack array, Is is a assumed figure ? or is that a fixed figure??

Why do you put 1024-2 to get ptr to the top of the stack?, is it not 1024-1?

What is the reason behind moving values to AX before moving them to SP and SS respectively?

Would appreciate your clarifications very much.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now