;Alto->8086 small-c compiler rev 2.0
C←CODE SEGMENT

$INCLUDE(8086LIB.D)

$INCLUDE(signaller.DEC)

ASSUME CS:C←CODE, DS:C←DATA

; #include	<Signal.h>

; extern MyFrame();

; extern CallSwat();

; extern CallersFrame();

; extern ReturnLoc();

; extern Call0();

; extern Apply();

; extern Ugt();

; extern ByteBlt();

; struct enab {

;   int ensignal;

;   int enproc;

;   int enframe;

;   int enenframe;

;   int enid;

;   int encontinue;

;   struct Seal *enseal;

;   };

; struct sigvec {

;   int svused;

;   int svmaxEnabled;

;   struct enab sven[1];

;   };

; extern int	getsv;

; extern int	sigid;

; extern int	signame;

; SigInit(svec, sveclen, gvproc)
←SigInit:

;   struct sigvec *svec;

;   int sveclen;

;   int gvproc;
PUSH BP
MOV BP,SP
PUSH CX
PUSH BX

;   {

;   sigid = 0;
MOV ←sigid,0

;   signame = 037777;
MOV ←signame,03FFFX

;   getsv = gvproc;

;	BX ← ←gvproc
POP BX
PUSH BX
MOV ←getsv,BX

;   svec->svused = 0;

;	BX ← ←svec
MOV BX,[BP+4]
MOV WORD PTR [BX],0

;   svec->svmaxEnabled = (sveclen-lensigvec)/lenenab;

;	BX ← ←svec
MOV BX,[BP+4]

;	CX ← ←sveclen
MOV CX,[BP-2]
ADD CX,0FFFCX
MOV AX,CX
MOV CX,0EX
CWD
IDIV AX,CX
MOV [BX+2],AX

;   };
MOV SP,BP
POP BP
RET;

; int Enable(s, p, sl)
←Enable:

;   int s, p;

;   struct Seal *sl;
PUSH BP
MOV BP,SP
PUSH CX
PUSH BX

;   {

;   return(EnableWithFrame(s, p, sl, MyFrame()));

;	BX ← ←s
MOV BX,[BP+4]
PUSH BX

;	BX ← ←p
MOV BX,[BP-2]
PUSH BX
CALL ←MyFrame

;	CX ← ←sl
MOV CX,[BP-4]
CALL ←EnableWithFrame
ADD SP,4
MOV SP,BP
POP BP
RET;

;   };

; int EnableWithFrame(s, p, sl, f)
←EnableWithFrame:

;   int s;		/* signal */

;   int p;		/* proc to call */

;   struct Seal *sl;

;   int f;
PUSH BP
MOV BP,SP
PUSH CX
PUSH BX

;   {

;   struct sigvec *sv;

;   struct enab *en;
ADD SP,0FFFCX

;   sv = Call0(getsv);
MOV BX,←getsv
CALL ←Call0

;	←sv ← BX
MOV [BP-6],BX

;   if (sv->svused == sv->svmaxEnabled) {

;	DI ← ←sv
MOV DI,[BP-6]
MOV BX,[DI]

;	CX ← ←sv
MOV CX,[BP-6]
MOV DI,CX
MOV CX,[DI+2]
CMP BX,CX
JNZ X1

;     if (purgesv() >= sv->svmaxEnabled) CallSwat(ecSignal+1);
CALL ←purgesv

;	CX ← ←sv
MOV CX,[BP-6]
MOV DI,CX
MOV CX,[DI+2]
CMP BX,CX
JL X2
MOV BX,03001X
CALL ←CallSwat
X2:

;     };
X1:

;   en = &sv->sven[sv->svused];

;	BX ← ←sv
MOV BX,[BP-6]
ADD BX,4

;	DI ← ←sv
MOV DI,[BP-6]
MOV CX,[DI]
MOV AX,0EX
IMUL AX,CX
ADD BX,AX

;	←en ← BX
POP DX
PUSH BX

;   sv->svused += 1;

;	BX ← ←sv
MOV BX,[BP-6]
INC WORD PTR [BX]

;   en->ensignal = s;

;	BX ← ←en
POP BX
PUSH BX

;	CX ← ←s
MOV CX,[BP+6]
MOV [BX],CX

;   en->enproc = p;

;	BX ← ←en
POP BX
PUSH BX

;	CX ← ←p
MOV CX,[BP+4]
MOV [BX+2],CX

;   en->enenframe = f;

;	BX ← ←en
POP BX
PUSH BX

;	CX ← ←f
MOV CX,[BP-4]
MOV [BX+6],CX

;   en->enframe = CallersFrame(f);

;	BX ← ←en
POP BX
PUSH BX
PUSH BX

;	BX ← ←f
MOV BX,[BP-4]
CALL ←CallersFrame
MOV CX,BX
POP BX
MOV [BX+4],CX

;   en->enseal = sl;

;	BX ← ←en
POP BX
PUSH BX

;	CX ← ←sl
MOV CX,[BP-2]
MOV [BX+12],CX

;   en->encontinue = ReturnLoc(f);

;	BX ← ←en
POP BX
PUSH BX
PUSH BX

;	BX ← ←f
MOV BX,[BP-4]
CALL ←ReturnLoc
MOV CX,BX
POP BX
MOV [BX+10],CX

;   sigid += 1;
INC ←sigid

;   en->enid = sigid;

;	BX ← ←en
POP BX
PUSH BX
MOV CX,←sigid
MOV [BX+8],CX

;   sl->slauth = sealseal;

;	BX ← ←sl
MOV BX,[BP-2]
MOV WORD PTR [BX],0B57EX

;   sl->slid = sigid;

;	BX ← ←sl
MOV BX,[BP-2]
MOV CX,←sigid
MOV [BX+2],CX

;   return(0);
XOR BX,BX
MOV SP,BP
POP BP
RET;

;   };

