Advertisement
Lawnknome

Fib Program2

Jul 8th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TITLE Simple Calculations (Program1.asm)
  2. ;Author: Jared Hughes
  3. ;Email: hughesja@onid.oregonstate.edu
  4. ;Class#: CS271-400
  5. ;Program Assignment 2
  6. ;Due: 7/12/2015
  7. ;Description: This program will take string input from the user
  8. ;             Take in a number from the user
  9. ;             Validate user input
  10. ;             Calculate and display all Fibonacci numbers up to the number given by user
  11. ;             Display a good bye message to the user and terminate the program
  12.  
  13. INCLUDE Irvine32.inc
  14.  
  15.     UPPER = 46
  16.     LOWER = 1
  17.     STRINGMAX = 80
  18.  
  19. .data
  20.    
  21.     outsideRange    BYTE    "Out of range.  Please enter a number within (1 - 46).", 0
  22.     myName          BYTE    "Fibonacci Numbers by Jared Hughes", 0 
  23.     explain         BYTE    "This program will take a number specified by you (n), and give the nth number in the Fibonacci sequence.", 0
  24.     yourName        BYTE    "What is your name? ", 0
  25.     helloU          BYTE    "Hello, ", 0
  26.     good_bye        BYTE    "That's it!  See ya later, ", 0
  27.     numberQ         BYTE    "Enter a number between 1 and 46 to calculate the Fibonacci sequence: ", 0
  28.     ;userName       BYTE    STRINGMAX+1 DUP (?)
  29.     fibNth          DWORD   ?
  30.     sequence        BYTE    "Here is the Fibonacci sequence requested.", 0
  31.     fib1            DWORD   0
  32.     fib2            DWORD   1
  33.     spaces          BYTE    "     "
  34.     period          BYTE    "."
  35.     sum             DWORD   ?
  36.     stringIn        BYTE    STRINGMAX+1 DUP (?)
  37.    
  38.     ;fibArray       DWORD   46 DUP(?)  
  39.    
  40. .code
  41. main PROC
  42.  
  43. ;introduction - Title of program and introductions of myself and the user
  44.  
  45.     mov edx, OFFSET myName              ;moves myName into the edx register
  46.     call WriteString                    ;prints the string in the edx register to the console
  47.     call CrLf                           ;newline (will not comment these last two lines out if they are repeated)
  48.     mov edx, OFFSET yourName
  49.     call WriteString
  50.     mov edx, OFFSET stringIn
  51.     mov ecx, STRINGMAX
  52.     call ReadString                     ;read string in from user and null terminate it
  53.     ;mov userName, OFFSET edx                  
  54.     call CrLf
  55.     mov edx, OFFSET helloU
  56.     call WriteString
  57.     mov edx, OFFSET stringIn
  58.     call WriteString
  59.     call Crlf
  60.        
  61. ;userInstructions - Tell user about the program and what to do
  62.  
  63.     mov edx, OFFSET explain        
  64.     call WriteString
  65.     call CrLf
  66.        
  67. ;getUserData - Get number from user for fibonacci calculation
  68.  
  69. getData:
  70.  
  71.     mov edx, OFFSET numberQ
  72.     call WriteString
  73.     call ReadDec                        ;read in a decimal number from the user
  74.     mov fibNth, eax
  75.  
  76. ;Validate data from user input
  77.    
  78.     cmp eax, UPPER                      ;if number read in is higher than 46 jump to errormessage
  79.     jg errorMessage
  80.     cmp eax, LOWER                      ;if read dec is lower than 1, jump to errorMessage
  81.     jl errorMessage
  82.     jmp validationDone                  ;if number is valid, jump past error and continue program
  83.    
  84. errorMessage:
  85.  
  86.     mov edx, OFFSET outsideRange
  87.     call WriteString
  88.     call CrLf
  89.     jmp getData
  90.  
  91. validationDone:
  92.    
  93.     mov edx, OFFSET sequence
  94.     call WriteString
  95.     call CrLf
  96.    
  97. ;calculateFib - Calculate fibonacci numbers and display
  98.    
  99.     mov ecx, fibNth                     ;sets loop counter to number nth term user requested
  100.     mov edx, 0                          ;sets edx counter to track how many terms per line
  101.  
  102. fibLoop:
  103.  
  104.     mov ebx, fib1              
  105.     mov eax, fib2
  106.     cmp edx, 5                          ;compare edx counter to 5, if equal to 5 print to new line
  107.     jne noNewLine                       ;if not equal to 5 jump to next dec print statement
  108.     call CrLf
  109.     mov edx, 0                          ;reset edx counter to 0
  110.  
  111. noNewLine:
  112.     call WriteDec
  113.     push edx                            ;push edx onto stack to allow use of edx for string
  114.     mov edx, OFFSET spaces
  115.     call WriteString
  116.     pop edx                             ;pop edx from stack and increment counter for next loop iteration
  117.     inc edx
  118.     add ebx, eax                        ;add the two terms together to get the next Fib number
  119.     mov fib2, ebx
  120.     mov fib1, eax
  121.    
  122.     loop fibLoop                        ;loop if ecx > 0
  123.  
  124. ;farewell: say goodbye to the user
  125.  
  126.     call CrLf
  127.     mov edx, OFFSET good_bye            ; say goodbye
  128.     call WriteString
  129.     mov edx, OFFSET stringIn            ;insert user name into goodbye
  130.     call WriteString
  131.     mov edx, OFFSET period              ;insert period at end of sentence
  132.     call WriteString
  133.     call CrLf
  134.    
  135.     exit    ;exits to OS
  136. main ENDP
  137. END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement