TEA Stack Array Example
Last Modified: 2006-10-30
find:

basket

Acroname Robotics PDF webpage version TEA Stack Array Example PDF

Related
Products

Product image for Brainstem GP 1.0 Module
Brainstem GP 1.0 Module
Product image for BrainStem Moto 1.0 Module
BrainStem Moto 1.0 Module

Contents

Introduction

The TEA programming language is a subset of C.  The main difference between the two languages is that TEA does not handle pointers or arrays.  Since a TEA program has only a small amount of stack memory, a full implementation of dynamic memory allocation and multi- dimensional arrays is not practical.  It is possible to implement a small array by using global variables and writing low-level routines that take advantage of absolute stack addressing. 

Source Code

Local variables are declared within the routines where they are used.  They are discarded once the routine exits.  Global variables are declared at the beginning of a program outside of all the routines.  They are available to all routines and are not discarded until the program terminates.  Unlike local variables, global variables are always stored at a fixed location in a TEA program.  They are pushed onto the stack after a placeholder for the return value byte(s) and any input parameter byte(s).  The main routine in the program below returns an int which is 2 bytes and expects a char as input which is 1 byte.  This means that global variables will start at byte 3 from the bottom of the stack.  Bytes 0,1,2 are the return value placeholder and input byte.  A fixed-size array may be creating by declaring several consecutive global variables.  The following program has an array of 5 char variables and an array of 5 int variables. 

The program has four low-level routines written with TEA opcodes that read or write char and int variables at arbitrary locations in the stack.  This is possible with absolute stack addressing.  The routines require a start offset and index offset to access an array of global variables.  The start offset is the beginning of the array.  It must be determined manually by the user.  The index offset selects an entry in the array.  Since int variables are 2 bytes, the functions for accessing array of int variables perform an extra calculation to multiply the index by 2. 

Steep and load the program.  Then run it with the command "launch 2 0 0" since the program requires an input byte.  The program prints the results of several tests, including basic read and write and calculating the sum of the elements in each array. 

/* array.tea */ /* fixed-size stack-based array example */ #include <aCore.tea> #include <aPrint.tea> // arrays start at 3 to account for 2-byte return value // and 1-byte input parameter in main() // int return value and char input parameter // are just used as an example #define ARRAY_START 3 // create and initialize an array of 5 char variables #define ARRAY1 0x0000+ARRAY_START #define ARRAY1SIZE 5 char c0=100; char c1=101; char c2=102; char c3=103; char c4=104; // create and initialize an array of 5 int variables #define ARRAY2 0x0005+ARRAY_START #define ARRAY2SIZE 5 int a0=1000; int a1=1001; int a2=1002; int a3=1003; int a4=1004; char arrayRdChar(int addr, int x) { char val=0; asm { pushss 7 // push addr pushss 7 // push x adds // calculate addr+x pushsbax // get byte at stack[addr+x] popbs 1 // put byte in val } return val; } void arrayWrChar(int addr, int x, char data) { asm { pushsb 3 // push data pushss 6 // push x pushss 10 // push addr adds // calculate addr+x popbsax // put data into stack[addr+x] } } int arrayRdInt(int addr, int x) { int val=0; asm { pushss 8 // push addr pushss 8 // push x pushss 2 // push x adds // calculate x+x (same as 2*x) adds // calculate addr+2*x pushssax // get short at stack[addr+2*x] popss 2 // put short in val } return val; } void arrayWrInt(int addr, int x, int data) { asm { pushss 4 // push data pushss 8 // push x pushss 2 // push x adds // calculate x+x (same as 2*x) pushss 12 // push addr adds // calculate addr+2*x popssax // put data into stack[addr+x*x] } } void printvar(int n) { aPrint_IntDec(n); aPrint_Char('\n'); aCore_Sleep(200); } int main(char d) { char i,c; int k,sum; aPrint_String("Test char array...\n"); c=arrayRdChar(ARRAY1,2); printvar(c); arrayWrChar(ARRAY1,2,112); c=arrayRdChar(ARRAY1,2); printvar(c); sum=0; for (i=0; i<ARRAY1SIZE; i++) { sum=sum+arrayRdChar(ARRAY1,i); } printvar(sum); aPrint_String("Test int array...\n"); k=arrayRdInt(ARRAY2,2); printvar(k); arrayWrInt(ARRAY2,2,1102); k=arrayRdInt(ARRAY2,2); printvar(k); sum=0; for (i=0; i<ARRAY2SIZE; i++) { sum=sum+arrayRdInt(ARRAY2,i); } printvar(sum); return 0; }

Revision History:

  • 2003-07-17: Example created.
 
 
voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy
© Copyright 1994-2010 Acroname, Inc., Boulder, Colorado. All rights reserved.