[英]Solving Pascal's Triangle (nCk) in Assembly Recursion
我目前無法解決合並問題的答案。 我的基本情況很好。 我認為問題在於評估組合(n-1,k),然后評估組合(n-1,k-1)。
這是我的代碼:n和k是用戶的輸入。
sub esp, 2
push word[n]
push word[k]
call combi
pop word[ans] ;store yung combi value sa ans
;convert to ascii string value
add word[ans], 30h
;print answer
mov eax, 4
mov ebx, 1
mov ecx, ans
mov edx, 2
int 80h
jmp exit
combi:
mov ebp, esp
mov ax, word[ebp+4] ;ax = k
cmp [ebp+6], ax ;if (n==k)
je basecase
cmp word[ebp+4],0 ;cmp k = 0
je basecase
;combi(n-1,k)
mov ax, [ebp+6] ; ax = n
mov bx, [ebp+4] ; bx = k
dec ax ;n-1
;execute again
sub esp, 2
push ax
push bx
call combi
pop cx ;stores to cx popped value combi(n-1,k)
mov ebp, esp ;update pointers
;combi(n-1,k-1)
push ax
dec bx
push bx
call combi
pop dx ;stores to dx popped value combi(n-1,k-1)
mov ebp, esp ;update pointers
add cx, dx ;combi(n-1,k) + combi(n-1,k-1)
mov word[ebp+8], cx
jmp combi_exit
basecase:
mov word[ebp+8], 1
combi_exit:
ret 4
希望您的回應和好主意! 謝謝!
要修復您的遞歸, combi:
的中間部分存在問題:
...
call combi
pop cx ;stores to cx popped value combi(n-1,k)
;* ^ this freed the allocated space for result
mov ebp, esp ;update pointers
;* not needed, as you will not use EBP right now, and next call destroys it
;combi(n-1,k-1)
push ax
;* ^ pushing first argument, but no stack space reserved for result
dec bx
push bx
call combi
...
要
解決此問題,
您可以將該部分調整為:
編輯:這對於2+深度遞歸將無法正常工作,因為您沒有按需保留寄存器,整個遞歸體系結構需要更多的注意,並且我首先選擇簡單地用更簡單的設計來重寫它,而不是修復所有這些問題。小問題。 此“修復”將至少停止段故障。
...
call combi
mov cx,[esp] ;stores to cx value combi(n-1,k)
;* ^ keeps the stack space reserved (not popping)
;combi(n-1,k-1)
push ax
...
當然,還有另一個問題,就是您的輸出僅對一位數字是正確的,但是僅搜索堆棧溢出和這些內容的標記[x86]信息 ,在此不再贅述。
順便說一句,此IMO來自不幸的過於復雜的用法堆棧,您是否出於某些特定原因而遵循這種復雜的調用約定? 如何在寄存器中提供一些自定義的類似fastcall的參數和結果? 它的性能也要高得多,但是對我個人而言,更容易跟蹤事物並正確地處理堆棧。 如果可以嘗試,我可以稍后在此答案中添加自己的變體...
編輯:注冊調用約定的完整工作示例:
檔案: so_32b_pascal_triangle.asm
section .text
global _start
_start:
mov ecx,5 ; n
mov edx,2 ; k
call combi
; return to linux with sys_exit(result)
mov ebx,eax
mov eax,1
int 80h
; ECX = n, EDX = k, returns result in EAX, no other register modified
; (_msfastcall-like convention, but extended to preserve ECX+EDX)
combi: ; c(n, k) = c(n-1, k-1) + c(n-1, k), c(i, 0) = c(i, i) = 1
test edx,edx ; k == 0
je .basecases ; c(i, 0) = 1
cmp edx,ecx ; k == n
je .basecases ; c(i, i) = 1
; 0 < k < n case:
dec ecx ; n-1
call combi ; EAX = c(n-1, k)
push esi
mov esi,eax ; keep c(n-1, k) in ESI
dec edx ; k-1
call combi ; EAX = c(n-1, k-1)
add eax,esi ; EAX = c(n-1, k-1) + c(n-1, k)
; restore all modified registers
pop esi
inc ecx
inc edx
ret ; return result in EAX
.basecases:
mov eax,1
ret
編譯+運行+結果顯示:
...$ nasm -f elf32 -F dwarf -g so_32b_pascal_triangle.asm -l so_32b_pascal_triangle.lst -w+all
...$ ld -m elf_i386 -o so_32b_pascal_triangle so_32b_pascal_triangle.o
...$ ./so_32b_pascal_triangle ; echo $?
10
...$
編輯:
而對於我自己的好奇心,試圖把它從C-ISH C ++代碼(驗證fastcall
約定按預期工作需要與C / C ++的互操作性,即使):
so_32b_pascal_triangle.asm
文件具有相同的combi:
代碼,但開頭已修改(添加了全局,刪除了_start
):
section .text
global combi
; ECX = n, EDX = k, returns result in EAX, no other register modified
; (fastcall-like convention, but extended to preserve ECX+EDX)
combi: ; c(n, k) = c(n-1, k-1) + c(n-1, k), c(i, 0) = c(i, i) = 1
...
文件so_32b_pascal_triangle_Cpp.cpp
:
#include <cstdio>
#include <cstdint>
extern "C" __attribute__ ((fastcall)) uint32_t combi(uint32_t n, uint32_t k);
int main()
{
for (uint32_t n = 0; n < 10; ++n) {
printf("%*c", 1+2*(10-n), ' ');
for (uint32_t k = 0; k <= n; ++k) {
printf("%4d", combi(n, k));
// 4-char width formatting - works for 3 digit results max.
}
printf("\n");
}
}
構建+測試:
$ nasm -f elf32 -F dwarf -g so_32b_pascal_triangle.asm -l so_32b_pascal_triangle.lst -w+all
$ g++ -std=c++17 -c -m32 -O3 -Wall -Wpedantic -Wextra so_32b_pascal_triangle_Cpp.cpp
$ g++ -m32 so_32b_pascal_triangle*.o -o so_32b_pascal_triangle
$ ./so_32b_pascal_triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.