0
;==============================================================================================
    ; recursive procedure:
    ; supersum(int x)
    ; returns 1*2 + 2*3 + 3*4 + ... + i*(i+1)

supersum PROC
    push ebp ; start of every procedure
    mov ebp, esp
    push ebx

    ; Actual subproc calc here
    mov eax, [ebp +8] ; returning eax to the original called value  
    cmp eax,1
    je basecase


    dec eax ; (n-1) ; its just a lie...
    mov recnum, eax  ; saving dec val for call
    mul double ; 2(n-1)
    mov rhs, eax ; the right hand side is finished

    push recnum
    call sumseries ; a_(n-1)
    add esp, 4

definition:
    add eax, rhs ; a_(n-1)+ 2(n-1)

    jmp skp
basecase:             ; a_1 = 0
        mov eax, 0
        mov rhs, 2
        jmp definition
skp:    
        pop ebx
        pop ebp
        ret
supersum ENDP
;============================================================================

我想要得到的序列的明确定义是 1*2 + 2*3 + 3*4 ... + i(i+1)。

我已经把它的数学记下来了,我发现这个系列的递归定义是 a_n = a_(n-1) + 2(n-1) ,其中 a_1 = 0 作为基本情况。我试图弄清楚为什么这段代码一直给我偶数系列:{2,4,6,8,10 ...} 而不是我要计算的系列

4

0 回答 0