0% found this document useful (0 votes)
8 views2 pages

Assembly Notes

The document discusses performing arithmetic calculations using mixed integer types in assembly language, focusing on the IntegerMul_ function that calculates the product of eight signed integers of varying sizes. It highlights techniques for integer promotion and accessing argument values stored on the stack, using fixed-sized integer types for clarity. The document details specific assembly instructions for sign-extending and multiplying these integers, ensuring the correct handling of different data sizes.

Uploaded by

Vundek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Assembly Notes

The document discusses performing arithmetic calculations using mixed integer types in assembly language, focusing on the IntegerMul_ function that calculates the product of eight signed integers of varying sizes. It highlights techniques for integer promotion and accessing argument values stored on the stack, using fixed-sized integer types for clarity. The document details specific assembly instructions for sign-extending and multiplying these integers, ensuring the correct handling of different data sizes.

Uploaded by

Vundek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Assembly Core Programming

Calculations Using Mixed Types

In many programs, it is often necessary to perform arithmetic calculations using


multiple integer types.
Consider the C++ expression a = b * c * d * e, where a, b, c, d, and e are declared
as long long, long
long, int, short, and char. Calculating the correct result requires proper
promotion of the smaller-sized
integers into large ones. In the next example, you’ll learn few techniques that can
be used to carry out integer
promotions in an assembly language function. You’ll also learn how to access
integer argument values of
various sizes that are stored on the stack.

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.

Calculation of the second intermediate product e * f * g * h is carried out next.


All of these
argument values were passed using the stack as shown in Figure 2-2. The movsx
rcx,byte ptr [rsp+40]
sign extends a copy of the 8-bit argument value e that’s located on the stack and
saves the result to register
RCX. The text byte ptr is a MASM directive that acts like a C++ cast operator and
conveys to the assembler
the size of the source operand. Without the byte ptr directive, the movsx
instruction is ambiguous since
several different sizes are possible for the source operand. The argument value f
is loaded next using a movsx
rdx,word ptr [rsp+48] instruction. Following calculation of the intermediate
product e * f using an imul
instruction, a movsxd rdx,dword ptr[rsp+56] instruction loads a sign-extended copy
of g into RDX. This is
followed by an imul rdx,qword ptr[rsp+64] instruction that calculates the
intermediate product g * h.
Use of the qword ptr directive is optional here; size directives are often used in
this manner to improve
program readability. The final two imul instructions calculate the final product.

You might also like