Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Switching Stacks

Posted on 1998-11-06
3
Medium Priority
?
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

636 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