Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/21/05 07:00
Read: times


 
#90078 - Stack pointer
Responding to: ???'s previous message
In the 8051 the stack grows upwards (ascending addresses) so the tradeoff is working ram vs stack. You always want to put the stack as high up in memory as possible and I think most of the compilers put the stack above the variables. This is what I would do when programming in assembler. Setting the stack pointer to 7fh would seem rather wasteful unless you needed 128 bytes of stack - which in many instances is gross overkill. Again, place the stack as high up in memory as possible.

This evokes the perennial question: "how much stack do I need?". There's no one answer for this and really comes down to your code - there's two bytes needed for each call so one normally keeps the number of nested subroutines to a minimum. Interrupts use a minimum of 2 bytes per call then you normally stack at least the ACC and PSW making this 4 bytes total. So the worst case is if you get the maximum number of interrupts nested whilst the lowest subroutine has been called. Another reason to limit the number if interrupt sources as much as possible. The stack is a limitation of the 8051 architecture especially with 'c' and large code. The 8051 designers obviously made this tradeoff due to the expected application of the cpu - and in many cases it is not a problem. As they say - 'horses for courses'. Because of the stack limitation, this makes the use of pre-emptive task switching difficult - each task has it's own stack so that places more demand on ram for the stack. Co-operative tasking allows for a much 'flatter' stack thus explaining why this is a technique more suited to the ideosynchracies of the 8051.

Simulators are handy for watching stack usage as you can fill the stack area with a magic number (say 55h) and you can see high tide mark of the stack where it hasn't overwritten the value.

There's nothing wrong with having to indirectly access the 'extra' 128 bytes of ram - if you have an array you're going to access this indirectly anyhow but for simple variable storage it can be a little annoying.

In assembler, the method I use to position the stack is as follows:


var1 ds 1
var2 ds 1
etc
etc
stack equ * ;locate beginning of stack at the end of the vars


reset:
mov sp,#stack





List of 21 messages in thread
TopicAuthorDate
initializing SP to 7FH            01/01/70 00:00      
   why?            01/01/70 00:00      
      To Oleg & Russell            01/01/70 00:00      
         To Mehdi            01/01/70 00:00      
         To Mehdi            01/01/70 00:00      
            why not            01/01/70 00:00      
               for example, please            01/01/70 00:00      
                  Oleg, why I do similar            01/01/70 00:00      
                  here they are            01/01/70 00:00      
                     well            01/01/70 00:00      
                        well well            01/01/70 00:00      
                           well, well - done            01/01/70 00:00      
                     tight SRAM - use C            01/01/70 00:00      
                        - or assembler            01/01/70 00:00      
   Stack pointer            01/01/70 00:00      
   external stack            01/01/70 00:00      
      why not?            01/01/70 00:00      
         SDCC            01/01/70 00:00      
   Re:initializing SP to 7FH            01/01/70 00:00      
   very old assemblers only            01/01/70 00:00      
      Let the assembler do the work!            01/01/70 00:00      

Back to Subject List