.data
  strPrompt: .asciiz "Enter an integer: "
  strAns1: .asciiz "The sum of all integers up to "
  strAns2: .asciiz " is "
  strAns3: .asciiz ".\n"
.globl main
.text
main:
  # Print out the prompt to get an integer
  la $a0, strPrompt
  li $v0, 4
  syscall

  # Read integer, save it in s0
  li $v0, 5
  syscall
  move $s0, $v0

  # Call the sum function on the argument
  move $a0, $s0
  jal sum
  # Get the result, store in s1
  move $s1, $v0

  # Print the answer. It is in three parts (strings),
  # with the two integers s0 and s1 in-between.

  la $a0, strAns1
  li $v0, 4
  syscall

  # Print s0
  move $a0, $s0
  li $v0, 1
  syscall

  la $a0, strAns2
  li $v0, 4
  syscall

  # Print s1
  move $a0, $s1
  li $v0, 1
  syscall

  la $a0, strAns3
  li $v0, 4
  syscall

  # Exit
  li $v0, 10
  syscall

sum:
  # If n < 1, go to basecase
  slti $t0, $a0, 1
  bne $t0, $zero, basecase

  # Save a0 and ra on stack
  addi $sp, $sp, -8
  sw $ra, 4($sp)
  sw $a0, 0($sp)

  # Decrement a0 for recursive call
  addi $a0, $a0, -1
  jal sum

  # Restore a0 and ra from stack
  lw $ra, 4($sp)
  lw $a0, 0($sp)
  addi $sp, $sp, 8

  # Add argument to return value
  add $v0, $v0, $a0

  jr $ra

basecase: # Return 0
  move $v0, $zero
  jr $ra