#-------------------------------------------
# Subroutines - Basic Subroutines
# Ported by: Noah Gergel
# Last updated: May 6 2020
#-------------------------------------------

.text

# A function foo. This is still just a regular label, we just choose
# to think of it as a function.
foo:

    # Function code.

_fooLabelName:

    # The return instruction.
    jalr  zero, ra, 0

# A function that calls bar.
foo1:
    addi    a0, zero, 0     # a0 <- 0
    addi    a1, zero, 1     # a1 <- 1
    jal     ra bar          # Call bar(0,1)
    add     s0, zero, a0    # s0 <- a0

# A function demonstrating fall-through control flow.
foo2:
    # Initialize t0.
    add   t0, zero, zero      # t0 <- 0
    bne   a0, zero, _skip     # if a0 == 0, skip initialization of t1
    add   t1, zero, zero      # t1 <- 0
_skip:
    sub t2, a1, t0

# Example with an exit label.
bar:
    # bar code...
    addi t0, zero, 31       # t0 <- 31

    _barLabel:
        addi t0, zero, 30   # t0 <- 30

        # Jump to the end directly.
        j _barDone

    # Another super useful command.
    sub t0, zero, t0        # t0 <- 0 - t0

    # And if we didn't jump to _barDone earlier this falls through and goes to it
    # anyways, which is what we want.

_barDone:
    # At this point we are done and want to return.
    jalr  zero, ra, 0
