	     .data
prompt1:     .asciiz  "\nEnter an expression 'A op B', up to 32 characters"
prompt2:     .asciiz  "\n(or 'q' to quit): "
bye:	     .asciiz  "\n\nBye!"
newln:       .asciiz  "\n"
express:     .space   80
errorprompt: .asciiz  "\nInvalid Input, Please try again!"

             .text
	     .globl main
main:	
go:	     li     $v0, 4	    # get ready to print 
				    # first string
    	     la     $a0, prompt1    # load string into memory
	     syscall		    # print the greeting
	     la     $a0, prompt2    # get ready to print second string
  	     syscall		    # print second string
	     li     $v0, 8          # get ready to read input string
	     la     $a0, express    # $a0 is now buffer space
	     li     $a1, 80         # length of string = 80
	     syscall		    # read the string
	     lb     $a3, ($a0)	    # load first byte 
	     li     $a2, 113	    # check if first byte is q
	     beq    $a3, $a2, terminated   # branch to exit if q
	     jal    picocalc	    # jump to calculator proc.
	     li     $a1, -1
	     beq    $a1, $v0, errorout	# error handling
	     li     $v0, 1          # print result
	     syscall
	     b      go              # branch to go
				
errorout:    li     $v0, 4	    # get ready to read string
	     la     $a0, errorprompt	# load str into $a0
	     syscall		    # print it
	     b      go		    # go to go subrout.

terminated:
 	     li     $v0, 4          # get ready to read str.
	     la     $a0, bye        # load str into $a0
	     syscall                # print it
	
	     li      $v0, 10                 # exit program service 
             syscall 
	

picocalc:    li     $t4, 10         # init t4 to 10 for place
	     addi   $t2, $0, 0      # init t2 to 0
	     move   $t0, $a0        # move val in a0 to t0
	     lb     $t1, ($t0)      # load first character
picoloop:    bltz   $t1, error      # check if char < "0"
	     li     $t3, 57         # load "9" into t3
	     bgt    $t1, $t3, error # check if char > "9"
	     addi   $t3, $t1 , -48  # convert to integer
	     mul    $t2, $t2, $t4   # multiply to put 
	    			    # integer in right place
	     add    $t2, $t3, $t2   # add total together
	     addi   $t0, $t0, 1     # increment to next byte
			            # in memory.
	     lb     $t1, ($t0)      # load next byte
	     li     $t3, 32         # check for space
	     bne    $t3, $t1, picoloop # go back to loop if no sp.
	     move   $a1, $t2        # move first operand to a1
blank1:	     addi   $t0, $t0, 1     # increment to next byte
	     lb     $t1, ($t0)      # load next byte
	     beq    $t1, $t3, blank1# check if space exists
	     li     $t3, 43         # determine if operation is +
	     beq    $t1, $t3, blank2# get 2nd operand if +
	     b      error           # go to error if no oper.
blank2:	     move   $a2, $t1        # move operation into $a2
	     li     $t3, 32         # load space char into t3
	     addi   $t2, $0, 0      # init $t2 to 0		
	     addi   $t0, $t0, 1     # increment to next byte
	     lb     $t1, ($t0)      # load next byte
	     bne    $t1, $t3, error # if no sp. goto error
blankloop:   addi   $t0, $t0, 1     # increment to next byte
	     lb     $t1, ($t0)      # load next byte
	     beq    $t1, $t3, blankloop # check for more spaces		
operand:     la     $t3,newln		# Load address of line character \n
	     lb     $t3, 0($t3)		# Load contents of line character \n 
					# into t3
	     beq    $t1, $t3, termstring # goto end of string
	     bltz   $t1, error      # check if < "0"
	     li     $t3, 57         # check if > "9"
	     bgt    $t1, $t3, error # goto error if > "9"
	     addi   $t3, $t1, -48   # convert to integer value
	     mul    $t2, $t2, $t4   # multiply to get right place
	     add    $t2, $t2, $t3   # add total together
	     addi   $t0, $t0, 1	    # increment to next byte
	     lb     $t1, ($t0)	    # load next byte
	     b      operand         # go to loop
termstring:  move   $a3, $t2        # move second operand to a3
	     li     $t3, 43         # check for "+"
	     beq    $t3, $a2, plus  # "+" operation
	     b      error
plus:	     add    $a0, $a1, $a3   # add
	     li     $v0, 0          # load 0 into $v0 to
				    # indicate no error
	     jr     $ra             # return to main program
error:	     li     $v0, -1         # load -1 into $v0 to
				    # indicate there is an error
	     jr     $ra             # go back to main program

