首页 > 解决方案 > 您如何在 PCspim MIPS 汇编语言上正确输出字符串元音和非元音和?

问题描述

我是新的 StackOverflow 用户,我想知道我的代码到底有什么问题?现在,无论字符串输入如何,输出都将始终显示 0 个元音和 2 个非元音。

##
##  num-vowel.s
##

#################################################
#                                               #
#               text segment                    #
#                                               #
#################################################

        .text
        .globl __start
__start:                    # execution starts here


    la $a0,prompt1      # print prompt on terminal
    li $v0,4            # system call to print
    syscall             # out a string

    la $v0,str
    li $a1,82
    li $v0, 8       # syscall 8 reads string/letter
    syscall

    li $t1, 0           # initialize the index and start at 0
    li $t3, 0       # initialize the vowel count and start at 0
    li $t4, 0       # initialize the non-vowel count and start at 0

loop:
    lb $t0 str($t1)

    li $t2, 'a'
    beq $t0, t2, isVowel    # Tests the string letter input for vowels
    nop         # nop is the same as sll zero, zero, 0

    li $t2, 'A'
    beq $t0, t2, isVowel    
    nop

    li $t2, 'e'
    beq $t0, t2, isVowel
    nop

    li $t2, 'E'
    beq $t0, t2, isVowel
    nop

    li $t2, 'i'
    beq $t0, t2, isVowel    
    nop

    li $t2, 'I'
    beq $t0, t2, isVowel
    nop

    li $t2, 'o'
    beq $t0, t2, isVowel
    nop

    li $t2, 'O'
    beq $t0, t2, isVowel    
    nop

    li $t2, 'u'
    beq $t0, t2, isVowel
    nop

    li $t2, 'U'
    beq $t0, t2, isVowel
    nop

    addi $t4, $t4, 1    # if not a vowel, increment non vowel sum

    j next
    nop

isVowel:
    addi $t3, $t3, 1    # if it is a vowel, increment vowel sum

next:
    addi $t1, $t1, 1

    beqz $t0, done      # once the program reaches the null char, perform output
    nop

    j loop
    nop

done:
    la $a0,vowelMsg1    # print vowelMsg1
    li $v0,4            
    syscall


    move $a0, $t3
    li $v0, 1       # Prints the vowel sum
    syscall


    la $a0,vowelMsg2    # print vowelMsg2
    li $v0,4            
    syscall


    la $a0,vowelMsg3    # print vowelMsg3
    li $v0,4            
    syscall


    move $a0, $t4
    li $v0, 1       # Prints the non-vowel sum
    syscall


    la $a0,vowelMsg4    # print vowelMsg4
    li $v0,4            
    syscall


        li $v0, 10      # Program end, bye!
        syscall



#################################################
#                       #
#               data segment            #
#                       #
#################################################

        .data

    vowelSum:   .word 0
    nonVowelSum:    .word 0

    prompt1:    .asciiz "Enter a sentence and/or a word: "
    str:        .word 80
    strEnd:     .word 0

    vowelMsg1:  .asciiz "What you have entered has "

    vowelMsg2:  .asciiz " vowels.\n"

    vowelMsg3:  .asciiz "What you have entered has "

    vowelMsg4:  .asciiz " non-vowels.\n"



##
##  end of file num-vowel.s

标签: mips

解决方案


您已声明str相当于:int str = 80;

您想使用.space 80而不是.word 80.

您还在初始读取字符串系统调用之前放入&str$v0而它属于$a0.


推荐阅读