Page 209 - ARM 64 Bit Assembly Language
P. 209

Integer mathematics 197





















                                            Figure 7.3: Multiplication of large numbers.


                     one clock cycle. The long multiply instructions take between three and five clock cycles,
                     depending on the size of the operands. Using the multiply instruction on an AArch64 pro-
                     cessor to multiply by a constant requires loading the constant into a register before performing
                     the multiply. Therefore, if the multiplication can be performed using two or fewer shift, add,
                     and/or subtract instructions, then it will be equal to or better than using the multiply instruc-
                     tion.


                     7.2.5 Multiplying large numbers

                     Consider the method used for multiplying two digit numbers in base ten, using only the one-
                                                                                                     0
                                                                                          1
                     digit multiplication tables. Fig. 7.3 shows how a two digit number a = a 1 × 10 + a 0 × 10 is
                                                                   1
                                                                             0
                     multiplied by another two digit number b = b 1 × 10 + b 0 × 10 to produce a four digit result
                     using basic multiplication operations which only take one digit from a and one digit from b at
                     each step.
                     This technique can be used for numbers in any base and for any number of digits. Recall that
                     one hexadecimal digit is equivalent to exactly four binary digits. If a and b are both eight-bit
                     numbers, then they are also two-digit hexadecimal numbers. In other words eight bit numbers
                     can be divided into groups of four bits, each representing one digit in base sixteen. Given a
                     multiply operation that is capable of producing an eight-bit result from two four-bit inputs, the
                     technique shown above can then be used to multiply two eight-bit numbers using only four-bit
                     multiplication operations.

                     Carrying this one step further, suppose we are given two sixteen-bit numbers, but our com-
                     puter only supports multiplying eight bits at a time and producing a sixteen bit result. We
                     can consider each sixteen bit number to be a two digit number in base 256, and use the
   204   205   206   207   208   209   210   211   212   213   214