Title: CS231 Lab4
Date: 2017-02-06T00:00:00
Tags: CS231, Assembly
Authors: Henry Brooks

This asm program takes a list of numbers from the user and returns the sum of those numbers.

## Henry Brooks
## CS231 Lab4 sum numbers

		.data
space:  	.asciiz 	"\n"
header1:	.asciiz 	"===============================================================================\n"
header2:	.asciiz 	"Program Description: This program asks a user how many numbers they want to sum\n"
header3:	.asciiz 	"Author:              Henry Brooks\n"
header4:	.asciiz 	"Creation Date:       02/06/2017\n"
header5:	.asciiz 	"===============================================================================\n"

nl:		.asciiz 	"\n"

query:		.asciiz		"How many numbers would you like to add together? "
answer:		.asciiz		"The sum of the numbers is "
		.text
main:
################### Print header information
		li 			$v0, 4
		la 			$a0, header1
		syscall
		la 			$a0, header2
		syscall
		la 			$a0, header3
		syscall
		la 			$a0, header4
		syscall
		la 			$a0, header5
		syscall
		
################### Print query
		li			$v0, 4
		la			$a0, query
		syscall
		
################### Take number of values the user wants to sum
################### and save it to $s0
		li			$v0, 5
		syscall
		
		add $s0, $v0, $0

################### Initialize $s2 to 0 , sum = 0
		add $s2, $0, $0

################### Loop to collect inputs from user
################### if ( count == value) break
###################   else sum += input
###################        count++
###################        loop

loop:		beq			$s0, $s1, exit
		
		li			$v0, 5
		syscall
		
		add			$s2, $s2, $v0		# $s2 += $v0 , sum += input
		addi			$s1, $s1, 1		# $s1++ , count++
		j loop


exit:		li			$v0, 4
		la			$a0, answer
		syscall
		
		li			$v0, 1
		add			$a0, $s2, $0
		syscall

}

◊filebox-highlight["Lab4b.asm" 'asm]{
## Henry Brooks
## CS231 Lab4 divisiblity

		.data
space:  	.asciiz 	"\n"
header1:	.asciiz 	"======================================================================\n"
header2:	.asciiz 	"Program Description: \tThis program asks a user how many numbers they\n\t\t\twant to find that are divisible by 6\n"
header3:	.asciiz 	"Author:              \tHenry Brooks\n"
header4:	.asciiz 	"Creation Date:       \t02/06/2017\n"
header5:	.asciiz 	"======================================================================\n"

nl:		.asciiz 	"\n"

query1:		.asciiz		"How many positive numbers that are divisable by 6 do you want to add?\n"
query2:		.asciiz		"Enter a number: "
badcount:	.asciiz		"Invalid number\nPlease enter a number greater than zero\n"
arrow:		.asciiz		"==> "
good:		.asciiz		" is divisible by 6\n"
bad:		.asciiz		" is not divisible by 6. Enter another number.\n"
error1:		.asciiz		"**** ERROR: "
error2:		.asciiz		" is not in the range of 1 to 100. Enter another number.\n"
error3:		.asciiz		" is not a positive number. Enter another number.\n"
answer:		.asciiz		"The sum of the positive numbers between 1 and 100 that are divisible by 6, is: "

		.text
main:
################### Print header information
		li 			$v0, 4
		la 			$a0, header1
		syscall
		la 			$a0, header2
		syscall
		la 			$a0, header3
		syscall
		la 			$a0, header4
		syscall
		la 			$a0, header5
		syscall

main2:
################### Print query
		li			$v0, 4
		la			$a0, query1
		syscall
		
################### Take user input
		li			$v0, 5
		syscall

################### Check that input is valid
		ble			$v0, $0, badinput			

################## If input is good set $s0 to $v0	
		add			$s0, $v0, $0

################### Initialize $s2 to 0 and $s1 to 0, sum = 0, count = 0
		add			$s2, $0, $0
		add			$s1, $0, $0

################### Loop to collect inputs from user
################### if ( count == value) break
###################   else sum += input
###################        count++
###################        loop

loop:		beq			$s0, $s1, exit		# s0 = total, s1 = count
		
		li			$v0, 4			# load print string
		la			$a0, query2		# load "enter number:" message
		syscall						# print message
		
		li			$v0, 5			# load user input program
		syscall						# get user input, (cin)
		
		add			$s3, $v0, $0		# save input to save3 register
		
		beq			$s3, $0, gt100		# run if input is zero
		
		blt			$s3, $0, ltzero		# run if input is less than 0
		
		addi			$t0, $0, 100		# make t0 = 100
		blt			$t0, $s3, gt100		# run if input is greater than 100
		
		addi			$t0, $0, 6		# make t0 = 6 
		div			$s3, $t0		# divide input by 6
		mfhi			$t0			# move remainder of input/6 to t0
		bne			$t0, $0, notdiv		# run if not divisible by 6
		
		li			$v0, 4			# load print string
		la			$a0, arrow		# load arrow string
		syscall
		
		li			$v0, 1			# load print int
		add			$a0, $s3, $0		# load user input into arg0
		syscall
		
		li			$v0, 4			# load print string
		la			$a0, good		# load divisiblity good message
		syscall
		
		add			$s2, $s2, $s3		# $s2 += $v0 , sum += input
		addi			$s1, $s1, 1		# $s1++ , count++
		j loop

#################### User input for count is less than or equal to 0
badinput:	li			$v0, 4
		la			$a0, badcount
		syscall
		
		j			main2

##################### input is less than 0
ltzero:		add			$t0, $v0, $0		# send input to temp
		
		li			$v0, 4			# load print string
		la			$a0, arrow		# load arrow string
		syscall						# print string
		
		la			$a0, error1		# load error header
		syscall						# print error header
		
		li			$v0, 1			# load print int
		add			$a0, $t0, $0		# load temp into arg0
		syscall						# print temp
		
		li			$v0, 4			# load print string
		la			$a0, error3		# load less than zero message
		syscall						# print message
		
		j			loop			# return to loop

#################### input is greater than 100
gt100:		add			$t0, $v0, $0		# send input to temp
		
		li			$v0, 4			# load print string
		la			$a0, arrow		# load arrow string
		syscall						# print string
		
		la			$a0, error1		# load error header
		syscall						# print error header
		
		li			$v0, 1			# load print int
		add			$a0, $t0, $0		# load temp into arg0
		syscall						# print temp
		
		li			$v0, 4			# load print string
		la			$a0, error2		# load less than zero message
		syscall						# print message
		
		j			loop			# return to loop

#################### input is not divisible by 6
notdiv:		add			$t0, $v0, $0		# send input to temp

		li			$v0, 4			# load print string
		la			$a0, arrow		# load arrow string
		syscall
		
		la			$a0, error1		# load error header
		syscall						# print error header
		
		li			$v0, 1			# load print int
		add			$a0, $t0, $0		# load temp into arg0
		syscall						# print temp
		
		li			$v0, 4			# load print string
		la			$a0, bad		# load not divisible message
		syscall
		
		j			loop

#################### exit from program
exit:		li			$v0, 4			# load print string
		la			$a0, answer		# load answer message
		syscall						# print answer message
		
		li			$v0, 1			# load print int
		add			$a0, $s2, $0		# load sum
		syscall						# print sum