меняем символы моей строки - PullRequest
0 голосов
/ 13 июня 2018

Итак, я хочу изменить все символы в моей строке одним символом. Я сделал это:

    .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 голосов
/ 13 июня 2018

правильный ответ таков:

    .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"
...