首页 > 解决方案 > 如何解释汇编并将其编写为类似 RISC 的微操作

问题描述

我正在学习组装并遇到了这种做法。

我正在使用的书是:Randal E. Bryant, David R. O'Hallaron - Computer Systems。程序员的视角 [第 3 版](2016 年,Pearson)

我得到了这段 C 代码内部循环的汇编代码,其中有一个累加计算来计算 r。使用 AT&T。

L3:
    mulsd   (%rcx,%rax,8), %xmm0     
    mulsd   8(%rcx,%rax,8), %xmm0    
    mulsd   16(%rcx,%rax,8), %xmm0   
    addq    $3, %rax                        
    cmpq    %r8, %rax                
    jl      .L3  

这是对应的C代码

double randomproduct(double a[], long n)
{
    long i;
    double x, y, z;
    double r=1;
    for (i=0; i<n-2; i+=3) {
        x = a[i]; y = a[i+1]; z = a[i+2];
        r = r*x*y*z; //accumulation calc
    }
    for (; i<n; i++)
        r *= a[i];
    return r;
}

我不确定我应该如何在类似 RISC 的微操作中编写此代码至少进行几次迭代。

这是我目前拥有的:

load r0, (rcx,rax,8)  //r0 is the loading register to store the data from the [array+i*8] calculation
mul r0,rax,rax //not sure if the name and order of the registers are correct I'm pretty lost :(

我真的很困惑应该为此使用哪些寄存器/内存位置以及如何编写类似 RISC 的微操作 shd。请发送帮助,感谢所有帮助。谢谢!

标签: cassemblyx86cpu-architecture

解决方案


推荐阅读