Page 206 - ARM 64 Bit Assembly Language
P. 206

194 Chapter 7


                3  y:     .8byte  0x75
                4  msg:   .asciz  "%lx * %lx = %lx\n"
                5         .text
                6         .type   main %function
                7         .global main
                8  main:  stp     x29, x30, [sp, #-16]!
                9         mov     x0, #0         // x0 is result
                10        ldr     x1, x          // x1 is multiplicand
                11        ldr     x2, y          // x2 is multiplier
                12  loop:  tst    x2, 0x1        // check LSB
                13        lsr     x2, x2, #1     // shift multiplier right
                14        add     x3, x0, x1
                15        csel    x0, x3, x0, ne  // Add if LSB was 1
                16        lsl     x1, x1, #1     // shift multiplicand left
                17        cmp     x2, xzr
                18        bne     loop           // if (multiplier == 0), we are done
                19        // printf("%lx * %lx = %lx\n")
                20        mov     x3, x0
                21        ldr     x2, x
                22        ldr     x1, y
                23        adr     x0, msg
                24        bl      printf
                25        mov     w0, #0         // return 0
                26        ldp     x29, x30, [sp], #16
                27        ret
                28        .size   main,(. - main)



                  7.2.3 Signed multiplication

                  Consider the two multiplication problems shown in Fig. 7.1 and Fig. 7.2. Note that the result
                  of a multiply depends on whether the numbers are interpreted as unsigned numbers or signed
                  numbers. For this reason, most computer CPU’s have two different multiply operations for
                  signed and unsigned numbers.



                                                                    11011001
                                      −39
                                                                ×   01001001
                                  ×     73
                                                    1111111111011001
                                      657      =
                                                    1111111011001
                                    219
                                                    1111011001
                                  −2847
                                                    1111010011100001

                                    Figure 7.1: In signed 8-bit math, 11011001 2 is −39 10 .
   201   202   203   204   205   206   207   208   209   210   211