如何在Mips中将int写入字符串

时间:2018-11-10 18:21:02

标签: assembly mips mars-simulator

我要用户输入,我想用该单词的大写字符数替换该单词的每个字符

示例: 输入:AaAA 输出:3333(4个字母-3个大写字母)

计算单词中大写字母的部分有效(存储在$ t5中)

但是我无法将“ 3333”替换为“ AaAA”。我正在尝试使用word_replace部分中的sb来做到这一点。在我的输出中,我可以看到方框而不是数字。

这是我的代码:

.data
prompt: .asciiz "Enter a string: "
msgout: .asciiz "Output string: "
input:  .space  256
output: .space  256
    .text
    .globl main

main:
    li  $v0, 4          # Print enter a string prompt
    la  $a0, prompt     
    syscall

    li  $v0, 8          # Ask the user for the string they want to reverse
    la  $a0, input      # We'll store it in 'input'
    li  $a1, 256        # Only 256 chars/bytes allowed
    syscall

    la  $t2, ($a0)      # t2 - input string

    word:
        li  $t1, 0              # Normal counter
        li  $t5, 0              # Uppercase counter

        word_countUppercase:
            add $t3, $t2, $t1       # $t2 is the base address for our 'input' array, add loop index
            lb  $t4, 0($t3)     # load a byte at a time according to counter    

            bltu    $t4, ' ', word_prereplace   # We found end of word

            addi    $t1, $t1, 1     # Advance our counter (i++)

            bltu    $t4, 'A', word_countUppercase
            bgtu    $t4, 'Z', word_countUppercase

            addi    $t5, $t5, 1         # Advance our counter (i++)
            j   word_countUppercase

        word_prereplace:
            la  $t2, ($a0)      # t2 - input string
            li  $t1, 0          # Normal counter

            word_replace:
                add $t3, $t2, $t1       # $t2 is the base address for our 'input' array, add loop index
                lb  $t4, 0($t3)     # load a byte at a time according to counter    

                bltu    $t4, ' ', exit      # We found end of word

                sb  $t5, output($t1)    # Overwrite this byte address in memory 

                addi    $t1, $t1, 1         # Advance our counter (i++)
                j   word_replace


exit:
    li  $v0, 4          # Print msgout
    la  $a0, msgout
    syscall

    li  $v0, 4          # Print the output string!
    la  $a0, output
    syscall

    li  $v0, 10         # exit()
    syscall

0 个答案:

没有答案