edit: sorry strcopy not strcat

Hi I'm new to MIPS and just need some basic clarification on what is actually going on here. I'm trying to make a copy of string1 into string2 by reading through the string copying each bit seperately, and stopping at the 0 terminator:

string1:	.asciiz "Hello"
string2:	.space 128

main:	la	$a2, string1
	la	$a3, string2
	jal 	strcopy
moo:	la	$a0, $a3
	li	$v0, 4
	li	$v0, 10

strcopy:	move	$v0, $a2
loop:	lbu	$t0,0($a2)
	sb	$t0,0,($a3)
	addi	$a2,$a2,1
	bgtz	$t0,loop
	jal	moo

The only problem is im getting really mixed up with all my $a-something.
I've looked at other examples of strcat but I can never actually figure out which line actually overwrites the one string. I think it's cause I don't fully understand the principle of $a0.

Could someone please adjust the above so that string1 will be copied into string2?

Thanks for any help or information you can give.

Recommended Answers

All 2 Replies

Are you taking a course or just doing this on your own?

You need to watch a few things. First, though, your strcopy should look like this:

strcopy:	lbu	$t0,($a2)	# load the char at $a2
		sb	$t0,($a3)	# save the char at $a3
		addi	$a2,$a2,1	# next $a2
		addi	$a3,$a3,1	# next $a3
		bgtz	$t0,strcopy	# do it all again?
		jr	$ra		# return to caller

If you don't already have a good MIPS reference you need to get one. It makes all the difference.
This one at WikiPedia is short, but good.

1. Every jal should be to a subroutine that returns with jr $ra. Using jal means that you don't need moo: at all.

2. The link I gave you gives the general meaning of all the register names.

3. The syscall to print the string requires $a0 to have the address of the string to print and $v0 to have the value 2.

The syscall to terminate the program only requires $v0 to have the value 10.

Hope this helps.

that's really helpful, thankyou.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.