Splitting the Atom back exit forward page 3

CHAPTER 1
OPERATION OF THE WORKSPACE AND OTHER STACKS

I. The Workspace Stack

A 4-byte wide workspace stack is used by the Atom to perform arithmetic functions and temporary storage of data being manipulated. This stack is best explained by comparison with the 6502 machine-code stack, as the principle is very similar.

The page zero locations 16 through 51 inclusive are reserved for the workspace stack, but since the information being stored is up to four bytes wide (that is, a BASIC integer range of about 2*10E9) this area is split into four parts:

16         24 LSB 4-byte wide
value
25         33  
34         42  
43         51 MSB
    ^
|
 
    4 = The Workspace Stack Pointer  

Just as the 6502 uses a stack from 1FF though 180 and points to the next free location in it by the stack pointer register S, the workspace also requires a pointer, and this is kept in location 4, as shown above.

In the case of the 6502 stack, the pushing and pulling of the numbers on the stack automatically changes S, the stack pointer, so that it points to the next free location. With the workspace stack the equivalent operation must be done by the software, by incrementing or decrementing the contents of 4 as needed

Many references are made in this book to routines which read or write values to the workspace stack, and may be used freely by those writing machine code routines. One example is given below. It is extracted from the Atom ROM at C99D, and is part of a routine to copy a random number in location 8 through B to the workspace stack.

C99D	LDY @8
	LDA 4
	STA 
	LDA #0001,Y
	STA #25,X
	LDA #0002,Y
	STA #34,X
	LDA #0003,Y
	STA #43,X
	LDA #0000,Y
	STA #16,X
	INX
	STX 4

Note how the X register is loaded from location 4 and then used as an offset to point at the current workspace stack values at 16,X; 25,X; etc. Note also that having pushed this data on the workspace stack, the workspace stack pointer is incremented by INX; STX 4. This is directly equivalent to the machine code instruction PHA (push value on stack and change stack pointer S) except that the routine achieves this on a 4-byte wide basis.

Machine code writers invoking existing ROM routines such as this should pay careful attention to the workspace stack pointer at 4, and always ensure that it stays inside the limits 0 through E.

back exit forward