[Assembly] Easy iterative 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 iterative faculty calculation

Post by ayu »

Doing some minor assignments in Assembly to train in the SPIM environment, and since SPIM does some things differently then MIPS (which is dumb as hell since SPIM is a MIPS simulator), I had to write two different codes.


MIPS version

I haven't been able to test this one since I don't have a MIPS board at home, and I just threw it together real quick to demonstrate the difference between the two.

Code: Select all

#include <regdef.h>

		.data
Sum:	.word	1 
Fak:		.word	4 
Msg:		.asciiz	"The faculty of !%d is %d \n"

		.text
		.globl main

main:
		la	t0, Sum
		lw	t1, Fak
		lw	s0, (t0)

loop:	
		mul	s0, s0, t1
		sub	t1, t1, 1
		beq	t1, 1, print
		j	loop

print:
		move	a1,t1
		move	a2,s0		
		lw	        a0,Msg
		jal		printf
	
end:	
		break

SPIM version

I was unable to create a proper output for this version, SPIM does it differently then MIPS does it (or maybe it doesn't, since printf is not a "real" instruction)

Code: Select all

		.data
Sum:	.word	1 
Fak:		.word	4 

		.text
		.globl main

main:
		la	$t0, Sum
		lw	$t1, Fak
		lw	$s0, ($t0)

loop:	
		mul	$s0, $s0, $t1
		sub	$t1, $t1, 1
		beq	$t1, 1, print
		j	loop

print:
		move	$a0,$s0
		li	$v0, 1
		syscall		
	
end:
		li	$v0, 10
		syscall
I apologize for the lack of comments ^^
"The best place to hide a tree, is in a forest"

Post Reply