Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 在使用%r8、%r9等寄存器时,有哪些功能?_Assembly_Att - Fatal编程技术网

Assembly 在使用%r8、%r9等寄存器时,有哪些功能?

Assembly 在使用%r8、%r9等寄存器时,有哪些功能?,assembly,att,Assembly,Att,我想在程序集中重写此C函数的代码: int digits_amount = digits_num / DIGITS_PER_ITERATION + 1; const long unsigned int array_length = DIGITS_PER_ITERATION*10*(digits_amount) / 3; long unsigned int remainders[array_length]; long unsigned int answ[digits_amount]; int

我想在程序集中重写此C函数的代码:

int digits_amount = digits_num / DIGITS_PER_ITERATION + 1;
const long unsigned int array_length = DIGITS_PER_ITERATION*10*(digits_amount) / 3;
long unsigned int remainders[array_length];  
long unsigned int answ[digits_amount];
int i = 0; 
for (; i < array_length; i++)
    remainders[i] = ARRINIT;
for (i = 0; i < digits_amount; i++) {
    int j = array_length-1;
    long unsigned int quotient = 0;
    long unsigned int sum = 0;
    for (; j >= 0; j--) {
        remainders[j] *= SCALE;
        sum = remainders[j]+quotient*(j+1);
        quotient = sum / (2*j+1);
        remainders[j] = sum - quotient*(2*j+1);
    }
    quotient = sum / SCALE;
    remainders[0] = sum - quotient*SCALE;
    answ[i] = quotient;
} //end of algo, then output
在生成的代码中,它如下所示:

.L1:
movq $200000000, (%rax, %rcx, 8)
incq %rcx
cmpq %rdx, %rcx
jb .L1
xorq %rcx, %rcx
.L2:
pushq %rbx
movq %rdx, %r8
decq %r8
movq $0, %r9
movq $0, %r10
.L3:
movq 0(%rax, %r8, 8), %r11
pushq %rax
movq $1000000000, %rax
mulq %r11
movq %rax, %r11
movq %r8, %rax
incq %rax
mulq %r9
addq %rax, %r10
addq %r11, %r10
movq %r10, %rax
movq %r8, %rbx
addq %rbx, %rbx
incq %rbx
pushq %rdx
xorq %rdx, %rdx
divq %rbx
movq %rax, %r9
movq %rdx, %rbx
popq %rdx
popq %rax
movq %rbx, 0(%rax, %r8, 8)
decq %r8
cmpq $0, %r8
jge .L3
movq $1000000000, %rbx
pushq %rdx
pushq %rax
movq %r10, %rax
xorq %rdx, %rdx
divq %rbx
movq %rax, %r9
popq %rax
movq %rdx, (%rax)
popq %rdx
popq %rbx
movq %r12, (%rbx, %rcx, 8)
incq %rcx
cmpq %rdi, %rcx
jb .L2

因此,答案并不一致。我检查了内部循环中的值,并提到,第一个值匹配,但其余值不匹配。我认为,问题可能在mul或div中,但我找不到它。div是否可能不仅更改%rdx和%rax?%r8、%r9、,。。。在我不知情的情况下,登记册正在更改?也许我在什么地方弄错了。使用这么多寄存器是一种好方法吗?这一定比使用内存更快,所以我尝试用这种方法来实现。

哪些值匹配?哪些值不匹配?它们是什么?它们应该是什么?除了隐式使用RAX和RDX的MUL和DIV指令之外,没有任何指令具有隐式操作数。然而,内联汇编语句在不告诉编译器的情况下更改了一组寄存器,这可能会导致在它之后执行的任何代码出现任何数量的问题。另外,在你的汇编代码中没有任何东西表明它实际上会比C版本运行得更快。除了@RossRidge所说的,你还改变了输入寄存器的内容。这是(参见警告)。此外,使用大量寄存器可能会使此特定代码运行得更快,但代价是降低已经在使用这些寄存器的代码的运行速度。我发现了一个错误(一个寄存器可能会溢出)。它起到了作用,但该计划没有奏效。然后,正如你所说,我试着不改变内部寄存器,并成功了。谢谢你的医生。与我之前阅读的内容相比,这些内容更加完整
.L1:
movq $200000000, (%rax, %rcx, 8)
incq %rcx
cmpq %rdx, %rcx
jb .L1
xorq %rcx, %rcx
.L2:
pushq %rbx
movq %rdx, %r8
decq %r8
movq $0, %r9
movq $0, %r10
.L3:
movq 0(%rax, %r8, 8), %r11
pushq %rax
movq $1000000000, %rax
mulq %r11
movq %rax, %r11
movq %r8, %rax
incq %rax
mulq %r9
addq %rax, %r10
addq %r11, %r10
movq %r10, %rax
movq %r8, %rbx
addq %rbx, %rbx
incq %rbx
pushq %rdx
xorq %rdx, %rdx
divq %rbx
movq %rax, %r9
movq %rdx, %rbx
popq %rdx
popq %rax
movq %rbx, 0(%rax, %r8, 8)
decq %r8
cmpq $0, %r8
jge .L3
movq $1000000000, %rbx
pushq %rdx
pushq %rax
movq %r10, %rax
xorq %rdx, %rdx
divq %rbx
movq %rax, %r9
popq %rax
movq %rdx, (%rax)
popq %rdx
popq %rbx
movq %r12, (%rbx, %rcx, 8)
incq %rcx
cmpq %rdi, %rcx
jb .L2