/*  8086util.c -- 8086 utilities */
/* from W. Duvall February 17, 1982  11:22 AM */
/* Last modified, L. Stewart, February 17, 1982  11:22 AM */

#include <8086defs.c>
#include <bcpldefs.c>

movebytes(nbytes, dest, source)
  int nbytes;
  char *dest, *source;
  {
  // BP+4 = nbytes
  // CX = dest
  // BX = source
  test (dest > source) && (dest < (source + nbytes))
  ifso
  while nbytes-- > 0 do
  dest[nbytes] = source[nbytes]
  ifnot
  {
  int i;
  for (i=0; ++i < nbytes; )
  dest[i] = source[i];
  };
  };

setblock(value, address, nbytes)
  short value;
  int nbytes;
  char *address;
  {
  #asm
  ; BP+4 = value
  ; CX = address, BX = Count
  PUSH ES;
  MOV AX,DS
  MOV ES,AX; set up string register
  CLD; direction = forwards
  
  MOV DI,CX; address
  MOV CX,BX; count
  MOV AX,[BP+4]
  REP STOSB;
  POP ES;
  #endasm
  };

clear(address, nbytes)
  int nbytes;
  char *address;
  {
  setblock(0, address, nbytes);
  };

int MIN(p1, p2)
  int p1, p2;
  {
  if p1 gr p2 then return p2;
  return p1;
  };

int MAX(p1, p2)
  int p1, p2;
  {
  if p1 gr p2 then return p1;
  return p2;
  };

short inrange(v1, v2, v3)
  int v1, v2, v3;
  {
  if v1 < v2 then resultis false;
  if v1 > v3 then resultis false;
  resultis true;
  };