/*

                           MySum.c

  Example of a C FUNCTION that is used in conjunction with a
  True BASIC driver.
  
  INPUT ARGUMENTS:
  
     Two integers
     
  RETURN VALUE:
  
     The sum of the integers

 ====================================================================== */

#include <exec/types.h>
#include "True BASIC:Assembly/TB.h"


/* =========================================== */
/* === ANSI function prototype definitions === */
/* =========================================== */
VOID MySum(LONG **arg, struct TBUserVector *uv);


/* ========================== */
/* === The routine itself === */
/* ========================== */
VOID CSum(LONG **arg, struct TBUserVector *uv)

{
   /* ========================================= */
   /* === Declare pointers to the arguments === */
   /* ========================================= */
   struct TBInt *arg1, *arg2, *ans;
   
   
   /* ======================================= */
   /* === Declare variable to hold answer === */
   /* ======================================= */
   LONG answer;
   
   
   /* ========================================= */
   /* === Read the arguments from the array === */
   /* ========================================= */
   arg1 = (struct TBInt *)*arg--;
   arg2 = (struct TBInt *)*arg--;
   ans  = (struct TBInt *)*arg;
   
   
   /* ================================================== */
   /* === Set the flag in return argument to integer === */
   /* ================================================== */
   ans ->exponent = -1;
   
   
   /* ===================================================== */
   /* ===   Check that the input arguments are proper   === */
   /* ===       integers and process accordingly        === */
   /* ===                                               === */
   /* === Verify that the output is within 16 bit range === */
   /* ===================================================== */
   if(arg1->exponent == -1 && arg2->exponent == -1)
   {
      answer = (LONG)arg1->integer + arg2->integer;
      
      if(answer < -32768 || answer > 32767)
         ans->integer = 0;
      else
         ans->integer = (SHORT)answer;
   }
   else
      ans->integer = 0;

}
