gpt4 book ai didi

c - 汇编指令阅读,leaq

转载 作者:行者123 更新时间:2023-12-02 19:26:23 26 4
gpt4 key购买 nike

因此,我尝试使用给定的汇编代码计算以下代码中的 M 和 N 的值。

int array1[M][N];
int array2[N][M];

void copyelement(int i, int j) {
array1[i][j] = array2[j][i];
}

组装:


copyelement:
movslq %esi, %rsi
movslq %edi, %rdi
leaq (%rdi,%rdi,8), %rax
addq %rsi, %rax
leaq (%rsi,%rsi,2), %rdx
leaq (%rsi,%rdx,4), %rdx
addq %rdx, %rdi
leaq array2(%rip), %rdx
movl (%rdx,%rdi,4), %ecx
leaq array1(%rip), %rdx
movl %ecx, (%rdx,%rax,4)
ret

在阅读汇编代码时,我一直读到array2(%rip),然后我不知道如何继续。

此时,根据我的计算,我应该有
%rdx = %rdi + (%rsi + %rax) + 4*((%rsi+%rax) + 2*(%rsi + %rax))

此外,我不太确定如何从中获取数组大小。

最佳答案

以下是带有注释的说明,说明了它们的作用。

// Initially, edi contains i and esi contains j.

movslq %esi, %rsi // Sign-extend 32-bit j to 64 bits.
movslq %edi, %rdi // Sign-extend 32-bit i to 64 bits.
leaq (%rdi,%rdi,8), %rax // rax = rdi + rdi*8 = 9*rdi = 9*i.
addq %rsi, %rax // rax = rax + rsi = 9*i + j.
leaq (%rsi,%rsi,2), %rdx // rdx = rsi + rsi*2 = 3*rsi = 3*j.
leaq (%rsi,%rdx,4), %rdx // rdx = rsi + rdx*4 = j + (3*j)*4 = 13*j.
addq %rdx, %rdi // rdi = rdi + rdx = i + 13*j = i + 13*j.
leaq array2(%rip), %rdx // rdx = array2 (address of first element).
movl (%rdx,%rdi,4), %ecx // Load *(rdx + rdi*4) = array2[rdi] = array2[i + 13*j] into ecx.
leaq array1(%rip), %rdx // rdx = array1 (address of first element).
movl %ecx, (%rdx,%rax,4) // Store ecx into (rdx + rax*4) = array1[rax] = array1[9*i + j].

因此,C 代码中的 array2[j][i] 是汇编代码中的 array2[i + 13*j](考虑到二维寻址)与一维寻址)。后者应该是array2[i + M*j],因此我们可以得出M是13。

同样,C 代码中的 array1[i][j] 是汇编代码中的 array1[9*i + j]。后者应该是array1[N*i + j],因此我们可以得出N是9。

关于c - 汇编指令阅读,leaq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62343089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com