Page 110 - ARM 64 Bit Assembly Language
P. 110

96 Chapter 4


                19  scanf("%lx", &y);
                20  __uint128_t prod = longMul(x, y);
                21  lo = (uint64_t) prod;
                22  hi = (uint64_t) (prod >> 64);
                23  printf("%016lx * %016lx = %016lx%016lx\n", x, y, hi, lo);
                24  return 0;
                25  }

                  The AArch64 implementation executes the multiplication step with just two instructions; most
                  of the code is devoted to calling printf and scanf statements, and moving parameters into
                  position:

                1         .text
                2         /* longMul takes two unsigned longs as input on x0 and x1 and
                3            returns a 128-bit product with the lower half "lo" in x0
                4            and the higher half "hi" in x1. */
                5         .type   longMul, %function
                6  longMul:
                7         mul     x9, x0, x1
                8         umulh   x1, x0, x1
                9         mov     x0, x9
                10        ret
                11        .type   main, %function
                12        .global main
                13        /* Reads two unsigned longs from stdin and prints the resulting
                14           product on stdout. */
                15  main:
                16        stp     x29, x30, [sp, #-32]!
                17        stp     x19, x20, [sp, #16]    // Callee-saved
                18
                19        // printf("Enter a number in hex: ")
                20        adr     x0, prompt
                21        bl      printf
                22
                23        // scanf("%ld", &x)
                24        adr     x0, fmtScan
                25        adr     x1, dataIn
                26        bl      scanf
                27        adr     x1, dataIn
                28        ldr     x19, [x1]      // x
                29
                30        // printf("Enter a number in hex: ")
                31        adr     x0, prompt
                32        bl      printf
                33
                34        // scanf("%ld", &y)
                35        adr     x0, fmtScan
                36        adr     x1, dataIn
   105   106   107   108   109   110   111   112   113   114   115