potato
potato

Reputation: 21

How to calculate factorial with masm32

I'm working on a compiler from C to masm32 code, the task is to calculate the factorial of some number (9 in this case), here is the code that I have:

.386
.model flat, stdcall
option casemap:none
include     C:\\masm32\\include\\masm32rt.inc
.data
res dword ?
.code
multiplyNumbers proc C n:dword
cmp n,0
jge ElsePart0
mov eax, 1
ret
ElsePart0:
ret
push 1
push n
call multiplyNumbers
multiplyNumbers endp
main proc C
push 0
pop res
push 9
call multiplyNumbers
mov res, eax
fn MessageBox,0,str$(eax),"Factroial",MB_OK
mov eax, res
ret
main endp

start:
call main
end start

It looks messed up, as the compiler works like that for now...
The problem is that it's not calculating the factorial of 9 (nine is hardcoded) properly
I would be very thankful if somebody could fix that code with minimal code changes

Btw, here is similar C code:

int multiplyNumbers(int n) {
    if (n <= 0) {
        return 1;
    }
    return n * multiplyNumbers(n - 1);
}

int main() {
    int res = 0;
    res = multiplyNumbers(9);
    return res;
}

The expected result is the asm code properly calculating factorial of 9
9! = 362880
right now it outputs 1703884

Upvotes: 1

Views: 164

Answers (0)

Related Questions