  # Name: Liam Kirsh, Kevin Yang
  # Section: 1
  # Description: Write a function which does exponentiation: x raised to the
  # power of y.
  
  # #include <stdio.h>

  # int power(int num, int exp) {
  #    if (!exp)
  #       return 1;
  #    int returnVal = num;
  #    int increm = num;
  #    int numStore = num;

  #    while (--exp) {
  #       num = numStore;
  #       while (--num) {
  #          returnVal += increm;
  #       }
  #       increm = returnVal;
  #    }
  #    return returnVal;
  # }

  # int main() {
  #    int base, pow;
  #    printf(" Please enter base: ");
  #    scanf(" %d", &base);
  #    printf(" Please enter power: ");
  #    scanf(" %d", &pow);
  #    printf("Result = %d", power(base, pow));
  # }

  
# declare global so programmer can see actual addresses.
.globl prompt_bas
.globl prompt_pow
.globl resText

#  Data Area (this area contains strings to be displayed during the program)
.data

prompt_bas:
	.asciiz " Please enter base: " # (len 21)
    
prompt_pow:
    .asciiz " Please enter power: " # (len 22)

resText: 
	.asciiz " \n Result = "

#Text Area (i.e. instructions)
.text

main:

	# printf(" Please enter base: ");
	ori     $v0, $0, 4
	lui     $a0, 0x1001
	syscall

	# scanf(" %d", &base);
	ori     $v0, $0, 5
	syscall
    # $v0 now has the value of the first integer

	# Clear $s0 for base storage
	ori     $s0, $0, 0

	# Add base integer to s0
	addu    $s0, $v0, $s0
	
	# printf(" Please enter power: ");
	# 0x15 is hexidecimal for 21 decimal (the length of the previous prompt message)
	ori     $v0, $0, 4			
	lui     $a0, 0x1001
	ori     $a0, $a0, 0x15
	syscall

	# scanf(" %d", &pow);
	ori	$v0, $0, 5
	syscall
	# $v0 now has the value of the second integer
    
    # $s1 - pow storage
	addu    $s1, $v0, $s1
    
    # $s2 - pow temp storage
    addiu   $s2, $s1, 0
    # Let's put it in $s4 too - increm (constant)
    addiu   $s4, $s0, 0
    
    # $s5 base storage for resetting the inner loop count (const)
    addiu   $s5, $s0, 0

outer:
    # while (--exp) {
    addiu   $s2, $s2, -1 # powCnt--
    beqz    $s2, done # if !powCnt j done
    # Gotta reset the inner pow count each time. Thanks s5
    addiu   $s1, $s5, 0 # j = baseCnt
inner:
    # while (--num) {
    addiu   $s1, $s1, -1 # num--
    beqz    $s1, newIncrem # if !num j newIncrem
    # returnVal += increm;
    addu    $s0, $s0, $s4
    b inner # end inner loop
newIncrem:
    addiu   $s4, $s0, 0 # increm = returnVal;
    b outer # end outer loop
    
done:
    # printf("Result = %d\n", answer);
	ori     $v0, $0, 4			
	lui     $a0, 0x1001
	ori     $a0, $a0, 0x2b # 0x1001 += decimal 43
	syscall
	
	# Display the remainder
	ori     $v0, $0, 1	# load 1 into $v0 to display an integer		
	add 	$a0, $s0, $0
	syscall
	
	# Exit (load 10 into $v0)
	ori     $v0, $0, 10
	syscall
