Jump to content

anyone here know mips (assembly)?

hey if you know mips maybe you can help me out, im native to java and python so this is leaving me scratching my head. my problem is to sort an array of a fixed length. I think what I have written is a viable solution, but am getting errors when i compile.

			.text			.globl main	main:		la 	$a0, Array		# sets the base address of the array to $a0	loop:		lw 	$t0, 0($a0) 		# sets $t0 to the current element in array			lw 	$t1, 4($a0)		# sets $t1 to the next element in array			blt	$t1, $t0, swap		# if the following value is greater, swap them			addi	$a0, $a0, 4		# advance the array to start at the next location from last time			j	loop			# jump back to loop so we can compare next two elements				swap:		sw	$t0, 4($a0)		# store the greater numbers contents in the higher position in array (swap)			sw	$t1, 0($a0)		# store the lesser numbers contents in the lower position in array (swap)			li	$a0, 0			# resets the value of $a0 back to zero so we can start from beginning of array			j	loop			# jump back to the loop so we can go through and find next swap 	 			.data 			 	Array:		.word 	14, 12, 13, 5, 9, 11, 3, 6, 7, 10, 2, 4, 8, 1 

 

it says it is getting an array out of bounds error on line 6 which is where 'loop:" begins

 

any ideas? thanks!

Link to comment
https://linustechtips.com/topic/65516-anyone-here-know-mips-assembly/
Share on other sites

Link to post
Share on other sites

I know assembly in general, not this one in specific, but I'd say that the error is in swap, when you load address 0 to $a0 instead of Array. When you jump back to loop you try to access address 0.

This is assuming everything else works as I think it does!

I might be completely wrong though, I haven't messed with assembly like that in a while.

Also, where is the loop/main termination? I don't think you can go on forever!

Link to post
Share on other sites

How does your code know when to stop?

 

Seems like your code eventually gets to the last element in the array, loads that into $t0, then tries to load the next element into $t1, but there is no next element.

 

After the addi, you should have a instruction that branches if your offset value $a0 ends up being equal to the max offset value (52 in this case, since there are 13 elements in that array) and then proceed to end the program.

 

EDIT: And yes, like MikeD said, you shouldn't be setting $a0 to zero after the swap.  You should repeat the 'la $a0, Array' instruction.

Intel Core i7-7700K | EVGA GeForce GTX 1080 FTW | ASUS ROG Strix Z270G Gaming | 32GB G-Skill TridentZ RGB DDR4-3200 | Corsair AX860i

Cooler Master MasterCase Pro 3 Samsung 950 Pro 256GB | Samsung 850 Evo 1TB | EKWB Custom Loop | Noctua NF-F12(x4)/NF-A14 LTT Special Edition

Dell S2716DGR | Corsair K95 RGB Platinum (Cherry MX Brown) | Logitech G502 Proteus Spectrum | FiiO E17 DAC/Amp | Beyerdynamic DT990 Pro

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×