home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 171_01 / timemark.txt < prev    next >
Text File  |  1983-09-18  |  9KB  |  298 lines

  1. /*****************************************************************************/
  2.  
  3. This file contains (4) source code files which are to be extracted and
  4. made into individual files.  Their names are "gtest.c", "gtime.c", "times.c",
  5. and "sysint.asm".  Using the Personal Editor, use Alt-B to mark the begin-
  6. ning and end of a file.  Then hit escape and enter for example "edit gtest.c"
  7. or "edit c:gtest.c"  Since this is a new file no text will appear until you
  8. hit escape to return to the text entry mode and hit Alt-Z.  The marked
  9. portion will now load.    Hit enter and put "save" on the command line.  Now
  10. do a "quit".  You will now return to the main file.  Now do an Alt-U to remove
  11. the marked area.  Repeat the marking, naming and saving out for each of the
  12. files.    Next use your Lattice-C compiler to obtain a ".obj" file for the
  13. ".c" source files and the Macro Assembler to obtain a ".obj" file for the
  14. ".asm" source.  Finally, enter "Link c+gtest+gtime+times+sysint".  I called
  15. the final executable file "Timemark".  We thank Ed Keating who is the C-Sig
  16. co-ordinator for the Northern Illinois IBM-PC User's Group" for the writing
  17. of these routines.  I made this single file put-together for the convenience
  18. of only having to download one file from this BBS.
  19.                              ^^^ Dick Stone ^^^
  20.  
  21. /*****************************************************************************/
  22. /*
  23.  * .title gtest.c---Timestamp program for testing Lattice C
  24.  */
  25. #include <stdio.h>
  26. main(argc,argv)
  27. int argc;
  28. char *argv[];
  29. {
  30.     looptest(); /* perform simple loop test */
  31.     addints();  /* add integers test */
  32.     fptest();   /* floating point test */
  33.     strings();  /* string concatenation */
  34.     tables();   /* table lookup test */
  35.  
  36.     printf("End benchmarks\n");
  37. }
  38. /*
  39.  * this is a routine to print the timestamped differences to standard output
  40.  */
  41. printimes(t1,t2)
  42. int t1[8],t2[8];    /* timestamp buffers */
  43. {
  44.     long d,tdiff(); /* for time difference calculations */
  45.     char tsbuf[20]; /* used to format time info */
  46.     char *ftime();    /* formats a timestamp into a buffer */
  47.  
  48.     printf("Time started was %s\n",ftime(t1,tsbuf));
  49.     printf("Time ended   was %s\n",ftime(t2,tsbuf));
  50.     d = tdiff(t1,t2);
  51.     printf("The difference in times in Milliseconds is %ld\n",d);
  52.  
  53. } /* end of printimes*/
  54. looptest()    /* basic loop test */
  55. {
  56.     int i,t1[8],t2[8];      /* timestamp buffers */
  57.  
  58.     gtime(t1);    /* get time one. */
  59.     for(i =0; i < 10000; i++);
  60.     gtime(t2);
  61.     printf("\nSimple loop test\n");
  62.     printimes(t1,t2);
  63. }
  64. addints()    /* add integers test */
  65. {
  66.  
  67.     int i,t1[8],t2[8];      /* timestamp buffers */
  68.     printf("\nAdding integers loop to 32767\n");
  69.     gtime(t1);
  70.     i = 0;
  71.     while(i < 32767) i++;
  72.     gtime(t2);
  73.     printimes(t1,t2);
  74. }
  75. fptest()    /* floating point test */
  76. {
  77.     int i,t1[8],t2[8];      /* timestamp buffers */
  78.     double    a,b,c;
  79.  
  80.     printf("\nFloating point benchmark\n");
  81.     gtime(t1);
  82.     a = 0; b = 1234.56; c = 78.9;
  83.     for(i = 0; i < 10000; i++) {
  84.      a=b*c;
  85.      a=b/c;
  86.     }
  87.     gtime(t2);
  88.     printimes(t1,t2);
  89. }
  90. strings()
  91. {
  92.     int i,t1[8],t2[8];      /* timestamp buffers */
  93.     char *pa,*pb,*pc;
  94.     static char sa[] = "This is a string";
  95.     static char sb[] = "This is a longer string with lots of words in it";
  96.     char sc[100];
  97.  
  98.     printf("\nString concatenation benchmark\n");
  99.     gtime(t1);
  100.     for(i = 0; i < 10000; i++) {
  101.      pc = &sc[0];
  102.      pa = &sa[0];
  103.      pb = &sb[0];
  104.      while(*pc++ = *pa++); /* copy up to first null */
  105.      pc--;     /* back up over trailing null */
  106.      while(*pc++ = *pb++);     /* add in the second string */
  107.     }
  108.     gtime(t2);
  109.     printimes(t1,t2);
  110. }
  111. tables()    /* table lookup tests */
  112. {
  113.     int i,j,t1[8],t2[8];        /* timestamp buffers */
  114.     static int data[25] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  115.         11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  116.         21, 22, 23, 24, 25 };
  117.     int array[25];
  118.  
  119.     printf("\nTable lookup\n");
  120.     gtime(t1);
  121.     for(i = 0; i < 1000; i++)
  122.      for(j = 0; j < 25; j++) array[j] = data[j];
  123.     gtime(t2);
  124.     printimes(t1,t2);
  125. }      /*  End of gtest.c  */
  126. /*****************************************************************************/
  127. /*
  128. ;
  129. ;      .title gtime.c
  130. ;
  131. ; Index gtime -- get system time into raw 8 word format
  132. ;
  133. ; Usage
  134. ;    gtime(buffer);
  135. ;
  136. ; In
  137. ;    int  buffer[8];     ; 8 word buffer for timestamp
  138. ;
  139. ; Out
  140. ;    int  buffer[8];     ; 8 word buffer will contain timestamp
  141. ;
  142. ; Description
  143. ;    This routine will get the system time and date and return it
  144. ;    in the 8 word buffer provided.
  145. ;    The buffer contains year (1980-2099), month 1-12, day 1-32
  146. ;    hour(0 - 23) minute (0-59) second (0-59) , milliseconds (0 - 999)
  147. ;    and the timer resolution in milliseconds.( On pc = 055)
  148. ;
  149. ; status
  150. ;    returns pointer to start of timerstruct.
  151. ;
  152. ; bugs
  153. ;    Uses Sysint to talk to Pcdos
  154. ;
  155. ; Updates
  156. ;     date        vers    who    Description
  157. ;     27-aug-83    0001    EJK    Initial coding
  158. ;
  159. ;-
  160. */
  161. #include <stdio.h>
  162. struct dosstruct {
  163.        char al,ah;
  164.        char bl,bh;
  165.        char cl,ch;
  166.        char dl,dh;
  167.        };
  168. struct dosregs {
  169.     int ax,bx,cx,dx;
  170.     };    /* used for casting */
  171. struct timerbuf {
  172.     int year,month,day,hour,minute,second,millisec,timres;
  173.     };
  174. #define TIMEFUNCTION 0x2c     /* time subfunction */
  175. #define DATEFUNCTION 0x2a    /* date subfunction */
  176. #define DOSINT 0x21        /* dos interrupt code */
  177. #define TIMRES 055        /* milliseconds resolution */
  178.  
  179. struct timerbuf *gtime(timbuf)
  180. struct timerbuf *timbuf;
  181. {
  182.     struct dosstruct dosiobuf;
  183.     struct dosstruct *in = &dosiobuf;
  184.     struct dosstruct *out= &dosiobuf;
  185. /* first, get the date. with system function 2a to dos */
  186.     in->ah = DATEFUNCTION;
  187.     sysint(DOSINT,in,out); /* do it! */
  188.     timbuf->year = ((struct dosregs *)out)->cx;    /* cast for 16bit */
  189.     timbuf->month = out->dh;
  190.     timbuf->day = out->dl;
  191. /* now, the time */
  192.     in->ah = TIMEFUNCTION;
  193.     sysint(DOSINT,in,out);
  194.     timbuf->hour = out->ch;
  195.     timbuf->minute = out->cl;
  196.     timbuf->second = out->dh;
  197.     timbuf->millisec = out->dl * 10;    /* make into milliseconds */
  198.     timbuf->timres = TIMRES;
  199.     return(timbuf); /* return pointer to his struct */
  200. }   /* end of gtime */
  201. /*****************************************************************************/
  202. /* .title   times.c
  203.  * tdiff -- return the time difference between a start time and an end time
  204.  * long tdiff(start,end )
  205.  * where: start and end are 8 word timestamp buffers
  206.  */
  207. long tdiff(t1,t2)
  208. int t1[8],t2[8];    /* timestamp buffers */
  209. {
  210.     long d; /* work variable */
  211.     d = t2[3] - t1[3];
  212.     d = d*60 + t2[4] - t1[4];
  213.     d = d*60 + t2[5] - t1[5];
  214.     d = d*1000 + t2[6] - t1[6];
  215.     return(d);
  216. }
  217. /*
  218.  * ftime -- formats a timestamp into a buffer in the form
  219.  * hh:mm:ss.msc     ( null terminated )
  220.  * returns a pointer to the start of the buffer
  221.  */
  222. char *ftime(t1,buf)
  223. int t1[];    /* timestamp */
  224. char *buf;    /* buffer */
  225. {
  226.        sprintf(buf,"%2.2d:%2.2d:%2.2d.%3.3d\0",t1[3],t1[4],t1[5],t1[6]);
  227.        return(buf);    /* return pointer to start */
  228. }      /* end of times.c */
  229. /*****************************************************************************/
  230. ; page 66,132
  231. ;+
  232. ;    .title sysint.asm
  233. ; index  system interrupt call function   sysint()
  234. ;
  235. ; Usage
  236. ;    sysint(inum,&inreg,&outreg);
  237. ;
  238. ; in
  239. ;    int    inum;        ; interrupt number to execute
  240. ;    int    inreg[4];    ; input registers ax,bx,cx,dx
  241. ;
  242. ; out
  243. ;    int    outreg[4];    ; registers returned ax,bx,cx,dx
  244. ;
  245. ; Description
  246. ;    This is a system interface call to allow system intrinsic functions
  247. ;      to be called from C. Parameters are passed via the register values
  248. ;      stored in inreg for input to the system call and returned in the
  249. ;      outreg struct. The default values for the segment registers are the
  250. ;      same as C routines.
  251. ;
  252. ; status
  253. ;    ax register is returned as status
  254. ;
  255. ; bugs
  256. ;    low level internal routine must be modified to be ported.
  257. ;
  258. ; Updates
  259. ;
  260. ;    date        vers    who    description
  261. ;    15-aug-83    0001    EJK    Added documemtation
  262. ;-
  263. pgroup    group    prog
  264. prog    segment byte    public 'prog'
  265.     assume    cs:pgroup
  266.     public    sysint
  267.  
  268. sysint    proc    near
  269.     push bp     ;save bp
  270.     mov bp,sp    ;sp->bp
  271.     mov ax,[bp]+4    ;get int#
  272.     mov cs:itm+1,al ;set int#
  273.     mov si,[bp]+6    ;in struc
  274.     mov ax,[si]    ;set ax
  275.     mov bx,[si]+2    ;set bx
  276.     mov cx,[si]+4    ;set cx
  277.     mov dx,[si]+6    ;set dx
  278.     push bp     ;save bp2
  279. itm    equ  this byte    ; 'this byte' is a keyword.
  280.     int 16        ;interrupt
  281.     pop bp        ;restore bp2
  282.     mov si,[bp]+8    ;out struc
  283.     mov [si],ax    ;ret ax
  284.     mov [si]+2,bx    ;ret bx
  285.     mov [si]+4,cx    ;ret cx
  286.     mov [si]+6,dx    ;ret dx
  287.     pop bp        ;restore bp
  288.     ret        ;return
  289. sysint    endp
  290. prog    ends
  291.     end
  292. /*****************************************************************************/
  293. 
  294.     ret        ;return
  295. sysint    endp
  296. prog    ends
  297.     end
  298. /**********************