; Disable(sl)
←Disable:

;   struct Seal *sl;
PUSH BP
MOV BP,SP
PUSH BX

;   {

;   sl->slauth = 0;

;	BX ← ←sl
POP BX
PUSH BX
MOV WORD PTR [BX],0

;   };
MOV SP,BP
POP BP
RET;

; Signal(s, c)
←Signal:

;   int s;		/* Signal */

;   int c;		/* unspecified */
PUSH BP
MOV BP,SP
PUSH CX
PUSH BX

;   {

;   struct enab *en;

;   struct sigvec *sv;

;   int enindex;

;   int enabres;

;   int ent;

;   int result;

;   int argv[3];
ADD SP,0FFEEX

;   sv = Call0(getsv);
MOV BX,←getsv
CALL ←Call0

;	←sv ← BX
MOV [BP-8],BX

;   enindex = purgesv();
CALL ←purgesv

;	←enindex ← BX
MOV [BP-10],BX

;   for (;;) {
X3:

;     enabres = true;
MOV WORD PTR [BP-12],0FFFFX

;     en = finden(s, &enindex);
;&←enindex
LEA BX,[BP-10]

;	CX ← ←s
MOV CX,[BP-2]
CALL ←finden

;	←en ← BX
MOV [BP-6],BX

;     if (en==0) CallSwat(ecSignal+2);

;	BX ← ←en
MOV BX,[BP-6]
OR BX,BX
JNZ X5
MOV BX,03002X
CALL ←CallSwat
X5:

;     argv[0] = s;

;	BX ← ←s
MOV BX,[BP-2]

;	←argv ← BX
POP DX
PUSH BX

;     argv[1] = c;

;	BX ← ←c
MOV BX,[BP-4]

;	←argv+2 ← BX
MOV [BP-20],BX

;     argv[2] = (int) en->enseal;

;	BX ← ←en
MOV BX,[BP-6]
MOV CX,[BX+12]

;	←argv+4 ← CX
MOV [BP-18],CX

;     result = Apply(argv, en->enproc, 3);
;&←argv
LEA BX,[BP-22]
PUSH BX

;	BX ← ←en
MOV BX,[BP-6]
MOV CX,[BX+2]
MOV BX,3
CALL ←Apply
POP DX

;	←result ← BX
MOV [BP-16],BX

;     switch (result) {

;	BX ← ←result
MOV BX,[BP-16]
JR X6

;       case REJECT: continue;	/* look for next catch phrase */
X8:
JR X3

;       case RESUME: return;
X9:
MOV SP,BP
POP BP
RET;

;       case RETRY: enabres = false;
X10:
MOV WORD PTR [BP-12],0

;       case CONTINUE:
X11:

;         unwind(enindex, enabres);

;	BX ← ←enabres
MOV BX,[BP-12]

;	CX ← ←enindex
MOV CX,[BP-10]
CALL ←unwind

;         break;
JR X7

;       default: CallSwat(ecSignal+3);
X12:
MOV BX,03003X
CALL ←CallSwat

;       };
JR X7
X6:
MOV AL,BH
OR AL,AL
JNZ X12
MOV AL,BL
CMP AL,1
JZ X8
CMP AL,2
JZ X9
CMP AL,3
JZ X10
CMP AL,4
JZ X11
JR X12
X7:

;     };
JMP X3
X4:

;   };
MOV SP,BP
POP BP
RET;

