
        .data
prompt:   .asciz "Enter the number N: "
formatIn: .asciz "%d"
output1: .asciz "sum: %d\n"
output2: .asciz "max: %d\n"
output3: .asciz "min: %d\n"
output4: .asciz "The random number is: %d\n"
n: .word 0

        .text 
        .balign 4
        .global main

main:   stp x29, x30, [sp, -16]!                //Save FP and LR to stack, allocating 16 bytes, pre-increment SP
        mov x29, sp
                                                //Update frame pointer (FP) to current stack pointer (SP)
        adrp x0, prompt                         //set x1 = N
        add x0, x0,: lo12:prompt                //format the print statement to take the input
        bl printf                               //print the statement

        adrp x0, formatIn                       //get the input as a double type
        add x0, x0, :lo12:formatIn              //add x0 as the input
        adr x1,n                                //address x1 as the input
        bl scanf                                //take the input

        mov x0,0                                //set x0 to 0
        bl time                                 //set time
        bl srand                                //set srand

        mov     x21,21                          //set max = 21 to get the min number
        mov     x22,0                          //set min =0 to get the max number
        mov x20,0                               //set sum=0
        adr x19, n                              //address counter x19=
        ldr x19,[x19]                           //load x19
        b test                                  //start the loop
test:   cmp     x19,0                           //compare the counter to 0
        

        b.gt    top                             //if x19 is greater than 0, keep running the loop
        b output                                //else, print the sum

top:    
        bl rand                                 //call rand function
        mov x1,x0                               //set x1 =x0 to print
        mov x3,20                               //set x3=20 (random from 0-20)
        sdiv x2,x1,x3                           //calculate the quotient
        msub x4, x3,x2,x1                       //calculate the remainder
       
        mov x1,x4                               //set x1=x4=the final random number from 0-20
        cmp    x21,x1                           //compare x21 with x1 to find min
        b.le    else                            //if less than
        mov x21,x1                              //swap

else:
        cmp x22,x1                              //compare x22 with x1
        b.ge next                               //if greater than, go next
        mov x22,x1                              //swap

next:
        
        add x20, x20, x1                        //calculate the sum of the random number (keep adding the new number)
        adrp x0, output4                        //address to print the new number to set argument
        add x0,x0,:lo12:output4                 //set low 12 bits
        bl printf                               //print new random number

        
        sub x19, x19, 1                         //minus 1 to the counter.


               
output: mov     x1,x20                          //set s1,sum
        //print sum
        adrp    x0, output1                   //set first argm of print  
        add     x0,x0, :lo12:output1          //add low 12 bits to x0
        bl      printf                        //print
        //print max
        mov x1, x22                            //set max = x1
        adrp    x0, output2                   //set second argm of print
        add     x0,x0, :lo12:output2          //add low 12 bits to x0
        bl      printf                         //print

        //print min                       
        mov x1,x21                              //set min = x1
        adrp    x0, output3                     //set third argm of print
        add     x0,x0, :lo12:output3            //add low 12 bits to x0         
        bl      printf                          //print

        mov     w0, 0                           // Store the value 0 in register x0. By convention, funtions use x0 to return values makes main return 0
        ldp     x29,x30,[sp],16                 // Load x29 & x30 from the address pointed to by SP. [sp], 16 increases sp by 16 *AFTER* this operation is done
        ret                                     // "Returns" from this function. Sets the program counter to the value stored in a register. If no register is given, defaults to x30 (AKA LR)
