Certainly! Here is a simple program in x86 assembly language to calculate the factorial of a given number:
section .data
number db 5
result dw 1
section .text
global _start
_start:
mov cx, 1
mov ax, 1
calculate_factorial:
cmp cx, byte[number]
jg end_calculation
imul ax, cx
inc cx
jmp calculate_factorial
end_calculation:
mov word[result], ax
; Add code here to display or store the result
; Exit the program
mov eax, 1
xor ebx, ebx
int 0x80
This program initializes the number to calculate the factorial of (in this case, 5) and then iterates through a loop to calculate the factorial using the imul
instruction. The result is stored in the result
variable. You would need to add code to display or store the result as needed.
Chat with our AI personalities
INCLUDE Irvine32.inc
.data
num DWORD ?
fact DWORD 1
str1 DB "Enter a number to Calculate Factorial: ",0
str2 DB "The Factorial is: ",0
.code
main PROC
call clrscr
mov edx,OFFSET str1
call WriteString
call ReadDec
mov num,eax
mov ecx,num
L1 :
mov eax,ecx
mov ebx,fact
mul ebx
mov fact,ebx
LOOP L1
mov edx,OFFSET str2
call WriteString
mov eax,fact
call WriteDec
exit
main ENDP
END main