View previous topic :: View next topic |
Author |
Message |
ee01ppa
Joined: 21 Feb 2005 Posts: 5
|
Using stacks |
Posted: Sat Mar 19, 2005 1:12 pm |
|
|
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
|
|
Posted: Sat Mar 19, 2005 3:59 pm |
|
|
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
|
|
Posted: Sat Mar 19, 2005 4:09 pm |
|
|
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
|
|
Posted: Sat Mar 19, 2005 4:11 pm |
|
|
Thanks!! I will give this a shot!!! |
|
|
|