How to resolve the algorithm Magic squares of odd order step by step in the 360 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Magic squares of odd order step by step in the 360 Assembly programming language

Table of Contents

Problem Statement

A magic square is an   NxN   square matrix whose numbers (usually integers) consist of consecutive numbers arranged so that the sum of each row and column,   and   both long (main) diagonals are equal to the same sum (which is called the   magic number   or   magic constant). The numbers are usually (but not always) the first   N2   positive integers. A magic square whose rows and columns add up to a magic number but whose main diagonals do not, is known as a semimagic square.

For any odd   N,   generate a magic square with the integers   1 ──► N,   and show the results here. Optionally, show the magic number.
You should demonstrate the generator by showing at least a magic square for   N = 5.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Magic squares of odd order step by step in the 360 Assembly programming language

Source code in the 360 programming language

*        Magic squares of odd order - 20/10/2015
MAGICS   CSECT
         USING  MAGICS,R15         set base register
         LA     R6,1               i=1
LOOPI    C      R6,N               do i=1 to n
         BH     ELOOPI
         LR     R8,R6              i
         SLA    R8,1               i*2
         LA     R9,PG              pgi=@pg
         LA     R7,1               j=1
LOOPJ    C      R7,N               do j=1 to n
         BH     ELOOPJ
         LR     R5,R8              i*2
         SR     R5,R7              -j
         A      R5,N               +n
         BCTR   R5,0               -1
         XR     R4,R4              clear high reg
         D      R4,N               /n
         LR     R5,R4              //n
         M      R4,N               *n
         LR     R2,R5              (i*2-j+n-1)//n*n
         LR     R5,R8              i*2
         AR     R5,R7              -j
         S      R5,=F'2'           -2
         XR     R4,R4              clear high reg
         D      R4,N               /n
         AR     R2,R4              +(i*2+j-2)//n
         LA     R2,1(R2)           +1
         XDECO  R2,PG+80           (i*2-j+n-1)//n*n+(i*2+j-2)//n+1
         MVC    0(5,R9),PG+87      put in buffer
         LA     R9,5(R9)           pgi=pgi+5
         LA     R7,1(R7)           j=j+1
         B      LOOPJ
ELOOPJ   XPRNT  PG,80
         LA     R6,1(R6)           i=i+1
         B      LOOPI
ELOOPI   XR     R15,R15            set return code
         BR     R14                return to caller
N        DC     F'9'               <== input
PG       DC     CL92' '            buffer
         YREGS
         END    MAGICS

  

You may also check:How to resolve the algorithm String length step by step in the ColdFusion programming language
You may also check:How to resolve the algorithm MAC vendor lookup step by step in the Zoea Visual programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Nested function step by step in the 11l programming language
You may also check:How to resolve the algorithm Tic-tac-toe step by step in the Pascal programming language