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

$INCLUDE(8086LIB.D)

$INCLUDE(random.DEC)

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

; extern int rindex, rtable[33];

; random()
←random:
 CALL StkChk
PUSH BP
MOV BP,SP

;   {

;    int new;

;    if (rindex >= 13) new = rtable[rindex-13];
PUSH DX
MOV BX,←rindex
CMP BX,0DX
JL X1
MOV BX,←rindex
ADD BX,0FFF3X
LEA CX,←rtable
SAL BX
ADD BX,CX
MOV CX,[BX]

;	←new ← CX
POP DX
PUSH CX

;    else new = rtable[rindex+20];
JR X2
X1:
MOV BX,←rindex
ADD BX,014X
LEA CX,←rtable
SAL BX
ADD BX,CX
MOV CX,[BX]

;	←new ← CX
POP DX
PUSH CX
X2:

;    new = rtable[rindex] = new + rtable[rindex];
MOV BX,←rindex
LEA CX,←rtable
SAL BX
ADD BX,CX
MOV CX,←rindex
LEA AX,←rtable
SAL CX
ADD CX,AX
MOV DI,CX
MOV CX,[DI]

;	AX ← ←new
POP AX
PUSH AX
ADD AX,CX
MOV [BX],AX

;	←new ← AX
POP DX
PUSH AX

;    rindex = ( rindex + 1 ) % 33;
MOV BX,←rindex
INC BX
MOV AX,BX
MOV BX,021X
CWD
IDIV AX,BX
MOV AX,DX
MOV ←rindex,AX

;    return (new);

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

;    };
MOV SP,BP
POP BP
RET;

; Externals Declared Here
PUBLIC ←random

C←CODE ENDS

; Number of Bytes of Code = 071X, (113)