; int Code()
←Code:
PUSH BP
MOV BP,SP

;   {

;   signame += 1;
INC ←signame

;   return(signame);
MOV BX,←signame
MOV SP,BP
POP BP
RET;

;   };

; static int purgesv()
←purgesv:
PUSH BP
MOV BP,SP

;   {

;   int f, prevf, invalid;

;   int enf, dest, i;

;   struct sigvec *sv;

;   struct enab *en;

;   struct Seal *sl;
ADD SP,0FFEEX

;   sv = Call0(getsv);
MOV BX,←getsv
CALL ←Call0

;	←sv ← BX
MOV [BP-14],BX

;   f = CallersFrame(MyFrame());
CALL ←MyFrame
CALL ←CallersFrame

;	←f ← BX
MOV [BP-2],BX

;   prevf = 0;
MOV WORD PTR [BP-4],0

;   invalid = false;
MOV WORD PTR [BP-6],0

;   for (i = (sv->svused) - 1; i >= 0; i -= 1) {

;	DI ← ←sv
MOV DI,[BP-14]
MOV BX,[DI]
DEC BX

;	←i ← BX
MOV [BP-12],BX
X15:

;	BX ← ←i
MOV BX,[BP-12]
CMP BX,0
JL X14
JR X13
X16:
DEC WORD PTR [BP-12]
JR X15
X13:

;     en = &sv->sven[i];

;	BX ← ←sv
MOV BX,[BP-14]
ADD BX,4

;	CX ← ←i
MOV CX,[BP-12]
MOV AX,0EX
IMUL AX,CX
ADD BX,AX

;	←en ← BX
MOV [BP-16],BX

;     sl = en->enseal;

;	BX ← ←en
MOV BX,[BP-16]
MOV CX,[BX+12]

;	←sl ← CX
POP DX
PUSH CX

;     if ((sl->slauth != sealseal) || (sl->slid != en->enid)) {

;	DI ← ←sl
POP DI
PUSH DI
MOV BX,[DI]
CMP BX,0B57EX
JNZ X19

;	BX ← ←sl
POP BX
PUSH BX
MOV CX,[BX+2]

;	BX ← ←en
MOV BX,[BP-16]
MOV AX,[BX+8]
CMP CX,AX
X19:
X18:
JZ X17

;       en->enid = 0;

;	BX ← ←en
MOV BX,[BP-16]
MOV WORD PTR [BX+8],0

;       invalid = true;
MOV WORD PTR [BP-6],0FFFFX

;       continue;
JR X16

;       };
X17:

;     enf = en->enframe;

;	BX ← ←en
MOV BX,[BP-16]
MOV CX,[BX+4]

;	←enf ← CX
MOV [BP-8],CX

;     while (Ugt(enf, f)) f = CallersFrame(f);
X20:

;	BX ← ←f
MOV BX,[BP-2]

;	CX ← ←enf
MOV CX,[BP-8]
CALL ←Ugt
OR BX,BX
JZ X21

;	BX ← ←f
MOV BX,[BP-2]
CALL ←CallersFrame

;	←f ← BX
MOV [BP-2],BX
JR X20
X21:

;     if (Ugt(prevf, enf) || Ugt(f, enf)) {

;	BX ← ←enf
MOV BX,[BP-8]

;	CX ← ←prevf
MOV CX,[BP-4]
CALL ←Ugt
OR BX,BX
JR $+5
X14:
JMP X25
JNZ X24

;	BX ← ←enf
MOV BX,[BP-8]

;	CX ← ←f
MOV CX,[BP-2]
CALL ←Ugt
OR BX,BX
X24:
X23:
JZ X22

;       en->enid = 0;

;	BX ← ←en
MOV BX,[BP-16]
MOV WORD PTR [BX+8],0

;       invalid = true;
MOV WORD PTR [BP-6],0FFFFX

;       };

;     else prevf = f;
JR X26
X22:

;	BX ← ←f
MOV BX,[BP-2]

;	←prevf ← BX
MOV [BP-4],BX
X26:

;     };
JMP X16
X25:

;   dest = 0;
MOV WORD PTR [BP-10],0

;   if (invalid) {

;	BX ← ←invalid
MOV BX,[BP-6]
OR BX,BX
JZ X27

;     for (i=0; i < sv->svused; i+= 1) {
MOV WORD PTR [BP-12],0
X30:

;	DI ← ←sv
MOV DI,[BP-14]
MOV BX,[DI]

;	CX ← ←i
MOV CX,[BP-12]
CMP CX,BX
JGE X29
JR X28
X31:
INC WORD PTR [BP-12]
JR X30
X28:

;       en = &sv->sven[i];

;	BX ← ←sv
MOV BX,[BP-14]
ADD BX,4

;	CX ← ←i
MOV CX,[BP-12]
MOV AX,0EX
IMUL AX,CX
ADD BX,AX

;	←en ← BX
MOV [BP-16],BX

;       if (en->enid == 0) continue;  /* invalid */

;	BX ← ←en
MOV BX,[BP-16]
MOV CX,[BX+8]
OR CX,CX
JNZ X32
JR X31
X32:

;       if (dest != i) {

;	BX ← ←i
MOV BX,[BP-12]

;	CX ← ←dest
MOV CX,[BP-10]
CMP CX,BX
JZ X33

;         ByteBlt(&sv->sven[dest], &sv->sven[i], lenenab);

;	BX ← ←sv
MOV BX,[BP-14]
ADD BX,4

;	CX ← ←dest
MOV CX,[BP-10]
MOV AX,0EX
IMUL AX,CX
ADD BX,AX
PUSH BX

;	BX ← ←sv
MOV BX,[BP-14]
ADD BX,4

;	CX ← ←i
MOV CX,[BP-12]
MOV AX,0EX
IMUL AX,CX
ADD BX,AX
PUSH BX
MOV BX,0EX
POP CX
CALL ←ByteBlt
POP DX

;         };
X33:

;       dest += 1;
INC WORD PTR [BP-10]

;       };
JR X31
X29:

;     sv->svused = dest;

;	BX ← ←sv
MOV BX,[BP-14]

;	CX ← ←dest
MOV CX,[BP-10]
JR $+5
X27:
JMP X34
MOV [BX],CX

;     };
X34:

;   return (sv->svused);

;	DI ← ←sv
MOV DI,[BP-14]
MOV BX,[DI]
MOV SP,BP
POP BP
RET;

;   };

