home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / examples / avg / avg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  3.5 KB  |  98 lines

  1. /*-----------------------------------------------------------------------*
  2.  * Copyright (c) 1989-1993 by SAS Institute Inc., Cary NC, USA
  3.  * All Rights Reserved
  4.  *
  5.  * Name       : avg.c 
  6.  * Author     : Michael S. Whitcher
  7.  * Date       : 02June92
  8.  * Description: This is an example to demonstrate how to 
  9.  *              -- call assembly language routines from C
  10.  *              -- call assembly language routines from assembler
  11.  *              -- call into AmigaDos from assembler and C routines
  12.  *              -- call a registerized parameter link library routine 
  13.  *              -- setup an assembler routine to handle the passing 
  14.  *                   parameters on the stack and by register
  15.  * Sources    : avg.c avg2.c
  16.  *
  17.  * Buildline  : sc avg.c avg2.a link
  18.  *              Writes the final executable to the name 'avg'.
  19.  *
  20.  * Scoptions  : Here is a list of options that you can use to vary how
  21.  *                 the example is built.  It gives you the opportunity 
  22.  *                 to see how the final executable changes as you change
  23.  *                 options.
  24.  *
  25.  *              DEBUG=SYMBOL  -- build with symbolic debugging information
  26.  *                This will allow you to use cpr to step through the program
  27.  *                in source mode and watch its execution.
  28.  *                                        
  29.  *              PARAMETERS=REGISTERS  -- pass the parameters to function calls
  30.  *                in register instead of storing them on the stack.
  31.  *
  32.  *              NOSTACKCHECK -- do not add code to check for stack overflows
  33.  *
  34.  *              SMALLCODE  -- merge all code hunks together into one
  35.  *
  36.  *              SMALLDATA  -- merge all data hunks together into one
  37.  *
  38.  *              ADDSYMBOLS -- generate a H_SYMBOL hunk for use by cpr
  39.  *
  40.  *              STARTUP=cres  -- make the executable residentable
  41.  *              
  42.  *             
  43.  * Notes      : To build with registerized parameters add the 
  44.  *                parms=register option to the sc command line
  45.  *                or scoptions file.
  46.  *
  47.  *-----------------------------------------------------------------------*/
  48.  
  49. #include <dos/dos.h>
  50. #include <proto/dos.h>
  51. #include <pragmas/dos_pragmas.h>
  52. #include <string.h>
  53.  
  54. /* This is an assembler routine that calls AmigaDos to write a string to */
  55. /* an open AmigaDos file handle.                                         */
  56. extern int __asm prnts(register __d0 BPTR fh, register __a0 char *str);
  57.  
  58. /* This is an assembler routine that calculates the average of a set of  */
  59. /* numbers.  The routine is built so that it can be called with the      */
  60. /* parameters placed onto the stack or in register.                      */
  61. extern long avg(int argc, char **argv);
  62.  
  63. main(argc, argv)
  64. int argc;
  65. char **argv;
  66. {
  67.    BPTR fh;
  68.    long l;
  69.    char buf[16];
  70.    
  71.    /* Call into AmigaDos to get the name of the file handle that points */
  72.    /* to the output window from which we are executing.                 */
  73.    fh = Output();
  74.    
  75.    /* prnts() is an assembly routine that will write to the output      */
  76.    /* window.  avg() is an assembly routine that will compute the       */
  77.    /* average for us.                                                   */
  78.  
  79.    if (argc < 2)
  80.       prnts(fh, "Usage: average <int1> <int2> ...\n");
  81.    else
  82.    {
  83.       /* compute the average */
  84.       l = avg(argc-1, argv+1);
  85.       
  86.       /* convert the number to a string */
  87.       stcl_d(buf, l);
  88.       
  89.       /* output the average to the user */
  90.       prnts(fh, "average = ");
  91.       prnts(fh, buf);
  92.       prnts(fh, "\n");
  93.    }
  94.    
  95.    /* successful */
  96.    return 0;
  97. }
  98.