保持改变我的字符串的字符

时间:2018-06-13 09:25:00

标签: assembly qtspim

所以我想用一个字符更改字符串上的所有字符。我已经这样做了:

    .text
    .globl __start
__start:

    la $t2,str# t2 points to the string
    la $a0,str
    li $a1,256
    li $v0,4
    syscall

    la $a0,endl # system call to print
    li $v0,4 # out a newline
    syscall

    li $t4,'-'
    li $t1,0 # t1 holds the count
    nextCh: lb $t0,($t2) # get a byte from string
    beqz $t0,strEnd # zero means end of string
    sw $t4,($t2)
    add $t1,$t1,1 # increment count
    add $t2,1 # move pointer one character
    j nextCh # go round the loop again

    strEnd:
    move $a0,$t1 # system call to print
    li $v0,1 # out the length worked out
    syscall

    la $a0,endl # system call to print
    li $v0,4 # out a newline
    syscall
    li $v0,10
    syscall # au revoir...

.data
str: .asciiz "abcdefghij"
endl: .asciiz "\n"

这只是给了我存储在数据部分中的字符串和一个几乎可以获取字符串的每个字符的循环。我应该添加到我的代码中以便我可以得到这个:

abcdefghij
----------

1 个答案:

答案 0 :(得分:0)

正确答案是:

    .text
    .globl __start
__start:

    la $t2,str# t2 points to the string
    la $a0,str
    li $a1,256
    li $v0,4
    syscall

    la $a0,endl # system call to print
    li $v0,4 # out a newline
    syscall


    li $t1,0 # t1 holds the count
    nextCh: lb $t0,($t2) # get a byte from string
    beqz $t0,strEnd # zero means end of string
    li $t4,'-'
    sb $t4,($t2)
    add $t1,$t1,1 # increment count
    add $t2,1 # move pointer one character
    j nextCh # go round the loop again

    strEnd:
    la $a0,str
    li $v0,4
    syscall 

    la $a0,endl # system call to print
    li $v0,4 # out a newline
    syscall
    li $v0,10
    syscall # au revoir...

.data
str: .asciiz "abcdefghij"
ans: .asciiz "Length is "
endl: .asciiz "\n"