Morgan Kenyon
Morgan Kenyon

Reputation: 3172

Build MIPS String by Hand

For a homework assignment I am suppose to take a integer and change it into its string representative. I have a basic understanding of MIPS, but I don't really understand why my code doesn't work. I'm wondering if anyone out there could give me some pointers how what I need to do or help on how to create a string from hand. I'm using MARS Simulator 4.2.

Here's my code so far with comments.

#itoa
#$t0 = initial integer
#$t1 = place where string is stored
#This program I'm attempting to by hand create a null ended string from an original integer 2.


li $t0, 2              #load integer 2
la $t1, number         #load memory location for string


addi $t0, $t0, 48       #add 48 to 2 to get ASCII character, 50
sb $t0, ($t1)           #store it in original byte of $t1


add $t1, $t1, 1     #increment $t1, to point to next byte
sb $zero, ($t1)     #store #zero in the next byte

move $a0, $t1           #move the hopefully finished string to print out
li $v0, 1
syscall               #print out string


#exit program
li $v0, 10
syscall

.data
 number: .space 1024

I'm basically just trying change 2 to it's ASCII value, add a 0 to represent the null string end, and then print that string out.

Thanks For Any Help.

Upvotes: 0

Views: 4685

Answers (2)

user3533757
user3533757

Reputation: 1

Also, li $v0 1 is for a syscall to print an integer. If you're really making a string, you should code li $v0 4.

Upvotes: -1

Musa
Musa

Reputation: 97672

You changed the value in $t1 so it no longer points to the begining of the string. You should reload the address or make a copy in another register before you overwrite $t1.

Upvotes: 2

Related Questions