Getting seg fault for writing procedures in x86 32-bit compiled using NASM

41 views Asked by At

Ackermann’s function A(m,n), where m ≥ 0 and n ≥ 0, is defined as

A(0, n) = n + 1
A(m + 1, 0) = A(m, 1)
A(m + 1, n + 1) = A(m,A(m+1,n))

I am trying to write the code for the above function but I am getting seg fault for m=1,n=1. What am I doing wrong?? I am using NASM assembly.

%include "io.mac"

.DATA
    str1        db          "Enter the value of m : ",0
    str2        db          "Enter the value of n : ",0
    str3        db          "A(m,n) = ",0
.UDATA
    m           resd        1
    n           resd        1
.CODE
    .STARTUP
        PutStr      str1
        GetLInt     [m]
        mov         AX,[m]
        PutStr      str2
        GetLInt     [n]
        mov         BX,[n]
        push        AX
        push        BX
        call        ackermanFunc
        jmp         end
    ackermanFunc:
        enter       0,0
        mov         AX,[EBP+10]
        mov         BX,[EBP+8]
        cmp         AX,0
        je          case1
        cmp         BX,0
        je          case2
        jmp         case3
    case1:
        mov         CX,BX
        inc         CX
        leave
        ret         4
    case2:
        dec         AX
        push        AX
        push        1
        call        ackermanFunc
        leave       
        ret         4
    case3:
        dec         BX
        push        AX
        push        BX
        call        ackermanFunc
        dec         AX
        mov         BX,CX
        push        AX
        push        BX
        call        ackermanFunc
        leave       
        ret         4
    end:
        PutStr      str3
        PutInt      CX
        nwln
    .EXIT
0

There are 0 answers