mardi 10 février 2015

Does this count as plagiarism?


In my compilers class assignment, we were asked to write some MIPS code. One of the problem involved implementing fact() function as a sub-problem. I wanted to implement the standard recursive version



int fact ( int n )
{
if ( n == 0 || n == 1 ) return 1;
return n * fact(n-1);
}


As I am new to MIPS, I looked up here on how to implement recursive routines. Unfortunately, the link explained recursion using fact() function itself. As I dint want to "copy" code, I just understood the concept of recursion and wrote the fact myself.


But my fact code seems very similar to the one I looked up to learn recursion in MIPS. Is it for the fact that I glanced at the code already or the fact that factorial is really a very simple function with hardly 10 lines in it? Am I guilty of plagiarism here?


Here is my code



fact: addi $sp, $sp, -8 # decrement SP to make room for pushing input var and ret addr
sw $ra, 4($sp) # save $ra
sw $a0, 0($sp) # save input on stack

li $v0, 1 # Set v0 to 1 initially.
beq $a0, 0, fact_ret # if ( n == 0 ) return 1;
beq $a0, 1, fact_ret # if (n == 1) return 1;

addi $a0, $a0, -1
jal fact # Recursive call fact(n-1)
lw $a0, 0($sp)
mult $v0, $a0 # n * fact(n-1)
mflo $v0 # Save answer in v0
fact_ret:
lw $ra, 4($sp) # restore $ra
addi $sp, $sp, 8 # restore $sp
jr $ra # return from function




Aucun commentaire:

Enregistrer un commentaire