Middle-square_method Generator 
 The Method

To generate a sequence of n-digit pseudorandom numbers, an n-digit starting value is created and squared, producing a 2n-digit number. If the result has fewer than 2n digits, leading zeroes are added to compensate. The middle n digits of the result would be the next number in the sequence and returned as the result. This process is then repeated to generate more numbers.

 Pseudo code

var seed = 675248
function random()
    var s = str(seed * seed) 'str: turn a number into string
    do while not len(s) = 12
        s = "0" + s          'add zeroes before the string
    end do
    seed = val(mid(s, 4, 6)) 'mid: string variable, start, length
                             'val: turn a string into number
    return seed
end function

 Middle-square method use

for i = 1 to 5
    print random()
end for

Task

 Generate a class/set of functions that generates pseudo-random
numbers (6 digits) as shown above.

 Show the first five integers generated with the seed 675248 as shown above.
 Show your output here, on this page.
 
/* ARM assembly AARCH64 Raspberry PI 3B */
/*  program pRandom64.s   */
 
/*******************************************/
/* Constantes file                         */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
 
/*********************************/
/* Initialized data              */
/*********************************/
.data
sMessResult:        .asciz " @ \n"
szCarriageReturn:   .asciz "\n"
 
qSeed:              .quad 675248
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss  
sZoneConv:                  .skip 24
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                             // entry of program 
 
    ldr x0,qAdrqSeed
    ldr x3,[x0]
    mov x2,#5
1:
    mov x0,x3
    bl computePseudo
    mov x3,x0
    ldr x1,qAdrsZoneConv
    bl conversion10               // call décimal conversion
    ldr x0,qAdrsMessResult
    ldr x1,qAdrsZoneConv          // insert conversion in message
    bl strInsertAtCharInc
    bl affichageMess              // display message
    subs x2,x2,#1
    bgt 1b
 
100:                              // standard end of the program 
    mov x0, #0                    // return code
    mov x8, #EXIT                 // request to exit program
    svc #0                        // perform the system call
qAdrszCarriageReturn:   .quad szCarriageReturn
qAdrsMessResult:        .quad sMessResult
qAdrsZoneConv:          .quad sZoneConv
qAdrqSeed:              .quad qSeed
/***************************************************/
/*   compute pseudo random number                  */
/***************************************************/
/* x0 contains the number            */
computePseudo:
    stp x1,lr,[sp,-16]! // save  registers
    stp x2,x3,[sp,-16]! // save  registers
    mov x2,x0
    mul x0,x2,x2
    ldr x2,qdiv
    udiv x1,x0,x2
    ldr x2,qdiv2
    udiv x0,x1,x2
    msub x0,x2,x0,x1
    ldp x2,x3,[sp],16   // restaur  2 registers
    ldp x1,lr,[sp],16   // restaur  2 registers
    ret                 // return to address lr x30
qdiv:            .quad 1000
qdiv2:           .quad 1000000
 
/********************************************************/
/*        File Include fonctions                        */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc" 
 