-
어셈블리어 - MASM프로그래밍 언어/서브루틴 2013. 6. 4. 20:20
TITLE Calculating a Factorial (Fact.asm)
.386
.MODEL flat,stdcall
INCLUDE SmallWin.inc
.STACK 4096
.code
main PROC
push 12 ; calculate 12 factorial
call Factorial ; calculate factorial (eax)
INVOKE ExitProcess,0
main ENDP
Factorial PROC
push ebp
mov ebp,esp
mov eax,[ebp+8] ; get n
cmp eax,0 ; n < 0?
ja L1 ; yes: continue
mov eax,1 ; no: return 1
jmp L2
L1: dec eax
push eax ; Factorial(n-1)
call Factorial
mov ebx,[ebp+8] ; get n
mul ebx ; ax = ax * bx
L2: pop ebp ; return EAX
ret 4 ; clean up stack
Factorial ENDP
END main