Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. (7 points) A. (5 points) Write an assembly program to calculate the factorial

ID: 3707391 • Letter: 2

Question

2. (7 points) A. (5 points) Write an assembly program to calculate the factorial of an unsigned integer n (n!) You need to prompt the user to enter an unsigned integer: Enter unsigned integer (n!): Pass the entered number to the Factorial subroutine. - See the book page 305-306 where you will find main and factorial subroutines. Make the required changes and run the program. Run your program using the debugger to verify your answers. Use Step Into instead of Step Over to keep track of the stack, ESP, EBP, EIP during debugging. To access the stack memory, copy the value of ESP from the register window and past it in the search text box in the memory window. o Submit the following: Lastname2.asm B. (1 point) What will happen to the Factorial program's output when you try to calculate 13! and why? C. (1 point) How many bytes of stack space will be used by the Factorial subroutine when calculating 3!?

Explanation / Answer

A)

//DATA SEGMENT HAS 5 VARIABLES:  
all variables are taken as DB(Define Byte - 1 byte size)

NO --> Number to be accepted by the user, but initially blank so ? is written
FACT --> 1H (H means hexadecimal)
RESULT --> Factorial of the number
STR1 --> To display the first message
STR2 --> To display the result

// program begins from CODE SEGMENT line
DATA for data segment
CODE for code segment
DATA is moved to DS using AX register
Then Message is printed to input a number from the user
Number is converted to BCD by subtracting 30H.
Loop starts with LOOP label and ends with LOOP instruction.

procedure code starts with PROC and ends with ENDP label.

----------------------------------------------------------------------------------------------------------------------------

DATA SEGMENT
NO DB ?
FACT DB 1H
RESULT DB 10 DUP ('$')
STR1 DB "ENTER A NUMBER : $"
STR2 DB 10,13,"FACTORIAL = : $"
DATA ENDS


CODE SEGMENT
ASSUME DS:DATA,CS:CODE
START:
MOV AX,DATA
MOV DS,AX

LEA DX,STR1
MOV AH,9
INT 21H

MOV AH,1
INT 21H
SUB AL,30H
MOV NO,AL

MOV AH,0
MOV AL,FACT
MOV CH,0
MOV CL,NO

LABEL1: MUL CL
LOOP LABEL1

LEA SI,RESULT
CALL HEX2DEC
  
LEA DX,STR2
MOV AH,9
INT 21H
  
LEA DX,RESULT
MOV AH,9
INT 21H
  
MOV AH,4CH
INT 21H   
CODE ENDS  
HEX2DEC PROC NEAR
MOV CX,0
MOV BX,10

LOOP1: MOV DX,0
DIV BX
ADD DL,30H
PUSH DX
INC CX
CMP AX,9
JG LOOP1

ADD AL,30H
MOV [SI],AL

LOOP2: POP AX
INC SI
MOV [SI],AL
LOOP LOOP2
RET
HEX2DEC ENDP

END START