0

I am trying to read a text file that contains six lines. for example

text.txt would contains

1. ABC
2. ABC
...
6. ABC

I would like to shuffle these lines and print it randomly. so it would look like

2. ABC
5. ABC
...
3. ABC

I am trying to use this shuffling algorithm

KNUTHSH  CSECT
         USING  KNUTHSH,R15
         LA     R6,1               i=1
LOOPI1   C      R6,=A(CARDS)       do i=1 to cards
         BH     ELOOPI1
         STC    R6,PACK(R6)        pack(i)=i
         LA     R6,1(R6)           i=i+1
         B      LOOPI1
ELOOPI1  LA     R7,CARDS           n=cards
LOOPN    C      R7,=F'2'           do n=cards to 2 by -1
         BL     ELOOPN
         L      R5,RANDSEED        r5=seed
         M      R4,=F'397204094'   r4r5=seed*const
         D      R4,=X'7FFFFFFF'    r5=r5 div (2^31-1)
         ST     R4,RANDSEED        r4=r5 mod (2^31-1); seed=r4
         LR     R5,R4              r5=seed
         LA     R4,0               r4=0
         DR     R4,R7              r5=seed div n; r4=seed mod n
         LA     R9,1(R4)           r2=randint(n)+1 [1:n]
         LA     R4,PACK(R7)        @pack(n)
         LA     R5,PACK(R9)        @pack(nw)
         MVC    TMP,0(R4)          tmp=pack(n)
         MVC    0(1,R4),0(R5)      pack(n)=pack(nw)
         MVC    0(1,R5),TMP        pack(nw)=tmp
         BCTR   R7,0               n=n-1
         B      LOOPN
ELOOPN   LA     R6,1               i=1
         LA     R8,PG              pgi=@pg
LOOPI2   C      R6,=A(CARDS)       do i=1 to cards
         BH     ELOOPI2
         XR     R2,R2              r2=0
         IC     R2,PACK(R6)        pack(i)
         XDECO  R2,XD              edit pack(i)
         MVC    0(3,R8),XD+9       output pack(i)
         LA     R8,3(R8)           pgi=pgi+3
         LA     R6,1(R6)           i=i+1
         B      LOOPI2
ELOOPI2  XPRNT  PG,80              print buffer
         XR     R15,R15            set return code
         BR     R14                return to caller
CARDS    EQU    20                 number of cards
PACK     DS     (CARDS+1)C         pack of cards
TMP      DS     C                  temp for swap
PG       DC     CL80' '            buffer
XD       DS     CL12               to decimal
RANDSEED DC     F'16807'           running seed
         YREGS  
         END    KNUTHSH

Here I am trying to read the text file.

READFILE CSECT
         SAVE  (14,12)             save registers on entry
         PRINT NOGEN
         BALR  R12,0               establish addressability
         USING *,R12               set base register
         ST    R13,SAVEA+4         link mySA->prevSA
         LA    R11,SAVEA           mySA
         ST    R11,8(R13)          link prevSA->mySA
         LR    R13,R11             set mySA pointer
         OPEN  (INDCB,INPUT)       open the input file
         OPEN  (OUTDCB,OUTPUT)     open the output file
LOOP     GET   INDCB,PG            read record
         CLI   EOFFLAG,C'Y'        eof reached?
         BE    EOF
         PUT   OUTDCB,PG           write record
         B     LOOP
EOF      CLOSE (INDCB)             close input
         CLOSE (OUTDCB)            close output
         L     R13,SAVEA+4         previous save area addrs
         RETURN (14,12),RC=0       return to caller with rc=0
INEOF    CNOP  0,4                 end-of-data routine
         MVI   EOFFLAG,C'Y'        set the end-of-file flag 
         BR    R14                 return to caller
SAVEA    DS    18F                 save area for chaining
INDCB    DCB   DSORG=PS,MACRF=PM,DDNAME=INDD,LRECL=80,                 *
               RECFM=FB,EODAD=INEOF
OUTDCB   DCB   DSORG=PS,MACRF=PM,DDNAME=OUTDD,LRECL=80,                *
               RECFM=FB
EOFFLAG  DC    C'N'                end-of-file flag
PG       DS    CL80                buffer
         YREGS
         END   READFILE

I am new to assembly and not sure how to tie these two and get it working. Please let me know if you have any suggestions.

fuz
  • 88,405
  • 25
  • 200
  • 352
Jaoa
  • 95
  • 9
  • You realize that your shuffling is for a different processor, right? You need to reimplement it as x86 if that's what you want to use. Probably easier to start from a high level algorithm instead. – Jester Nov 05 '19 at 17:15
  • @Jester good catch, I have updated the code. I pasted the wrong code I had. My apologies. – Jaoa Nov 05 '19 at 17:22
  • 1
    [This page](https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array) has an example of shuffling an array in its accepted answer. It may help you understand the algorithm. – Alexis Wilke Nov 05 '19 at 17:25
  • Are those code snippets ones that you have written, or found somewhere? How much z/OS assembler do you know and is this a homework question? – Steve Ives Nov 06 '19 at 12:07

0 Answers0