Task

Produce a spiral array.



A   spiral array   is a square arrangement of the first    N2   natural numbers,   where the

numbers increase sequentially as you go around the edges of the array spiraling inwards.



For example, given   5,   produce this array:

 0  1  2  3  4
15 16 17 18  5
14 23 24 19  6
13 22 21 20  7
12 11 10  9  8



Related tasks

   Zig-zag matrix 
   Identity_matrix
   Ulam_spiral_(for_primes)


SPIRALM  CSECT
         USING  SPIRALM,R13
SAVEAREA B      STM-SAVEAREA(R15)
         DC     17F'0'
         DC     CL8'SPIRALM'
STM      STM    R14,R12,12(R13)
         ST     R13,4(R15)
         ST     R15,8(R13)
         LR     R13,R15
*        ----   CODE
         LA     R0,0
         LA     R1,1
         LH     R12,N           n
         LR     R4,R1           Row=1
         LR     R5,R1           Col=1
         LR     R6,R1           BotRow=1
         LR     R7,R1           BotCol=1
         LR     R8,R12          TopRow=n
         LR     R9,R12          TopCol=n
         LR     R10,R0          Dir=0
         LR     R15,R12         n
         MR     R14,R12         R15=n*n
         LA     R11,1           k=1
LOOP     CR     R11,R15
         BH     ENDLOOP
         LR     R1,R4
         BCTR   R1,0
         MH     R1,N
         AR     R1,R5
         LR     R2,R11          k
         BCTR   R2,0
         BCTR   R1,0
         SLA    R1,1
         STH    R2,MATRIX(R1)   Matrix(Row,Col)=k-1
         CH     R10,=H'0'
         BE     DIR0
         CH     R10,=H'1'
         BE     DIR1
         CH     R10,=H'2'
         BE     DIR2
         CH     R10,=H'3'
         BE     DIR3
         B      DIRX
DIR0     CR     R5,R9           if Col<TopCol
         BNL    DIR0S
         LA     R5,1(R5)        Col=Col+1
         B      DIRX
DIR0S    LA     R10,1           Dir=1
         LA     R4,1(R4)        Row=Row+1
         LA     R6,1(R6)        BotRow=BotRow+1
         B      DIRX
DIR1     CR     R4,R8           if Row<TopRow
         BNL    DIR1S
         LA     R4,1(R4)        Row=Row+1
         B      DIRX
DIR1S    LA     R10,2           Dir=2
         BCTR   R5,0            Col=Col-1
         BCTR   R9,0            TopCol=TopCol-1
         B      DIRX
DIR2     CR     R5,R7           if Col>BotCol
         BNH    DIR2S
         BCTR   R5,0            Col=Col-1
         B      DIRX
DIR2S    LA     R10,3           Dir=3
         BCTR   R4,0            Row=Row-1
         BCTR   R8,0            TopRow=TopRow-1
         B      DIRX
DIR3     CR     R4,R6           if Row>BotRow
         BNH    DIR3S
         BCTR   R4,0            Row=Row-1
         B      DIRX
DIR3S    LA     R10,0           Dir=0
         LA     R5,1(R5)        Col=Col+1
         LA     R7,1(R7)        BotCol=BotCol+1
DIRX     EQU    *
         LA     R11,1(R11)      k=k+1
         B      LOOP
ENDLOOP  EQU    *
         LA     R4,1            i
LOOPI    CR     R4,R12
         BH     ENDLOOPI
         XR     R10,R10
         LA     R5,1            j
LOOPJ    CR     R5,R12
         BH     ENDLOOPJ
         LR     R1,R4
         BCTR   R1,0
         MH     R1,N
         AR     R1,R5
         BCTR   R1,0
         SLA    R1,1
         LH     R2,MATRIX(R1)   Matrix(i,j)
         LA     R3,BUF
         AR     R3,R10
         CVD    R2,P8
         MVC    0(4,R3),=X'40202120'
         ED     0(4,R3),P8+6
         LA     R10,4(R10)
         LA     R5,1(R5)
         B      LOOPJ
ENDLOOPJ EQU    *
         WTO    MF=(E,WTOMSG)		  
         LA     R4,1(R4)
         B      LOOPI
ENDLOOPI EQU    *
*        ----   END CODE
         L      R13,4(0,R13)
         LM     R14,R12,12(R13)
         XR     R15,R15
         BR     R14
*        ----   DATA
N        DC     H'5'            max=20 (20*4=80)
         LTORG  
P8       DS     PL8
WTOMSG   DS     0F
         DC     H'80',XL2'0000'
BUF      DC     CL80' '
MATRIX   DS     H               Matrix(n,n)
         YREGS  
         END    SPIRALM