Page 248 - ARM 64 Bit Assembly Language
P. 248
236 Chapter 7
7.5 Chapter summary
Complement mathematics provides a method for performing all basic operations using only
the complement, add, and shift operations. Addition and subtraction are fast, but multiplica-
tion and division are relatively slow. In particular, division should be avoided whenever possi-
ble. The exception to this rule is division by a power of the radix, which can be implemented
as a shift. Good assembly programmers replace division by a constant c with multiplication
by the reciprocal of c. They also replace the multiply instruction with a series of shifts and
add or subtract operations when it makes sense to do so. These optimizations can make a big
difference in performance.
Writing sections of a program in assembly can result in better performance, but it is not guar-
anteed. The chance of achieving significant performance improvement is increased if the
following rules are used:
1. Only optimize the parts that really matter.
2. Design data structures with assembly in mind.
3. Use efficient algorithms and data structures.
4. Write the assembly code last.
5. Ignore the C version and write good, clean, assembly.
6. Reduce function calls wherever it makes sense.
7. Avoid unnecessary memory accesses.
8. Write good code. The compiler will beat poor assembly every time, but good assembly
will beat the compiler every time.
Understanding the basic mathematical operations can enable the assembly programmer to
work with integers of any arbitrary size with efficiency that cannot be matched by a C com-
piler. However, it is best to focus the assembly programming on areas where the greatest gains
can be made.
Exercises
7.1. Multiply −90 by 105 using signed 8-bit binary multiplication to form a signed 16-bit
result. Show all of your work.
7.2. Multiply 166 by 105 using unsigned 8-bit binary multiplication to form an unsigned
16-bit result. Show all of your work.
7.3. Write a section of AArch64 assembly code to multiply the value in x1 by 13 10 using
only shift and add operations.
7.4. The following code will multiply the value in x0 by a constant C.Whatis C?