Last active
August 29, 2015 14:07
-
-
Save addisonhuddy/fa444eca7cf9823fced8 to your computer and use it in GitHub Desktop.
CS271 assignment02.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TITLE Assignment 02 (huddya_assignment02.asm) | |
; Author: Addison Huddy | |
; Course / Project ID CS271 / Assignment 2 Date: Oct 19, 2014 | |
; Description: 1. Getting the user's name | |
; 2. Displaying the user's name | |
; 3. User enters the number of Fibonacci numbers they want | |
; 4. Validates the input from the user | |
; 5. Loops through to calculate Fibonacci numbers | |
; 6. Displays Fibonacci numbers in groups of 5 per line | |
; 7. Says goodbye to the user | |
INCLUDE Irvine32.inc | |
; (insert constant definitions here) | |
MAXFIB EQU 46 ;the maxinum number of fibanacci numbers the program can generate | |
.data | |
splash BYTE "Lets count some fibonacci numbers by Addison Huddy", 0 | |
namePrompt BYTE "Please enter your name: ", 0 | |
welcomeMessage BYTE "Welcome, ", 0 | |
numberPrompt BYTE "Enter the number of fibonacci numbers to display (1 to 46): ", 0 | |
numberError BYTE "Please make sure to enter a number 1 to 46, inclusive. Try again: ", 0 | |
goodByeMessage BYTE "Thanks for hanging out. Goodbye ", 0 | |
spacing BYTE " ", 0 ;this variable will provide easy access to spacing between numbers | |
; .data? ; some reason this was throughing a weird error | |
userName DWORD 32 DUP(0) ; for storing the user's name | |
userNum DWORD ? ; how many fib numbers they want to display | |
oldSum DWORD ? ; this is needed to store the last sum | |
column DWORD ? ; needed for incrementing through 5 columns | |
.code | |
main PROC | |
; ********************* Greet the user and get her name ********************** | |
; splash message | |
mov edx, OFFSET splash | |
call WriteString | |
call CrLf | |
; prompt user to enter in name and read it into userName | |
mov edx, OFFSET namePrompt | |
call WriteString | |
mov edx, OFFSET userName | |
mov ecx, SIZEOF userName | |
call ReadString | |
; welcome the user | |
mov edx, OFFSET welcomeMessage | |
call WriteString | |
mov edx, OFFSET userName | |
call WriteString | |
call CrLf | |
; ********************* Get fib numbers and check ********************** | |
; set of the user instructions | |
mov edx, OFFSET numberPrompt | |
call WriteString | |
getUserNum: | |
call ReadInt | |
cmp eax, 1 | |
jl userError | |
cmp eax, MAXFIB | |
jg userError | |
jmp goodUserNum | |
; prompting the user to enter in a good fib number | |
userError: | |
mov edx, OFFSET numberError | |
call WriteString | |
jmp getUserNum | |
; setting the fib number | |
goodUserNum: | |
mov userNum, eax | |
call CrLf | |
; ********************* Get fib numbers and check ********************** | |
; first need to set everything up for the for loop | |
mov ecx, userNum | |
mov eax, 0 | |
mov ebx, 1 | |
mov oldSum, 0 ; starting a 0. It ensures that we get a proper fib sequence. 0 1 1 2 3 5 8 13 21 | |
mov column, 1 | |
; main loop for iterating through fib numbers | |
forLoop: | |
mov oldSum, eax ; storing off the old sum | |
add eax, ebx ; adding the old sum to the new | |
mov ebx, oldSum ; getting the old sum to be ready for the next round | |
call WriteDec ; print what was recently added ans stored in eax | |
mov edx, OFFSET spacing | |
call WriteString | |
cmp column, 5 ; checking if a new line should be returned after 5 numbers to display | |
je yesReturn | |
jnz noReturn | |
yesReturn: | |
call CrLf ; new line after five numbers (e.g. columns) | |
mov column, 0 | |
noReturn: | |
mov edx, column | |
inc edx | |
mov column, edx | |
loop forLoop | |
; ********************* Say goodbye to the user ********************** | |
call CrLf | |
mov edx, OFFSET goodByeMessage | |
call WriteString | |
mov edx, OFFSET userName | |
call WriteString | |
call CrLf | |
exit ; exit to operating system | |
main ENDP | |
; (insert additional procedures here) | |
END main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment