Vote count:
0
I want to write a x86 masm program that calculates a big factorials (results up to 128 bits), but I am not sure how to do that. I've written a version that calculates up to 32bits, how could I change it up so that it works for larger numbers?
TITLE MASM Template (main.asm)
INCLUDE Irvine32.inc
.data
msge byte "Enter number: ",0;
num dword 0;
.code
main proc
mov edx,offset msge
call writestring ;print message on Console
call readint ;used get input from user
mov num,eax ;assign input value to variable 'num'
push num ;push num in stack
call fact ;call factorial function for calculalting factorial
call writedec ;print the factorial on console
call crlf ;clear screen
exit
main ENDP ;end main function
;Factorial function
fact proc
push ebp ;push ebp register in stack
mov ebp,esp ;assing stack pointer 'esp' to the eax register
mov eax,[ebp+8]
cmp eax,1 ;compare until number reaches to 1
je l1 ;jump if equal
dec eax ;decrement number by 1. if current number is 5 then decrement by 1 and come 4
push eax ;again push eax number to stack
call fact
mov ebx,[ebp+8]
mul ebx; multply e.g 5*4*3*2*1=120
l1:
pop ebp; pop ebp 'base pionter'
ret 4 ;return the factorial
fact endp
;End factorial function
END main
asked 30 secs ago
Factorial for large numbers
Aucun commentaire:
Enregistrer un commentaire