From 0c4ad9b9bc7571d6baaa77353465ec7149d17428 Mon Sep 17 00:00:00 2001 From: JonahSpicher Date: Tue, 5 Nov 2019 19:40:15 -0500 Subject: [PATCH] added exponentiate test --- asmtest/mguzman_jspicher/Exp/exp.asm | 63 +++++++++++++++++++ asmtest/mguzman_jspicher/Exp/exp.dat | 38 +++++++++++ .../mguzman_jspicher/LCM/{LCM.hex => LCM.dat} | 0 .../Multiply/{Multiply.hex => Multiply.dat} | 0 asmtest/mguzman_jspicher/README.md | 9 +++ 5 files changed, 110 insertions(+) create mode 100644 asmtest/mguzman_jspicher/Exp/exp.asm create mode 100644 asmtest/mguzman_jspicher/Exp/exp.dat rename asmtest/mguzman_jspicher/LCM/{LCM.hex => LCM.dat} (100%) rename asmtest/mguzman_jspicher/Multiply/{Multiply.hex => Multiply.dat} (100%) diff --git a/asmtest/mguzman_jspicher/Exp/exp.asm b/asmtest/mguzman_jspicher/Exp/exp.asm new file mode 100644 index 0000000..ef0d907 --- /dev/null +++ b/asmtest/mguzman_jspicher/Exp/exp.asm @@ -0,0 +1,63 @@ +#Main: +addi $sp, $zero, 16380 +ddi $a0, $zero, 8 +addi $a1, $zero, 0 +jal exp +add $s0, $v0, $zero + +addi $a0, $zero, 12 +addi $a1, $zero, 1 +jal exp +add $s1, $v0, $zero + +addi $a0, $zero, 3 +addi $a1, $zero, 2 +jal exp +add $s2, $v0, $zero + +addi $a0, $zero, 4 +addi $a1, $zero, 3 +jal exp +add $s3, $v0, $zero + + + + +j finish + + +multiply: #Multiplies $a0 by $a1, stores in $v0 +addi $t0, $zero, 0 # Running total +addi $t1, $zero, 0 # Counter for loop +loop: +beq $t1, $a1, exit # If i == b: +add $t0, $t0, $a0 #sum += a +addi $t1, $t1, 1 # i++ +j loop + +exit: +add $v0, $zero, $t0 +jr $ra + + +exp: #Stores $a0 ^ $a1 in $v0 (a^b) +beq $a1, $zero, base #Go to base case if b is 0 + +addi $sp, $sp, -4 +sw $ra, 0($sp) + +addi $a1, $a1, -1 +jal exp #Calls a^(b-1) + +add $a1, $v0, $zero #moves result of previous exp call to $v0 +jal multiply # Calls multiply, storing $a0 * $a1 in $v0 + +lw $ra, 0($sp) # Get our return address back from the stack +addi $sp, $sp, 4 +jr $ra #We can return right away because $v0 is where we wanted it + +base: +addi $v0, $zero, 1 #return 1 +jr $ra + +finish: diff --git a/asmtest/mguzman_jspicher/Exp/exp.dat b/asmtest/mguzman_jspicher/Exp/exp.dat new file mode 100644 index 0000000..91386e6 --- /dev/null +++ b/asmtest/mguzman_jspicher/Exp/exp.dat @@ -0,0 +1,38 @@ +201d3ffc +20040008 +20050000 +0c00001a +00408020 +2004000c +20050001 +0c00001a +00408820 +20040003 +20050002 +0c00001a +00409020 +20040004 +20050003 +0c00001a +00409820 +08000026 +20080000 +20090000 +11250003 +01044020 +21290001 +08000014 +00081020 +03e00008 +10a00009 +23bdfffc +afbf0000 +20a5ffff +0c00001a +00402820 +0c000012 +8fbf0000 +23bd0004 +03e00008 +20020001 +03e00008 diff --git a/asmtest/mguzman_jspicher/LCM/LCM.hex b/asmtest/mguzman_jspicher/LCM/LCM.dat similarity index 100% rename from asmtest/mguzman_jspicher/LCM/LCM.hex rename to asmtest/mguzman_jspicher/LCM/LCM.dat diff --git a/asmtest/mguzman_jspicher/Multiply/Multiply.hex b/asmtest/mguzman_jspicher/Multiply/Multiply.dat similarity index 100% rename from asmtest/mguzman_jspicher/Multiply/Multiply.hex rename to asmtest/mguzman_jspicher/Multiply/Multiply.dat diff --git a/asmtest/mguzman_jspicher/README.md b/asmtest/mguzman_jspicher/README.md index 7acfc3f..bfbd5ff 100644 --- a/asmtest/mguzman_jspicher/README.md +++ b/asmtest/mguzman_jspicher/README.md @@ -16,3 +16,12 @@ Finds the least common multiple of a and b. Sets a to 6, sets b to 4. - Expected Result: 12 - Program is formatted as hexadecimal, answer stored in register 2 ($v0) - No additional instructions required + + +### Exponentiate + +Contains a function which finds a^b recursively and using a separte multiply function. Contains four calls, finding 8^0, 12^1, 3^2, and 4^3. Stores results in $s0, $s1, $s2, and $s3 respectively. + +- Expected Results: 1, 12, 9, 64 +- Program given as a .asm and a .dat. +- Sets $sp to 3ffc on first line, otherwise no memory format requirements.