[Assembly] Easy recursive faculty calculation

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[Assembly] Easy recursive faculty calculation

Post by ayu »

This solution could have been done in 100000xPI different ways, and this one is far from the best, but I didn't have much time or energy to think it through due to the time being 23:14 now and I need sleep, and it's 26 degrees Celsius in here now....

anyway, this time I only made the SPIM code, don't have time to modify it for MIPS since I have to start with my Interruption Handler now (only supposed to handle external interrupts so far), and that might take some more time.

SPIM version

Code: Select all

	.data
	
sSize = 32
sFac  = 28
sRa   = 24

Sum:	.word	1
Fac:	.word	4

	
	.text
	.globl	main
main:
	la	$t0, Sum
	lw	$s0, ($t0)
	lw	$t1, Fac
	lw	$s1, Fac

facrec:
	subu	$sp, $sp, sSize
	sw	$t1, sFac($sp)
	sw	$ra, sRa($sp)
	beq	$t1, 1, facreturn
	sub	$t1, $t1, 1
	jal	facrec

facreturn:
	lw	$t0, sFac($sp)
	lw	$ra, sRa($sp)
	mul	$s0, $s0, $t0
	addu	$sp, $sp, sSize
	beq	$t0, $s1, printout
	j	$ra

printout:
	move	$a0, $s0
	li	$v0, 1
	syscall

end:
	li	$v0, 10
	syscall
Yet again I apologize for the lack of comments!
"The best place to hide a tree, is in a forest"

Post Reply