; static struct enab *finden(s, lvindex)
←finden:

;   int s, *lvindex;
PUSH BP
MOV BP,SP
PUSH CX
PUSH BX

;   {

;   struct sigvec *sv;

;   struct enab *en;

;   int i;
ADD SP,0FFFAX

;   sv = Call0(getsv);
MOV BX,←getsv
CALL ←Call0

;	←sv ← BX
MOV [BP-6],BX

;   for (i = (*lvindex) - 1; i >= 0; i -= 1) {

;	DI ← ←lvindex
MOV DI,[BP-4]
MOV BX,[DI]
DEC BX

;	←i ← BX
POP DX
PUSH BX
X37:

;	BX ← ←i
POP BX
PUSH BX
CMP BX,0
JL X36
JR X35
X38:
DEC WORD PTR [BP-10]
JR X37
X35:

;     en = &sv->sven[i];

;	BX ← ←sv
MOV BX,[BP-6]
ADD BX,4

;	CX ← ←i
POP CX
PUSH CX
MOV AX,0EX
IMUL AX,CX
ADD BX,AX

;	←en ← BX
MOV [BP-8],BX

;     if ((en->ensignal == s) || (en->ensignal == ANY)) {

;	DI ← ←en
MOV DI,[BP-8]
MOV BX,[DI]

;	CX ← ←s
MOV CX,[BP-2]
CMP BX,CX
JZ X41

;	DI ← ←en
MOV DI,[BP-8]
MOV BX,[DI]
OR BX,BX
X41:
X40:
JNZ X39

;       *lvindex = i;

;	BX ← ←lvindex
MOV BX,[BP-4]

;	CX ← ←i
POP CX
PUSH CX
MOV [BX],CX

;       return (en);

;	BX ← ←en
MOV BX,[BP-8]
MOV SP,BP
POP BP
RET;

;       };
X39:

;     };
JR X38
X36:

;   *lvindex = (-1);

;	BX ← ←lvindex
MOV BX,[BP-4]
MOV WORD PTR [BX],0FFFFX

;   return (0);
XOR BX,BX
MOV SP,BP
POP BP
RET;

;   };

; static unwind(toenindex, result)
←unwind:

;   int toenindex, result;
PUSH BP
MOV BP,SP
PUSH CX
PUSH BX

