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

$INCLUDE(8086LIB.D)

$INCLUDE(QueueImpl.DEC)

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

; #include "env.h"

; extern int CallDebugger();

; extern int IWDC();

; extern int DWDC();

; extern int Zero();

; InitQueue(q)
←InitQueue:

;   struct Queue *q;
PUSH BP
MOV BP,SP
PUSH BX

;   {

;   Zero(q, lenQueue);
MOV BX,2

;	CX ← ←q
POP CX
PUSH CX
CALL ←Zero

;   };
MOV SP,BP
POP BP
RET;

; struct Element *Dequeue(q)
←Dequeue:

;   struct Queue *q;
PUSH BP
MOV BP,SP
PUSH BX

;   {

;   struct Element *item;
PUSH DX

;   IWDC();
CALL ←IWDC

;   item = q->head;

;	DI ← ←q
MOV DI,[BP-2]
MOV BX,[DI]

;	←item ← BX
POP DX
PUSH BX

;   if (item) q->head = item->next;

;	BX ← ←item
POP BX
PUSH BX
OR BX,BX
JZ X1

;	DI ← ←item
POP DI
PUSH DI
MOV BX,[DI]
MOV CX,BX

;	BX ← ←q
MOV BX,[BP-2]
MOV [BX],CX
X1:

;   DWDC();
CALL ←DWDC

;   return (item);

;	BX ← ←item
POP BX
PUSH BX
MOV SP,BP
POP BP
RET;

;   };

; Enqueue(q, item)
←Enqueue:

;   struct Queue *q;

;   struct Element *item;
PUSH BP
MOV BP,SP
PUSH CX
PUSH BX

;   {

;   if (item == 0) CallDebugger(ecQueue+1);

;	BX ← ←item
POP BX
PUSH BX
OR BX,BX
JNZ X2
MOV BX,02001X
CALL ←CallDebugger
X2:

;   IWDC();
CALL ←IWDC

;   if (q->head) q->tail->next = item;

;	DI ← ←q
MOV DI,[BP-2]
MOV BX,[DI]
OR BX,BX
JZ X3

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

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

;   else q->head = item;
JR X4
X3:

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

;	CX ← ←item
POP CX
PUSH CX
MOV [BX],CX
X4:

;   q->tail = item;

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

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

;   item->next = 0;

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

;   DWDC();
CALL ←DWDC

;   };
MOV SP,BP
POP BP
RET;

; int Unqueue(q, item)
←Unqueue:

;   struct Queue *q;

;   struct Element *item;
PUSH BP
MOV BP,SP
PUSH CX
PUSH BX

;   {

;   struct Element *p, *lastp;

;   if (item == 0) CallDebugger(ecQueue+2);
ADD SP,0FFFCX

;	BX ← ←item
MOV BX,[BP-4]
OR BX,BX
JNZ X5
MOV BX,02002X
CALL ←CallDebugger
X5:

;   IWDC();
CALL ←IWDC

;   p = q->head;

;	DI ← ←q
MOV DI,[BP-2]
MOV BX,[DI]

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

;   lastp = (struct Element *) q;

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

;	←lastp ← BX
POP DX
PUSH BX

;   while (p) {
X6:

;	BX ← ←p
MOV BX,[BP-6]
OR BX,BX
JZ X7

;     if (p==item) {

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

;	CX ← ←p
MOV CX,[BP-6]
CMP CX,BX
JNZ X8

;       lastp->next = p->next;

;	DI ← ←p
MOV DI,[BP-6]
MOV BX,[DI]
MOV CX,BX

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

;       if (p->next == 0) q->tail = lastp; /* item was the tail */

;	DI ← ←p
MOV DI,[BP-6]
MOV BX,[DI]
OR BX,BX
JNZ X9

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

;	CX ← ←lastp
POP CX
PUSH CX
MOV [BX+2],CX
X9:

;       DWDC();
CALL ←DWDC

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

;       };
X8:

;     lastp = p;

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

;	←lastp ← BX
POP DX
PUSH BX

;     p = p->next;

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

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

;     };
JR X6
X7:

;   DWDC();
CALL ←DWDC

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

;   };

; Externals Declared Here
PUBLIC ←InitQueue
PUBLIC ←Dequeue
PUBLIC ←Enqueue
PUBLIC ←Unqueue

C←CODE ENDS

; Number of Bytes of Code = 0F6X, (246)