Guest User

Untitled

a guest
Jul 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         Title Factorial Finder
  2.          
  3.         INCLUDE Irvine32.inc
  4.          
  5.         ;Declaration of all variables:
  6.         .data
  7.         msg0 BYTE "Please enter 10 integers",0dh,0ah,0
  8.         msg1 BYTE "Enter an integer: ",0
  9.         msg2 BYTE "The factorials are: ",0dh,0ah,0
  10.         msgDebug BYTE " Note: Result should be: ",0
  11.         count1 DWORD ?
  12.         array DWORD 10 DUP(?)            ;DWORD: same size as registers
  13.        
  14.         ;Beginning of code:
  15.         ;Note that:
  16.         ;"ecx" is used as a loop counter
  17.         ;"esi" is used as the array's index
  18.         ;"edx" is used to store the location of a string to be printed
  19.         ;"eax" is used to temporarily store a numerical value
  20.         ;"WriteString" prints what is pointed to by edx
  21.         ;"WriteInt" prints what is stored in eax
  22.         ;"ReadInt" reads an integer input from the user and stores it in "eax"
  23.        
  24.         .code
  25.         main PROC
  26.            mov ecx, LENGTHOF array       ;Set counter to number of elements in the array
  27.            mov esi, 0                    ;Set index to 0
  28.          
  29.            mov edx, OFFSET msg0          ;Prepare to print msg0
  30.            call WriteString              ;Print "Please enter 10 integers"  
  31.            
  32.            ;Beginning of loop L1
  33.            ;In each iteration, an integer is read from the user
  34.            ;that integer is then used in the nested loop L2,
  35.            ;which calculates the factorial of that integer.
  36.            ;The result is then stored in: array[esi]
  37.            ;and the index "esi" is incremented by 1.
  38.            ;The part marke "For debugging purposes"
  39.            ;simply prints array[esi].
  40.            L1:
  41.               mov edx, OFFSET msg1       ;This's necessary because MUL edits edx
  42.               call WriteString           ;Print "Enter an integer: "
  43.               call ReadInt               ;Read integer from user into eax
  44.               mov count1, ecx            ;Store the value of ecx for use later
  45.               mov ecx, eax               ;Set counter to the input integer
  46.               mov eax, 1
  47.               ;Beginning of loop L2
  48.               ;In each iteration, ecx, which initially holds the integer that was input
  49.               ;by the user, is multiplied by eax (initially 1), and ecx is decremented.
  50.               ;Keeps looping until ecx = 0.
  51.               L2:
  52.                  MUL ecx                 ;Multiply eax by ecx, store in eax
  53.               Loop L2
  54.               ;End of loop L2
  55.               ;The result is now inside eax.
  56.               mov array[esi], eax        ;Store result in the array
  57.              
  58.               ;;For debugging purposes   ;Prints array[esi]
  59.               mov edx, OFFSET msgDebug
  60.               call writestring
  61.               mov eax, array[esi]
  62.               call WriteInt
  63.               call CRLF      
  64.               ;;
  65.                  
  66.               inc esi                    ;Ready next element / increment the index
  67.               mov ecx, count1            ;Reset counter to the L1-relevant value
  68.            Loop L1
  69.            ;End of loop L1
  70.          
  71.            mov edx, OFFSET msg2          ;Prepare to print msg2
  72.            call CRLF                     ;Carriage Return + Line Feed (new line)
  73.            call WriteString              ;Print "The factorials are: "
  74.            mov ecx, LENGTHOF array       ;Set counter to array's length
  75.            mov esi, 0                    ;Reset index to 0
  76.          
  77.            ;Beginning of loop L3
  78.            ;This loop prints array[0], array[1], array[2], ...array[9]
  79.            L3:
  80.               mov eax, array[esi]        ;Prepare to print array[esi]
  81.               call WriteInt              ;Print result
  82.               inc esi                    ;Ready next element
  83.               call CRLF                  ;Carriage Return + Line Feed (new line)
  84.            Loop L3
  85.            ;ENd of loop L3
  86.  
  87.            Exit
  88.         main ENDP
  89.          
  90.         END main
Add Comment
Please, Sign In to add comment