簡體   English   中英

(家庭作業)將C遞歸函數轉換為匯編(AT&T語法)

[英](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中此行為是否有特定名稱? (消除尾音)?

基本上,我要問的是當您退出(到達最后一個括號)遞歸調用函數並返回到父調用時所調用的行為是什么。 如何在匯編中實現這一點(請沒有代碼,只有邏輯)。如果您需要更多信息,請詢問。

首先,一些重要的組裝說明:

  • 調用 :在調用后按下要執行的下一條指令的地址,並調用函數或方法。
  • RET :彈出棧頂元素並跳轉到該地址。
  • 堆棧 :堆棧是您的好朋友,可在寄存器不可用時將變量從一個遞歸調用傳遞到另一個遞歸調用。

有關英特爾指令的更多信息,請參閱: 英特爾手冊

一些示例代碼(在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM