Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Title Factorial Finder
- INCLUDE Irvine32.inc
- ;Declaration of all variables:
- .data
- msg0 BYTE "Please enter 10 integers",0dh,0ah,0
- msg1 BYTE "Enter an integer: ",0
- msg2 BYTE "The factorials are: ",0dh,0ah,0
- msgDebug BYTE " Note: Result should be: ",0
- count1 DWORD ?
- array DWORD 10 DUP(?) ;DWORD: same size as registers
- ;Beginning of code:
- ;Note that:
- ;"ecx" is used as a loop counter
- ;"esi" is used as the array's index
- ;"edx" is used to store the location of a string to be printed
- ;"eax" is used to temporarily store a numerical value
- ;"WriteString" prints what is pointed to by edx
- ;"WriteInt" prints what is stored in eax
- ;"ReadInt" reads an integer input from the user and stores it in "eax"
- .code
- main PROC
- mov ecx, LENGTHOF array ;Set counter to number of elements in the array
- mov esi, 0 ;Set index to 0
- mov edx, OFFSET msg0 ;Prepare to print msg0
- call WriteString ;Print "Please enter 10 integers"
- ;Beginning of loop L1
- ;In each iteration, an integer is read from the user
- ;that integer is then used in the nested loop L2,
- ;which calculates the factorial of that integer.
- ;The result is then stored in: array[esi]
- ;and the index "esi" is incremented by 1.
- ;The part marke "For debugging purposes"
- ;simply prints array[esi].
- L1:
- mov edx, OFFSET msg1 ;This's necessary because MUL edits edx
- call WriteString ;Print "Enter an integer: "
- call ReadInt ;Read integer from user into eax
- mov count1, ecx ;Store the value of ecx for use later
- mov ecx, eax ;Set counter to the input integer
- mov eax, 1
- ;Beginning of loop L2
- ;In each iteration, ecx, which initially holds the integer that was input
- ;by the user, is multiplied by eax (initially 1), and ecx is decremented.
- ;Keeps looping until ecx = 0.
- L2:
- MUL ecx ;Multiply eax by ecx, store in eax
- Loop L2
- ;End of loop L2
- ;The result is now inside eax.
- mov array[esi], eax ;Store result in the array
- ;;For debugging purposes ;Prints array[esi]
- mov edx, OFFSET msgDebug
- call writestring
- mov eax, array[esi]
- call WriteInt
- call CRLF
- ;;
- inc esi ;Ready next element / increment the index
- mov ecx, count1 ;Reset counter to the L1-relevant value
- Loop L1
- ;End of loop L1
- mov edx, OFFSET msg2 ;Prepare to print msg2
- call CRLF ;Carriage Return + Line Feed (new line)
- call WriteString ;Print "The factorials are: "
- mov ecx, LENGTHOF array ;Set counter to array's length
- mov esi, 0 ;Reset index to 0
- ;Beginning of loop L3
- ;This loop prints array[0], array[1], array[2], ...array[9]
- L3:
- mov eax, array[esi] ;Prepare to print array[esi]
- call WriteInt ;Print result
- inc esi ;Ready next element
- call CRLF ;Carriage Return + Line Feed (new line)
- Loop L3
- ;ENd of loop L3
- Exit
- main ENDP
- END main
Add Comment
Please, Sign In to add comment