Page 205 - ARM 64 Bit Assembly Language
P. 205

Integer mathematics 193


                    5         .text
                    6         .type  main %function
                    7         .global main
                    8  /* Multiplies x and y by adding a series of partial products.
                    9    Shifts x (multiplicand) left and y (multiplier) right. Only
                   10    adds if the least significant bit of multiplier is 1.  */
                   11  main:  stp    x29, x30, [sp, #-16]!
                   12         mov    x0, #0          // resultLow = 0
                   13         mov    x1, #0          // resultHigh = 0
                   14         adr    x2, x           // x2 = &x
                   15         ldr    x2, [x2]        // x2 = multiplicandLow
                   16         mov    x3, #0          // x3 = multiplicandHigh
                   17         adr    x4, y           // x4 = &y
                   18         ldr    x4, [x4]        // x4 = multiplier
                   19  loop:  tst    x4, #1          // Only add if lowest bit of
                   20         beq    endIf           // multiplier is 1:
                   21         adds   x0, x0, x2      // resultLow += multiplicandLow.
                   22         adc    x1, x1, x3      // resultHigh +=
                   23                                //  multiplicandHigh + Carry
                   24  endIf:  // shift 128-bit multiplicand left. Carry bit from lo to hi.
                   25         ands   xzr, x2, 0x8000000000000000
                   26         lsl    x2, x2, #1
                   27         lsl    x3, x3, #1
                   28         cinc   x3, x3, ne
                   29         // shift multiplier right
                   30         lsr    x4, x4, #1
                   31         // if (y != 0) repeat loop
                   32         cmp    x4, xzr
                   33         bne    loop
                   34         // print results
                   35         mov    x4, x0
                   36         mov    x3, x1
                   37         adr    x2, y
                   38         ldr    x2, [x2]
                   39         adr    x1, x
                   40         ldr    x1, [x1]
                   41         adr    x0, msg
                   42         bl     printf
                   43         mov    w0, #0          // return 0
                   44         ldp    x29, x30, [sp], #16
                   45         ret
                   46         .size  main,(. - main)


                      Listing 7.3 AArch64 assembly code for multiplication with a 64 bit result without using
                                                            mul.

                    1         .section .rodata
                    2  x:     .8byte  0x57
   200   201   202   203   204   205   206   207   208   209   210