;   {

;   struct sigvec *sv;

;   struct enab *toen, *en;

;   int id, index, toframe, toenframe, prevframe, nextframe;

;   int argv[3];
ADD SP,0FFE8X

;   sv = Call0(getsv);
MOV BX,←getsv
CALL ←Call0

;	←sv ← BX
MOV [BP-6],BX

;   toen = &sv->sven[toenindex];

;	BX ← ←sv
MOV BX,[BP-6]
ADD BX,4

;	CX ← ←toenindex
MOV CX,[BP-2]
MOV AX,0EX
IMUL AX,CX
ADD BX,AX

;	←toen ← BX
MOV [BP-8],BX

;   id = toen->enid;

;	BX ← ←toen
MOV BX,[BP-8]
MOV CX,[BX+8]

;	←id ← CX
MOV [BP-12],CX

;   index = purgesv();
CALL ←purgesv

;	←index ← BX
MOV [BP-14],BX

;   if ((index<=toenindex) || (toen->enid != id)) CallSwat(ecSignal+4);

;	BX ← ←toenindex
MOV BX,[BP-2]

;	CX ← ←index
MOV CX,[BP-14]
CMP CX,BX
JLE X44

;	BX ← ←toen
MOV BX,[BP-8]
MOV CX,[BX+8]

;	BX ← ←id
MOV BX,[BP-12]
CMP CX,BX
JZ X43
X44:
MOV AL,1
JR X45
X43:
XOR AL,AL
X45:
OR AL,AL
JZ X42
MOV BX,03004X
CALL ←CallSwat
X42:

;   toframe = toen->enframe;

;	BX ← ←toen
MOV BX,[BP-8]
MOV CX,[BX+4]

;	←toframe ← CX
MOV [BP-16],CX

;   toenframe = toen->enenframe;

;	BX ← ←toen
MOV BX,[BP-8]
MOV CX,[BX+6]

;	←toenframe ← CX
MOV [BP-18],CX

;   prevframe = MyFrame();
CALL ←MyFrame

;	←prevframe ← BX
MOV [BP-20],BX

;   en = finden(UNWIND, &index);
;&←index
LEA BX,[BP-14]
MOV CX,1
CALL ←finden

;	←en ← BX
MOV [BP-10],BX

;   for (;;) {
X46:

;     nextframe = CallersFrame(prevframe);

;	BX ← ←prevframe
MOV BX,[BP-20]
CALL ←CallersFrame

;	←nextframe ← BX
MOV [BP-22],BX

;     while ((index>toenindex) && (en->enframe == nextframe)) {
X48:

;	BX ← ←toenindex
MOV BX,[BP-2]

;	CX ← ←index
MOV CX,[BP-14]
CMP CX,BX
JLE X50

;	BX ← ←en
MOV BX,[BP-10]
MOV CX,[BX+4]

;	BX ← ←nextframe
MOV BX,[BP-22]
CMP CX,BX
JNZ X50
MOV AL,1
JR X51
X50:
XOR AL,AL
X51:
OR AL,AL
JZ X49

;       argv[0] = UNWIND;
MOV WORD PTR [BP-28],1

;       argv[1] = 0;
MOV WORD PTR [BP-26],0

;       argv[2] = (int) en->enseal;

;	BX ← ←en
MOV BX,[BP-10]
MOV CX,[BX+12]

;	←argv+4 ← CX
MOV [BP-24],CX

;       Apply(argv, en->enproc, 3);
;&←argv
LEA BX,[BP-28]
PUSH BX

;	BX ← ←en
MOV BX,[BP-10]
MOV CX,[BX+2]
MOV BX,3
CALL ←Apply
POP DX

;       en = finden(UNWIND, &index);
;&←index
LEA BX,[BP-14]
MOV CX,1
CALL ←finden

;	←en ← BX
MOV [BP-10],BX

;       };
JR X48
X49:

;     if (nextframe==toframe) break;

;	BX ← ←toframe
MOV BX,[BP-16]

;	CX ← ←nextframe
MOV CX,[BP-22]
CMP CX,BX
JNZ X52
JR X47
X52:

;     prevframe = nextframe;

;	BX ← ←nextframe
MOV BX,[BP-22]

;	←prevframe ← BX
MOV [BP-20],BX

;     };
JR X46
X47:

;   returnto(toenframe, toframe, toen->encontinue, result);

;	BX ← ←toenframe
MOV BX,[BP-18]
PUSH BX

;	BX ← ←toframe
MOV BX,[BP-16]
PUSH BX

;	BX ← ←toen
MOV BX,[BP-8]
MOV CX,[BX+10]

;	BX ← ←result
MOV BX,[BP-4]
CALL ←returnto
ADD SP,4

;   };
MOV SP,BP
POP BP
RET;

; Externals Declared Here
PUBLIC ←SigInit
PUBLIC ←Enable
PUBLIC ←EnableWithFrame
PUBLIC ←Disable
PUBLIC ←Signal
PUBLIC ←Code

C←CODE ENDS

; Number of Bytes of Code = 04A6X, (1190)