#include <cosc065.h>

#define BASE  r13
#define EXP   r14
#define TOTAL r15

.rodata

HELLO:	.string "COSC 065 Exponent program\n"
PROMPT1:.string "Base "
PROMPT2:.string "Exponent "
ANSWER:	.string "Answer = "
	
.text
.globl main

main:
        /* Function prolog. */
        /* Sets up environment for user program to execute. */
        stwu    r1,-32(r1)  /* Make room on Stack for O/S values.  */
        mflr    r0          /* Store O/S return address on Stack.  */
        stw     r0,36(r1)
        stw     r31,28(r1)  /* Store O/S frame pointer on Stack.   */
        mr      r31,r1      /* new stack top is now frame pointer. */

        /* Start of your program. */

	lis	r3, HELLO@ha      /* Print a starting message. */
	addi	r3, r3, HELLO@l
	bl	printf
	
input1:	
	lis	r3, PROMPT1@ha    /* Print prompt. */
	addi	r3, r3, PROMPT1@l
	bl	printf
	
	bl	getnum		  /* Input number. */
	cmpwi	r3, 0             /* Only accept numbers >= 0. */
	blt	input1
	mr	BASE, r3          /* This is the base value. */
input2:	
	lis	r3, PROMPT2@ha    /* Print prompt. */
	addi	r3, r3, PROMPT2@l
	bl	printf
	
	bl	getnum		  /* Input number. */
	cmpwi	r3, 0             /* Only accept numbers >= 0. */
	blt	input2
	mr	EXP, r3	          /* This is the exponent value. */

	li	TOTAL, 1          /* Initialize Multiplicand. */
loop:	cmpwi	EXP, 0            /* Exponent becomes counter for */
	ble	done              /*  number of multiplications.  */

	mullw	TOTAL, TOTAL, BASE
	subi	EXP, EXP, 1
	b	loop
	
done:
	lis	r3, ANSWER@ha     /* Print out the final answer. */
	addi	r3, r3, ANSWER@l
	bl	printf
	mr	r3, TOTAL
	bl	printnum
	
        /* End of your program. */

        /* Function epilogue. */
        /* Restores the environment from the O/S. */
        lwz r11,0(r1)     /* Restore O/S Stack pointer.          */
        lwz r0,4(r11)     /* Restore O/S return address.         */
        mtlr r0           
        lwz r31,-4(r11)   /* Restore O/S frame pointer.          */
        mr r1,r11
        blr               /* Return to Operating System.         */
