CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Using stacks

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ee01ppa



Joined: 21 Feb 2005
Posts: 5

View user's profile Send private message

Using stacks
PostPosted: Sat Mar 19, 2005 1:12 pm     Reply with quote

Can someone help me out?? I need to push 4 or more variables into a stack and then pop these variable out using LIFO but i dnt know how to do this due to my limited programming skills. Can someone please help me out!!!
Ttelmah
Guest







PostPosted: Sat Mar 19, 2005 3:59 pm     Reply with quote

The PIC, does not have a register stack. Hence you will need to do a software emulation of this. You don't say what size the variables are?. So something like:
Code:

#define stack_size (32)
char stack[stack_size];
int8 inptr=0;
int8 outptr=0;
union joiner {
   int8 b[2];
   int16 w;
}res;
int8 temp;
#define push8(x) {stack[inptr++]=x;\
   inptr=inptr % stack_size;}
#define pop8 (inptr=((--inptr) % stack_size),\
   stack[inptr])
#define push16(x) {push8(make8(x,0));\
   push8(make8(x,1));}
#define pop16 (res.b[1]=pop8,\
   res.b[0]=pop8,\
   res.w)


The instructions are 'push8(val)', and 'push16(longval)', to put data on th stack, and val=pop8, together with longval=pop16, to pull data off the stack. There is no error checking (the stack will happily overfill, and lose data, if you push more than it can hold). Beware that ',' seperators are _required_ for this code to work in places, so I suggest you use 'cut and paste' to copy it. As shwn it allows a maximum of 32 bytes to be held, you can change this by altering the defined value of 'stack_size'.

Best Wishes
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sat Mar 19, 2005 4:09 pm     Reply with quote

Well you'll need two functions one to push something onto the stack and the other to pop something off the stack you'll also need a top_of_stack (TOS) pointer. Suppose you use an array for the stack Ex stack[5] and set TOS to zero. Your push function places the new item in the array at position TOS+1 (ex stack[TOS+1]=new item )and increments TOS. Your pop function gets the item from the array at position TOS and decrements TOS. Now you'll probably want to check for a pop when the stack is empty ( TOS=0 ) and a push when TOS equals the size of your array. Its best to learn by doing things and when your are stumped there is always PCMprogrammer and others in whose gratitude like me you'll be as they share their expertise.
ee01ppa



Joined: 21 Feb 2005
Posts: 5

View user's profile Send private message

PostPosted: Sat Mar 19, 2005 4:11 pm     Reply with quote

Thanks!! I will give this a shot!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group