Chapter 3 Machine-Level Representation of Programs

这篇博客主要涵盖了Chapter 3中关于机器级程序表示的作业问题,包括3.58到3.75等多个题目。内容涉及矩阵索引计算、循环优化以及递归函数的复杂度分析等计算机科学基础概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2020-03-31在自己博客园搬运过来…
2020-03-31 3.71 73 74 75未解决

Homework Problems

3.58
long decode2(long x, long y, long z)
{
   
	return ((y - z) * x) ^ (((y - z) << 63) >> 63);
}
3.59
/* 
   x = 2 ^ 64 * xh + xl;
   y = 2 ^ 64 * yh + yl;
   p = x * y = (2 ^64 * xh + xl) * (2 ^ 64 * yh + yl)
     = 2 ^ 128 * xh * yh + 2 ^ 64 * (xh * yl + x1 * yh) + xl * yl
because "2 ^ 128 * xh * yh" is more than  128 bits, so 
   p = 2 ^ 64 * (xh * yl + x1 * yh) + xl * yl
   */

x in %rsi, y in %rdx
store_prod:
    movq    %rdx, %rax        //y-->%rax
    cqto                    //将%rax符号扩展为8字节,高4位保存在%rdx,值为yh;低四位在%rax,值为yl
    movq    %rsi, %rcx        //x-->%rcx
    sarq    $63, %rcx        //符号扩展x的位数为64位,xh保存在%rcx, xl保存在%rsi
    imulq    %rax, %rcx        //m1 = (yl * xh)
    imulq    %rsi, %rdx        //m2 = (xl * yh)
    addq    %rdx, %rcx        //m1 + m2 = (xl * yh) + (yl * xh)
    mulq    %rsi            //128位无符号乘法, 计算%rsi * %rax, 结果的高64位存储在%rdx,低64位在%rax
    addq    %rcx, %rdx        //m1 + m2 + %rdx
    movq    %rax, (%rdi)    //保存最终结果的低64位
    movq    %rdx, 8(%rdi)    //保存最终结果的高64位
    ret
3.60
//A
x in %rdi, n in %esi, result in %rax, mask in %rdx

//B
The initial values of result and mask are 0 and 1

//C
mask != 0

//D
mask <<= 4;

//E
result |= (x & mask)

//F
long loop(long x, int n)
{
   
    long result = 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值