/* HS08IB1.C 
 * Laufzeitmessug von Programmabschnitten
 * Beschleunigung von BIOS-Aufrufen in C
 */
#include <stdio.h>
#include <time.h>
#include <bios.h>
#include <conio.h>

#define SPEICHERGROESSE(x) \
__asm              \
{                  \
  __asm INT 12H     \
  __asm MOV x##,AX \
}                  
 
unsigned int SpeicherGroesse(void)
{ 
  unsigned int rax;                               
  __asm INT 12H   
  __asm MOV rax,AX
  return rax;
}
              
enum COUNTERMODE { CTR_START, CTR_STOP, CTR_STOPREAD, CTR_SUSPEND, CTR_RESUME}; 

double counter(enum COUNTERMODE mode)
{
  static clock_t start;
  clock_t finish;
  static double duration;

  switch (mode)
  {
  default: return duration;
  case CTR_START:
    start = clock();
    duration = 0.0;
    return duration;
  case CTR_STOP:
    finish = clock();
    duration = duration + (double)(finish - start) / CLOCKS_PER_SEC;
    return duration;
  case CTR_STOPREAD:
    finish = clock();
    duration = duration + (double)(finish - start) / CLOCKS_PER_SEC;
    printf( "%f Sekunden\n", duration );
    return duration;
  case CTR_SUSPEND:
    finish = clock();
    duration = duration + (double)(finish - start) / CLOCKS_PER_SEC;
    return duration;
  case CTR_RESUME:
    start = clock();
    return duration;
  }
}
                
void main(void)
{
  union REGS r;
  unsigned long i;       
  unsigned int s;

  counter(CTR_START);
  for (i=0; i<1000000L; i++)
    SPEICHERGROESSE(s);
  counter(CTR_STOPREAD);
  SPEICHERGROESSE(s);
  printf("Der Speicher ist %u kByte groá\n", s);

  counter(CTR_START);
  for (i=0; i<1000000L; i++)
    SpeicherGroesse();
  counter(CTR_STOPREAD);
  printf("Der Speicher ist %u kByte groá\n", SpeicherGroesse());

  counter(CTR_START);
  for (i=0; i<1000000L; i++)
    int86(0x12,&r,&r);
  counter(CTR_STOPREAD);
  printf("Der Speicher ist %u kByte groá\n", r.x.ax);
  getch();
}     


