我正在堆栈溢出,我不太确定

时间:2018-05-03 02:14:23

标签: assembly 64-bit x86-64 masm

好的,所以当我从我的组合中递归调用我的阶乘过程时会发生这种情况。它会在因子过程中抛出异常。我已经包含了所有代码,但问题与堆栈和我的阶乘过程有关。这是因为某些原因我试图推动rax。我无法弄清楚为什么会这样。这是我的代码:

INCLUDELIB libcmt.lib
INCLUDELIB legacy_stdio_definitions.lib

EXTERN printf:PROC
EXTERN scanf:PROC
.DATA
    promt BYTE "1permutation..2)combination ",0
    inFmt BYTE "%d",0
    disp BYTE "You entered %d", 10, 0
    num QWORD ?
    fct_num QWORD ?
    num2 REAL8 1.5
    factpromt BYTE "What number do you want to find the factorial of?",0
    factFmt BYTE "%d",0
    factNum BYTE "The result is = %d", 10, 0
    n   QWORD   ?
    r   QWORD   ?
    result  QWORD   ?
    answer  QWORD   ?
    divisor QWORD   ?

   .CODE
   main PROC
 push   divisor         ;ebp+20
 push   n              ;ebp+16
 push   r              ;ebp+12
 push   result          ;ebp+8
 sub    rsp, 24
 lea    rcx, promt      ;for x64
 call   printf
 lea    rdx, num        ;for x64
 lea    rcx, inFmt      ;for x64
 call   scanf
 mov    rdx, num
 cmp    rdx, 2
 je     run_comb
 jmp    run_perm
 run_perm:
    call permutation
 run_comb:
    call combination
 ;lea   rcx, disp
 ;call  printf
 add    rsp, 24
 mov    eax,0
 ret
 main ENDP



factorial PROC 

mov     rax,qword ptr [rsp+8]
cmp     rax,1
jle     endRecursive
dec     rax
push    rax;throws exception here
call    factorial
mov     rsi,qword ptr [rsp+8]
mul     rsi
endRecursive:
    ret 8



factorial ENDP

permutation PROC 

mov ecx, 5
mov eax, ecx

ret
permutation ENDP

combination PROC 

push    rbp
mov     rbp,rsp

mov     rax, [rbp+32]   ;find (n-r)!
sub     rax, [rbp+24]
mov     rbx, rax
push    rbx
call    factorial
mov     rdx,[rbp+40]    ;move (n-r)! into result
mov     [rdx],rax

mov     rbx, [rbp+24]        ;find r!
push    rbx
call    factorial


mov     rdx,[rbp+40]
mov     rbx, [rdx]
mul     rbx         ;r!*(n-r)!, store product in eax
mov     rbx, [rbp+40]
mov     [rbx], rax          ;store product in divisor variable

mov     rbx, [rbp+32]   ;find n!
push    rbx
call    factorial
mov     rdx,[rbp+40]            
mov     rbx,[rdx]           ;move value of divisor into ebx

mov     rdx, 0
div     rbx         ;divide n! by divisor (r!*(n-r)!)
mov     rbx, [rbp+16]
mov     [rbx],rax                 ;move quotient into result

pop     rbp
ret     32
combination ENDP

END

0 个答案:

没有答案