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

$INCLUDE(8086LIB.D)

$INCLUDE(RingBuffer.DEC)

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

; #include	"env.h"

; struct CBuf {

;   char *buf;

;   int len, rp, wp;

;   };

; InitRingBuffer(p, buffer, length)
←InitRingBuffer:

;   struct CBuf *p;

;   char *buffer;

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

;   {

;   p->buf = buffer;

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

;	CX ← ←buffer
MOV CX,[BP-2]
MOV [BX],CX

;   p->len = length;

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

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

;   p->wp = p->rp = 0;

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

;	CX ← ←p
MOV CX,[BP+4]
XCHG BX,CX
MOV WORD PTR [BX+4],0
MOV BX,CX
MOV WORD PTR [BX+6],0

;   };
MOV SP,BP
POP BP
RET;

; int RingBufferEmpty(p)
←RingBufferEmpty:

;   struct CBuf *p;
PUSH BP
MOV BP,SP
PUSH BX

;   {

;   if (p->wp == p->rp) return(true);

;	BX ← ←p
POP BX
PUSH BX
MOV CX,[BX+6]

;	BX ← ←p
POP BX
PUSH BX
MOV AX,[BX+4]
CMP CX,AX
JNZ X1
MOV BX,0FFFFX
MOV SP,BP
POP BP
RET;

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

;   };

; int RingBufferFull(p)
←RingBufferFull:

;   struct CBuf *p;
PUSH BP
MOV BP,SP
PUSH BX

;   {

;   int np;
PUSH DX

;   np = p->wp + 1;

;	BX ← ←p
MOV BX,[BP-2]
MOV CX,[BX+6]
INC CX

;	←np ← CX
POP DX
PUSH CX

;   if (np >= p->len) np = 0;

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

;	BX ← ←np
POP BX
PUSH BX
CMP BX,CX
JL X2
MOV WORD PTR [BP-4],0
X2:

;   if (np == p->rp) return(true);

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

;	BX ← ←np
POP BX
PUSH BX
CMP BX,CX
JNZ X3
MOV BX,0FFFFX
MOV SP,BP
POP BP
RET;

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

;   };

; int WriteRingBuffer(p, c)
←WriteRingBuffer:

;   struct CBuf *p;

;   char c;
PUSH BP
MOV BP,SP
PUSH CX
PUSH BX

;   {

;   int np;

;   if (RingBufferFull(p)) return(false);
PUSH DX

;	BX ← ←p
MOV BX,[BP-2]
CALL ←RingBufferFull
OR BX,BX
JZ X4
XOR BX,BX
MOV SP,BP
POP BP
RET;
X4:

;   IWDC();
CALL ←IWDC

;   np = p->wp + 1;

;	BX ← ←p
MOV BX,[BP-2]
MOV CX,[BX+6]
INC CX

;	←np ← CX
POP DX
PUSH CX

;   if (np >= p->len) np = 0;

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

;	BX ← ←np
POP BX
PUSH BX
CMP BX,CX
JL X5
MOV WORD PTR [BP-6],0
X5:

;   p->buf[np] = c;

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

;	CX ← ←np
POP CX
PUSH CX
ADD BX,CX

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

;   p->wp = np;

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

;	CX ← ←np
POP CX
PUSH CX
MOV [BX+6],CX

;   DWDC();
CALL ←DWDC

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

;   };

; int ReadRingBuffer(p)
←ReadRingBuffer:

;   struct CBuf *p;
PUSH BP
MOV BP,SP
PUSH BX

;   {

;   int np, c;

;   if (RingBufferEmpty(p)) return(-1);
ADD SP,0FFFCX

;	BX ← ←p
MOV BX,[BP-2]
CALL ←RingBufferEmpty
OR BX,BX
JZ X6
MOV BX,0FFFFX
MOV SP,BP
POP BP
RET;
X6:

;   IWDC();
CALL ←IWDC

;   np = p->rp + 1;

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

;	←np ← CX
MOV [BP-4],CX

;   if (np >= p->len) np = 0;

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

;	BX ← ←np
MOV BX,[BP-4]
CMP BX,CX
JL X7
MOV WORD PTR [BP-4],0
X7:

;   c = p->buf[np];

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

;	CX ← ←np
MOV CX,[BP-4]
ADD BX,CX
MOV AL,[BX]
XOR AH,AH

;	←c ← AX
POP DX
PUSH AX

;   p->rp = np;

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

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

;   DWDC();
CALL ←DWDC

;   return(c & 0377);

;	BX ← ←c
POP BX
PUSH BX
AND BX,0FFX
MOV SP,BP
POP BP
RET;

;   };

; Externals Declared Here
PUBLIC ←InitRingBuffer
PUBLIC ←RingBufferEmpty
PUBLIC ←RingBufferFull
PUBLIC ←WriteRingBuffer
PUBLIC ←ReadRingBuffer

C←CODE ENDS

; Number of Bytes of Code = 0134X, (308)