[英](Homework) Translating a C recursive function to assembly (AT&T syntax)
Here is my C Code: 这是我的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
}
}
What this function does is not very relevant to the question, I'm just stuck on the for loop after return. 该函数的作用与问题不是很相关,返回后我只是停留在for循环上。 See this for loop calls the function recursively. 请参见此以循环方式递归调用函数。 Eventually the for loop will not hold true, and it will just exit the function by reaching the last brackets. 最终,for循环将不成立,只会在到达最后一个括号时退出该函数。
In C, this automatically returns control back to the parent call to combinationComputer, inside this for loop. 在C语言中,这会自动将控制权返回给该for循环内的combinationComputer的父调用。 I'm not sure how to implement this in assembly. 我不确定如何在汇编中实现这一点。 Is there a specific name for this behavior in C? 在C中此行为是否有特定名称? (Is it tail-call elimination)? (消除尾音)?
Basically what I am asking is what is the behavior called when you exit (by reaching the last bracket) a function called recursively and it returns to the parent call. 基本上,我要问的是当您退出(到达最后一个括号)递归调用函数并返回到父调用时所调用的行为是什么。 How can this be implemented in assembly (no code please, just logic) If you need more information please ask. 如何在汇编中实现这一点(请没有代码,只有逻辑)。如果您需要更多信息,请询问。
First, some important instructions in assembly: 首先,一些重要的组装说明:
For learn more about intel instructions look: Intel Manuals 有关英特尔指令的更多信息,请参阅: 英特尔手册
Some sample code (of fibbonacci in asm): 一些示例代码(在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
You could use gcc -o [.asm output file] -S [.c to compile]
and read the output to learn much more about code generation. 您可以使用gcc -o [.asm output file] -S [.c to compile]
并阅读输出以了解有关代码生成的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.