;******************************************
;* Random [>Dest:] Max/S
;* Stephan Gromer, Franz-Liszt Straße 26
;* W-7525 Bad Schönborn 1, (07253)1303 
;* Dieses Programm liefert einen String mit
;* einer Zufallszahl im Bereich von 0 bis 
;* zu einem angegebenen Maximalwert-1
;* Bsp.: Random 5 kann die Werte 000,001,
;*       002,003,004 liefern. 
;******************************************

ExecBase      =   4
OldOpenLibrary=-408 
CloseLibrary  =-414

Output        =- 60
Write         =- 48 
 
RETURN_OK     =   0
RETURN_FAIL   =  20

main:
 movem.l a0/d0,-(sp)

 move.l ExecBase.w,a6
 lea    dosname(pc),a1
 jsr    OldOpenLibrary(a6)
 move.l d0,a6

 movem.l (sp)+,a0/d1
 subq.w #1,d1
 bne.S  weiter
error:
 moveq #RETURN_FAIL,d4
infoout:
 move.l #infotext,d2             ; D2:^Text
 moveq  #infotextende-infotext,d3; D3: Länge
textout:
 jsr    Output(a6)
 move.l d0,d1                     ; D1:^Filehandle
 beq.S  cant_write
 jsr    Write(a6)
cant_write:
 move.l a6,a1
 move.l ExecBase.w,a6
 jsr    CloseLibrary(a6)
 move.l d4,d0
 rts

weiter: 
 moveq  #RETURN_OK,d4
 moveq  #0,d0; Ergebnis init.
loop:
 tst.w  d1
 beq.S  check
 cmp.b  #'?',(a0)
 beq.S  infoout
 subq.w #1,d1
 cmp.b  #' ',(a0)+
 beq.S  loop

 sub.b  #'0',-1(a0)
 bmi.S  error
 cmp.b  #10,-1(a0)
 bpl.S  error
 mulu   #10,d0
 add.b  -1(a0),d0
 bra.S  loop
check: 
 tst.l  d0
 beq.S  error
 cmp.w  #256,d0
 bpl.S  error

 moveq  #0,d1                                                        
 move.w $dff006,d1; Vertical Beam Position holen
 divu   d0,d1
 clr.w  d1
 swap   d1
 
 lea    randomtext(pc),a0
 move.l a0,d2                        ; D2:^Text
 moveq  #randomtextende-randomtext,d3; D3: Länge
 divu   #100,d1
 add.b  d1,(a0)+
 clr.w  d1
 swap   d1
 divu   #10,d1
 add.b  d1,(a0)+
 swap   d1
 add.b  d1,(a0)
 bra    textout

dosname: dc.b "dos.library",0
randomtext:
 dc.b "000",$a,$d
randomtextende:
infotext:
 dc.b "Random Max/S",$a,$d
 dc.b "Returns a randomvaluestring ranging"
 dc.b " from 0 to Max-1",$a,$d 
 dc.b "Max can range from 1 to 255",$a,$d
infotextende:
