Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TITLE Simple Calculations (Program1.asm)
- ;Author: Jared Hughes
- ;Email: hughesja@onid.oregonstate.edu
- ;Class#: CS271-400
- ;Program Assignment 2
- ;Due: 7/12/2015
- ;Description: This program will take string input from the user
- ; Take in a number from the user
- ; Validate user input
- ; Calculate and display all Fibonacci numbers up to the number given by user
- ; Display a good bye message to the user and terminate the program
- INCLUDE Irvine32.inc
- UPPER = 46
- LOWER = 1
- STRINGMAX = 80
- .data
- outsideRange BYTE "Out of range. Please enter a number within (1 - 46).", 0
- myName BYTE "Fibonacci Numbers by Jared Hughes", 0
- explain BYTE "This program will take a number specified by you (n), and give the nth number in the Fibonacci sequence.", 0
- yourName BYTE "What is your name? ", 0
- helloU BYTE "Hello, ", 0
- good_bye BYTE "That's it! See ya later, ", 0
- numberQ BYTE "Enter a number between 1 and 46 to calculate the Fibonacci sequence: ", 0
- ;userName BYTE STRINGMAX+1 DUP (?)
- fibNth DWORD ?
- sequence BYTE "Here is the Fibonacci sequence requested.", 0
- fib1 DWORD 0
- fib2 DWORD 1
- spaces BYTE " "
- period BYTE "."
- sum DWORD ?
- stringIn BYTE STRINGMAX+1 DUP (?)
- ;fibArray DWORD 46 DUP(?)
- .code
- main PROC
- ;introduction - Title of program and introductions of myself and the user
- mov edx, OFFSET myName ;moves myName into the edx register
- call WriteString ;prints the string in the edx register to the console
- call CrLf ;newline (will not comment these last two lines out if they are repeated)
- mov edx, OFFSET yourName
- call WriteString
- mov edx, OFFSET stringIn
- mov ecx, STRINGMAX
- call ReadString ;read string in from user and null terminate it
- ;mov userName, OFFSET edx
- call CrLf
- mov edx, OFFSET helloU
- call WriteString
- mov edx, OFFSET stringIn
- call WriteString
- call Crlf
- ;userInstructions - Tell user about the program and what to do
- mov edx, OFFSET explain
- call WriteString
- call CrLf
- ;getUserData - Get number from user for fibonacci calculation
- getData:
- mov edx, OFFSET numberQ
- call WriteString
- call ReadDec ;read in a decimal number from the user
- mov fibNth, eax
- ;Validate data from user input
- cmp eax, UPPER ;if number read in is higher than 46 jump to errormessage
- jg errorMessage
- cmp eax, LOWER ;if read dec is lower than 1, jump to errorMessage
- jl errorMessage
- jmp validationDone ;if number is valid, jump past error and continue program
- errorMessage:
- mov edx, OFFSET outsideRange
- call WriteString
- call CrLf
- jmp getData
- validationDone:
- mov edx, OFFSET sequence
- call WriteString
- call CrLf
- ;calculateFib - Calculate fibonacci numbers and display
- mov ecx, fibNth ;sets loop counter to number nth term user requested
- mov edx, 0 ;sets edx counter to track how many terms per line
- fibLoop:
- mov ebx, fib1
- mov eax, fib2
- cmp edx, 5 ;compare edx counter to 5, if equal to 5 print to new line
- jne noNewLine ;if not equal to 5 jump to next dec print statement
- call CrLf
- mov edx, 0 ;reset edx counter to 0
- noNewLine:
- call WriteDec
- push edx ;push edx onto stack to allow use of edx for string
- mov edx, OFFSET spaces
- call WriteString
- pop edx ;pop edx from stack and increment counter for next loop iteration
- inc edx
- add ebx, eax ;add the two terms together to get the next Fib number
- mov fib2, ebx
- mov fib1, eax
- loop fibLoop ;loop if ecx > 0
- ;farewell: say goodbye to the user
- call CrLf
- mov edx, OFFSET good_bye ; say goodbye
- call WriteString
- mov edx, OFFSET stringIn ;insert user name into goodbye
- call WriteString
- mov edx, OFFSET period ;insert period at end of sentence
- call WriteString
- call CrLf
- exit ;exits to OS
- main ENDP
- END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement