# This example contains 2 leaf functions which do not need any stack space.

.data
    prompt:    .asciiz   "Enter an exponent: "

.text
main:
    # Prompt for the user to enter an exponent
    li $v0, 4
    la $a0, prompt
    syscall

    # Read an integer from standard in
    li $v0, 5
    syscall

    # Call the power_of_2 function with the integer that was read
    move $a0, $v0
    # jal stands for "jump and link". It stores the address to jump back to
    # (the return address) in $ra automatically.
    jal power_of_2

    # Call the print_int function which prints the integer with a newline
    move $a0, $v0
    jal print_int

    j exit

# Computes a power of 2.
# Parameters:
#   $a0: the exponent
# Return values:
#   $v0: 2 raised to $a0
power_of_2:
    # Computing a power of 2 is easy with shifting. Load 1 into $t0 and then
    # shift it left by the exponent, storing the result in $v0
    li $t0, 1
    sllv $v0, $t0, $a0
    # jr jumps to an address stored in a register. $ra contains the return
    # address, so we jump there to return
    jr $ra

# Prints an integer to standard out, followed by a newline.
# Parameters:
#   $a0: the integer to print
# Return values:
#   None
print_int:
    # The integer to print is already in $a0
    li $v0, 1
    syscall

    li $a0, '\n'
    li $v0, 11
    syscall

    jr $ra

exit:
    li $v0, 10
    syscall
