Assembly Language for x86 Processors Write a nonrecursive version of the Factori
ID: 3557303 • Letter: A
Question
Assembly Language for x86 Processors
Write a nonrecursive version of the Factorial procedure (Section 8.3.2) that uses a loop. (A VideoNote
for this exercise is posted on the Web site.) Write a short program that interactively tests your Factorial
procedure. Let the user enter the value of n. If overflow occurs in your loop when calculating each factorial
value, your program should display an error message. If no overflow occurs, display the calculated
factorial. Following is a sample of the interaction between the user and the program:
Enter the value of n to calculate the factorial (-1 to quit): 0
The factorial is: 1
Enter the value of n to calculate the factorial (-1 to quit): 1
The factorial is: 1
Enter the value of n to calculate the factorial (-1 to quit): 5
The factorial is: 120
Enter the value of n to calculate the factorial (-1 to quit): 12
The factorial is: 479001600
Enter the value of n to calculate the factorial (-1 to quit): 13
Error: Calculated value cannot fit into 32 bits
Enter the value of n to calculate the factorial (-1 to quit): -1
Explanation / Answer
ideal p286n model tiny codeseg org 100h jmp start start: mov cx, 8 ; Again, CX becomes the counter mov ax, 1 ; Since mul instruction uses DX:AX register pair mov dx, 0 ; We use them to hold our factorial product. ; We initially set it to 1 myloop: mul cx ; DX:AX = AX * CX dec cx ; decrement counter cmp cx, 0 jne myloop quit: ; here DX:AX will hold the value of the factorial mov ax, 4c00h int 21h end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.