[英](Homework) Translating a C recursive function to assembly (AT&T syntax)
这是我的C代码:
void combinationComputer(int* temp, int* arr, int* data, int start, int end,
int index, int k, int* x, int* y, int count) {
int i;
if (index == k) {
if (count > k) {
*x = *x + k;
*y = *y + k;
}
for (i = *x; i < *y; i++)
temp[i] = data[i - *x]; // storing all combinations into temp[] as a
// single chunk, one combination at a time
return;
}
for (i = start; i <= end && end - i + 1 >= k - index; i++) {
data[index] = arr[i]; // each combination is stored in data[], with each
// subsequent combination overwriting the previous
// one.
count = count + 1;
combinationComputer(temp, arr, data, i + 1, end, index + 1, k, x, y,
count); // recursive call
}
}
该函数的作用与问题不是很相关,返回后我只是停留在for循环上。 请参见此以循环方式递归调用函数。 最终,for循环将不成立,只会在到达最后一个括号时退出该函数。
在C语言中,这会自动将控制权返回给该for循环内的combinationComputer的父调用。 我不确定如何在汇编中实现这一点。 在C中此行为是否有特定名称? (消除尾音)?
基本上,我要问的是当您退出(到达最后一个括号)递归调用函数并返回到父调用时所调用的行为是什么。 如何在汇编中实现这一点(请没有代码,只有逻辑)。如果您需要更多信息,请询问。
首先,一些重要的组装说明:
有关英特尔指令的更多信息,请参阅: 英特尔手册
一些示例代码(在asm中为fibbonacci):
mov eax, 10 # calculating fib(10)
call fib # eax contain the fib to calc
.. PRINT fib result in eax ..
..
proc fib:
cmp eax, 0 # checking for final conditions 0 or 1
jnz continue1;
mov ebx, 1 # returning 1 for fib(0)
ret
continue1:
cmp eax, 1
jnz continue2
mov ebx, 1 # returning 0 for fib(1)
ret
continue2:
dec eax # decrementing the **n** value to calc (the first
push eax # saving n-1
call fib # calc fib(n-1)
mov edx, eax # saving result in edx
pop eax # restoring n-1 in eax
dec eax
call fib # calc fib(n-2)
add edx, eax # add to the previos fib(n-1)
ret
您可以使用gcc -o [.asm output file] -S [.c to compile]
并阅读输出以了解有关代码生成的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.