I am trying to write a recursive combination function in assembly (Yasm (similar to nsam)). I cannot use loops, multiplication or division.
I certain I am on the right track but am having issues once I hit the second inner function call. Can anyone help me and tell me where I am going wrong?
Edit: This is my updated code which returns a result but is not always correct. I think I must have a small bit of logic incorrect.
mov rax, [n]
push rax
mov rax, [k]
push rax
call func
... ... program continues from here
func:
push rbp
mov rbp, rsp
push rdi
push rsi
cmp rsi, 0
je stopcond
cmp rdi, rsi
jne contin
stopcond:
mov rax, 1
jmp endfunc
contin:
;C(n-1,k-1)
mov rax, [rsp] ; This is k
dec rax
mov rdx, rax
mov rax, [rsp+8] ; This is n
dec rax
mov rsi, rdx
mov rdi, rax
call func
mov rbx, rax
mov rax, [rsp+8] ; This is n
dec rax
mov rdx, rax
mov rax, [rsp] ; This is k
mov rsi, rax
mov rdi, rdx
call func
add rax, rbx
endfunc:
add rsp, 16
pop rbp
ret
This is a javascript implementation I have been using as a reference
function(n,k) {
if ( k==0 || k==n ) {
return 1
} else {
return C(n-1,k-1) + C(n-1,k)
}
}