-2

The following code computes the product of x and y and stores the result in memory. Data type ll_t is defined to be equivalent to long long.

gcc generates the following assembly code implementing the computation:

 typedef long long ll_t;

 void store_prod(ll_t *dest, int x, ll_t y)
 {
 *dest = x*y;
 }

dest at %ebp+8, x at %ebp+12, y at %ebp+16

1 movl  16(%ebp), %esi
2 movl  12(%ebp), %eax
3 movl  %eax, %edx
4 sarl  $31, %edx
5 movl  20(%ebp), %ecx
6 imull %eax, %ecx
7 movl  %edx, %ebx
8 imull %esi, %ebx
9 addl  %ebx, %ecx
10 mull %esi
11 leal (%ecx,%edx), %edx
12 movl 8(%ebp), %ecx
13 movl %eax, (%ecx)
14 movl %edx, 4(%ecx)

This code uses three multiplications to implement the multi precision arithmetic required to implement 64-bit arithmetic on a 32-bit machine. Describe the algorithm used to compute the product, and annotate the assembly code to show how it realizes your algorithm.

Question: What does line 5 do? what value is it moving to register ecx? also what does line 11 do ?

Hambuzo
  • 83
  • 10
  • If your offsets are at 8, 12, 16 and 20, then they are all 32-bits. 64-bit operands would have 8-byte offsets. – stark Oct 05 '15 at 18:37
  • @stark wrong. `dest` is a pointer, so 4 bytes. `x` is indeed 32 bits. `y` is 64 bits, starting at +16 with the low dword, and +20 the high dword. – Jester Oct 05 '15 at 18:45
  • [What's the purpose of the LEA instruction?](http://stackoverflow.com/q/1658294/995714), [What is the difference between MOV and LEA](http://stackoverflow.com/q/1699748/995714) – phuclv Oct 05 '15 at 18:46

1 Answers1

2

Line 5: it's copying the value of some local variable to ECX. The value is unkown as of this listing, as we lack part of the original function code.

Line 11: it's equivalent to: EDX = EDX+ECX. The LEA instruction is used to compute the EA of a memory value and store that EA into a destination register, thus, it can be used to quickly do additions and constant multiplication.

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
  • 1
    line 5 is copying the high dword of the `y` operand. – Jester Oct 05 '15 at 18:45
  • so ive added the function. but im still not sure what eax (x) is being multiplied to in the next line (line 6). – Hambuzo Oct 05 '15 at 18:46
  • 2
    It's a silly way of doing a 32 * 64 multiply by sign extending to 64 bits and doing a 64 * 64. – Jester Oct 05 '15 at 18:51
  • I don't understand why 3 multiplies are necessary on a 32-bit machine when the operands are 32 and 64 bit. Surely either 2 multiplies (32b * 64b) or 4 multiplies (64b * 64b) are needed? – Weather Vane Oct 05 '15 at 18:56
  • 1
    @WeatherVane: If you're doing a 64-bit by 64-bit multiply and only keeping the *low* 64 bits of the result, then you only need three 32-bit by 32-bit multiplies: the fourth one would be multiplying the high-order 32-bits of each of the operands, and the result of that fourth multiply would only contribute to the *high* 64 bits of the 128-bit result. – Mark Dickinson Oct 05 '15 at 19:08
  • @MarkDickinson that's true, but the operands here are 32 by 64 bit. – Weather Vane Oct 05 '15 at 19:26
  • @WeatherVane: Right, but see Jester's comment. – Mark Dickinson Oct 05 '15 at 19:28