【深入理解计算机系统-第二版】3.55习题

深入理解计算机系统(英文版 第二版)中的第三章家庭作业3.55想了一个晚上也没有想出来到底是怎么回事。百度也没有百度出结果,而用google搜索相关内容,终于找到了其中的“猫腻”。

如果想要直接看,则点击下面链接。

http://stackoverflow.com/questions/11680720/implement-64-bit-arithmetic-on-a-32-bit-machine

书中,3.55题在P329页,题目略有不同,但原理一致。


 

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.

1 typedef long long ll_t;
2 void store_prod(ll_t *dest, int x, ll_t y) {
3     *dest = x*y;
4 }

gcc generates the following assembly code implementing the computation:

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 multiprecision 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.


 

简要地分析一下上述代码的原理。我们知道x是32位,而y是64位,我们将x按符号扩展为64位,这一步即为汇编中3、4行的作用。实际上,x在符号扩展前后,与y相乘的64位结果是一样的。

x=xhigh*232+xlow

y=yhigh*232+ylow

x*y=(xhigh*yhigh)*264        (1)

  +(xlow*yhigh+xhigh*ylow)*232  (2)

  +xlow*ylow               (3)

我们要取它的低64位结果,故(1)直接省略;

64位结果中,低32位的结果可以直接使用(3)计算出来;

高32位结果由两部分构成:(2)的低32位结果+(3)的高32位结果;

要得到(3)的高32位结果,我们需要使用mul的单操作数形式,在edx中保存乘积的高32位。需要注意的是这里使用mul无符号乘法运算,是由于y是64位的,低32位可以看作无符号数,因此,如果使用imul,虽然低32位的结果是正确的,但是高32位结果却是错误的。

转载于:https://www.cnblogs.com/XjChenny/archive/2013/04/06/3002000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值