Assembly Notes
Assembly Notes
The assembly language function IntegerMul_ calculates the product of eight signed
integers ranging
in size from 8 bits to 64 bits. The C++ declaration for this function uses the
fixed-sized integer types that
are declared in the header file <cstdint> instead of the normal long long, int,
short, and char. Some
assembly language programmers (including me) prefer to use fixed-sized integer
types for assembly
language function declarations since it emphasizes the exact size of the argument.
The declaration of
function UnsignedIntegerDiv_, which demonstrates how to perform unsigned integer
division, also uses
fixed-size integer types. Figure 2-2 illustrates the contents of the stack at entry
to IntegerMul_.
The first instruction of IntegerMul_, movsx rax,cl (Move with Sign Extension),
sign-extends a copy
of the 8-bit integer value a that’s in register CL to 64 bits and saves this value
in register RAX. Note that the
original value in register CL is unaltered by this operation. Another movsx
instruction follows that saves a
64-bit sign-extend copy of the 16-bit value d to RDX. Like the previous movsx
instruction, the source operand
is not modified by this operation. An imul rax,rdx instruction computes the product
of a and b. The twooperand
form of the imul instruction that’s used here saves only the lower 64 bits of the
128-bit product in
the destination operand RAX. The next instruction movsxd rcx,r8d sign-extends the
32-bit operand c to 64
bits. Note that a different instruction mnemonic is required when sign extending a
32-bit integer to 64 bits.
The next two imul instructions compute the intermediate product a * b * c * d.