Solved

Switching Stacks

Posted on 1998-11-06
3
270 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
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.

809 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