2013-08-24 150 views
-2
.data 
str: .asciiz "Hello Sir" 
.text 

la $a0, str # load the address of the string 
addi $t0, $a0, 0 # starting address of array 
#TODO: find length of string, it's zero terminated, so loop until a zero 

strLen:     #getting length of string 
lb  $t1, str($t2) #loading value 
add  $t2, $t2, 1 
bne  $t1, $zero, strLen 
sub  $t2, $t2, 1 
li  $v0, 11   #load imediate - print low-level byte 



addi $t1, $a0, 21 # initialize loop counter to array end position 
loop: lb $a0, 0($t0) # load a single character 
    li $v0, 11 # specify print character service 3 
    syscall # print 
    addi $t0, $t0, 1 # add 1 to the loop index 
    blt $t0, $t1, loop # continue if not at string length 


Loop: 
sub  $t2, $t2, 1 
la  $t1, str($t2) #loading value 
lb  $a0, ($t1) 
syscall 

bnez $t2, Loop 
    li $v0, 10 # system call for exit 
    syscall # we are out of here. 

我想创建打印出字符串并打印出反向的代码。例如“Hello SirriS olleH”。我将如何更改此行打印出反向字符串

addi $t1, $a0, 21 # initialize loop counter to array end position 

因此,它将初始化为给定字符串的长度,而不是默认设置为21。干杯。

回答

2

鉴于你已经有strLen它产生的字符串长度在$t2你可以直接在比较中使用该寄存器。如果你愿意的话,你当然也可以把它复制到$t1,但这样做没有多大意义。

备注:在您的strLen循环中打印正向字符串可能会有意义,因此您可以摆脱第二个循环。

最后,我建议你使用更好的标签,loopLoop是不是很丰富的信息,使用不同的标签是不好的,无论如何是不好的想法。