1	#####################################################################
     2	#	Title:   CSCI 221 - Sum of Integers from 1 to N
     3	#	Author:  Bary W Pollack
     4	#	File:    Sum.asm
     5	#	Date:    Jan. 13, 2002 - 23:00
     6	#####################################################################
     7	#	Register - Variable Cross Reference
     8	#		v0: N
     9	#		t0: Sum
    10	#####################################################################

    11		.data

    12	titl:	.ascii	"\n  **** Sum of Integers Program ****\n\n"
    13		.ascii	"  Author: Bary W Pollack\n"
    14		.ascii	"  File:   sum.asm\n"
    15		.asciiz	"  Date:   Jan. 13, 2002\n"
    16	prmt:	.asciiz	"\n\n  Input a value for N = "
    17	rslt:	.asciiz	"  The sum of the integers from 1 to N is "
    18	bye:	.asciiz	"\n  **** Fin ****\n"

    19	#####################################################################

    20		.globl	main

    21		.text
    22	main:
    23		li	$v0, 4 		# code for Print String
    24		la	$a0, titl		# load address of titl into $a0
    25		syscall			# print the title message
    26	lup:
    27		li	$v0, 4            # code for Print String
    28		la	$a0, prmt         # load address of prmt into $a0
    29		syscall                 # print the prompt message

    30		li	$v0, 5		# code for Read Integer
    31		syscall			# reads the value of N into $v0

    32		blez	$v0, end		# branch to end if $v0 <= 0 
    33		li	$t0, 0		# clear register $t0
    34	loop:
    35		add 	$t0, $t0, $v0	# sum of integers in register $t0
    36		addi	$v0, $v0, -1	# summing integers in reverse order
    37		bnez	$v0, loop		# branch to loop if $v0 is != zero		
    38		li	$v0, 4		# code for Print String
    39		la	$a0, rslt		# load address of message into $a0
    40		syscall			# print the string
    41		
    42		li	$v0, 1		# code for Print Integer
    43		move	$a0, $t0		# move value to be printed to $a0 
    44		syscall			# print sum of integers
    45		b 	lup			# branch to lup
    46	end:
    47		li	$v0, 4		# code for Print String
    48		la	$a0, bye		# load address of "bye" message into $a0
    49		syscall			# print the string

    50		li	$v0, 10		# terminate program and
    51		syscall			# return control to the system

    52		.end

    53	#####################################################################