
-----------------------------------
md
Fri Jan 20, 2006 6:28 pm

The Joys of MIPS
-----------------------------------
For all of you who are thinking about taking CS at UW, and anyone else who wants to hurt their brains here is some of the code I've had to write thus far in MIPS. MIPS is an assembly language/processor design a la RISC.

Since the assigments that this was for is now due you'd be hard pressed to copy my stuff, and I figured someone might be interested in the mind blowing drugery that is assembly.

#	Course:		CS241
#	Assignment:	1, Part 4
#	UserID:		jblackwo
#
#

.globl a1p4

#######################################
#	Function a1p4
#		Calculates the fibonacci sequence and stores it in the passed
#		array.
#
#	Registers:
#		$2, $3 are used to store the last two numbers
#		$4 is used to count how many numbers we've saved
#		$5 is the total we're suppsed to save
#		$6 is the address of the current element in the array
#		$10 is a temporary storage variable
#
#	case_1: total = 1
#	case_2: total = 2
#	case_3: total > 2

a1p4:	addi $30, $30, -24	# save registers, 6*4 = 24 bytes needed
	sw $2,   0($30)
	sw $3,   4($30)
	sw $4,   8($30)
	sw $5,  12($30)
	sw $6,  16($30)
	sw $10, 20($30)

	lw $5, 24($30)		# load the total number of elements to find		
	lw $6, 28($30)		# load the address of the array
		
	# check for nothing
	beq $5, $0, return	# if( $5 = 0) return
	
	# check for case 1
	addi $10, $0, 1		# set $10 to 1
	beq $5, $10, case_1	# if( $5 = 1) do case 1
	
	# check for case 2
	addi $10, $0, 2		# set $10 to 2
	beq $5, $10, case_2	# if( $5 = 2) do case 2

	# else run case 3
case_3:	addi $4, $0, 0		# initialize counter to 0
	addi $2, $0, 1		# first number is 1
	addi $3, $0, 1		# second number is 1
	
	# n = 1
	sw $2, 0($6)		# store 1 in the first spot in the array
	addi $6, $6, 4		# increment the address pointer (4 byte words)
	addi $4, $4, 1		# increment the counter
	
	# n = 2
	sw $2, 0($6)		# store 1 in the second spot in the array
	addi $6, $6, 4		# increment the address pointer (4 byte words)
	addi $4, $4, 1		# increment the counter
	
	# for $5 > n > 2
c3_lp:	sub $10, $5, $4		# $10 = $5 - $4
	blez $10, return	# if $10 