Page 325 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 325
302 Chapter 10 Elementary Data Structures
As words are pulled from top or bottom, more space is made available to push
words on either the top or bottom. To take advantage of this, we think of the buffer as a
ring or loop of words, so that the next word below the bottom of the buffer is the word
on the top of the buffer (see Figure 10.6). That way, as words are pulled from the top,
the memory locations can become available to store words pushed on the bottom as well
as words pushed on the top, and vice versa. Then to push or pop data into or from the
top or bottom of it, we can execute procedures:
void pstop(int item_tq_push) {
{if((size-H-)>=50)error=l;if(top==50)top=0; deque [top++] = item_to_push;}
int pltop()
<if((—size) < 0)error=l;if(top == 0)top=50;return(deque[—top]);}
void psbot(int item_tojpush) {
if((size++)>=50)error=l; if(bot==0) bot=50;deque[—hot]=item_tq_push?}
int plbot()
{if((--size)<0) error=l; if(bot==50) bot=0; return( deque[bot++]);}
In assembly language, a deque can use registers to point to its top and bottom
elements. In our discussion, we will first assume that all of memory is available to
store the deque elements, and then we will consider the more practical case where the
deque is confined to a buffer rather than all of memory. We use register X to point to the
top and Y to point to the bottom of the deque. If location L is where one wants the first
possible push on the top to go, one initializes the top pointer with
LDX #L
A push from accumulator B onto the top of the deque then corresponds to
STAB 1, X+
while a pull from the top into B corresponds to
LDAB 1,-X
Just as we wrapped around a drum as shown in Figure 10.6 in C, we need to do the same
in assembly language. When a byte is pushed into the bottom of the deque, it is actually
put into the bottom byte of the buffer. The pointer is initialized to the top of the buffer,
but upon the first push to the bottom of the deque, the pointer is moved to the bottom of
the buffer. As an example, if we use a buffer with 50 bytes to hold the deque, we would
have the directive