/* 
 * File:        sumprod.s
 * Description: Defines a function that computes the sum
 *              and product of two numbers, passing arguments
 *              by reference.       
 */
        .arch   armv8-a

        .global main

        .text
        .align  2
main:                                   // The main function
        stp     fp, lr, [sp,-16]!
        mov     fp, sp
        
        adr     x0, prompt              // Print the prompt
        bl      printf

        adr     x0, infmt               // Read x and y
        adr     x1, x
        adr     x2, y
        bl      scanf

        adr     x0, x                   // Call sumprod
        adr     x1, y
        bl      sumprod

        adr     x0, outfmt              // Print the results
        adr     x1, x
        ldr     x1, [x1]
        adr     x2, y
        ldr     x2, [x2]
        bl      printf
        
        mov     x0, #0                  // Retrurn from main
        ldp     fp, lr, [sp], 16
        ret

        // sumprod - Two integer arguments are passed by reference. Their
        //           sum and  product is computed. The sum is passed back
        //           in the first argument and the product in the second.

sumprod:
        stp     fp, lr, [sp, -16]!
        mov     fp, sp

        ldr     x2, [x0]                // Get the value of x
        ldr     x3, [x1]                // Get the value of y
        add     x4, x2, x3              // Add x and y
        mul     x5, x2, x3              // Multiply x and y
        str     x4, [x0]                // Store x
        str     x5, [x1]                // Store y

        ldp     fp, lr, [sp], 16        // Return from sumprod
        ret
        
        .data
        .align  3                       // Align on an address divisible by 8
        
x:      .quad  0
y:      .quad  0

        .section .rodata

prompt: .string "Enter two integers: "
infmt:  .string "%ld %ld"
outfmt: .string "sum = %ld product = %ld\n"
