In this post, we’ll walk through an 8086 assembly program that calculates the factorial of a given integer. This program highlights the power of the 8086’s loop instruction and the use of general-purpose registers for iterative calculations. We’ll use the AX register as an accumulator for the result and the CX register as a loop counter, which is its special-purpose function. Let’s get to the code!
data segment
n dw 5 ; The number to find the factorial of
fact dw ? ; To store the 16-bit result
data ends
code segment
assume ds:data, cs:code
start:
mov ax, data
mov ds, ax
mov cx, n ; Load N (5) into the counter register CX
mov ax, 1 ; Initialize factorial result (AX) to 1
fact_loop:
mul cx ; Multiply AX by CX. Result in DX:AX
loop fact_loop ; Decrement CX, jump to fact_loop if CX != 0
mov fact, ax ; Store the final result from AX into 'fact'
int 3 ; Halt execution (breakpoint for debugging)
code ends
end start
Continue reading 8086 Assembly Program to Calculate the Factorial of an Integer Using Loops and Registers