2013-03-14 34 views
0

对于作业分配,我想采用一个整数并将其更改为字符串代表。我对MIPS有一个基本的了解,但我不明白为什么我的代码无法工作。我想知道如果有人在那里可以给我一些指针,我怎么需要做或帮助如何从手中创建一个字符串。我正在使用MARS Simulator 4.2。手动构建MIPS字符串

这是我的代码到目前为止的评论。

#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 

基本上我只是想改变2到它的ASCII值,加0来表示空字符串末尾,然后打印串出。

感谢您的帮助。

回答

2

您更改了$t1中的值,因此不再指向字符串的开头。在覆盖$t1之前,您应该重新载入地址或在另一个寄存器中复制副本。

+0

这很好。谢谢。 – morganw09dev 2013-03-15 22:26:04

-1

此外,li $v0 1用于打印整数的系统调用。如果你真的是一个字符串,你应该编码li $v0 4