1

我的程序正在运行,我唯一的问题是循环运行的时间比它应该运行的时间多一倍,这会使辅音计数增加一倍。我当然可以通过从最后的计数中减去 1 来解决这个问题,但我很好奇它为什么要运行额外的时间。我已经使用 step 功能尝试在 PCSpim 中进行故障排除,我可以看到 $a0 = 0000000a 而不是 00000000 应该是最后一个循环。为什么会这样?

#################################################
#                       #
#               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 $a0,string
    li $a1,20
    li $v0, 8   # syscall 8 reads string/letter
    syscall
    
    li $t1, 0   # register used to keep track of number of vowels
    li $t2, 0   # register used to keep track of number of consonances

    la $a1, string  # prepare for passing a letter to the procedure
    

loop:   
    lb $a0, 0($a1)  # load one character from the string
    beqz $a0, End   # if no more characters then go to End
    addi $a1,$a1,1  # increment address position to print next character in string

    jal vowelp  # procedure call

    move $t0,$v0    # save the value returned from the procedure call

    beqz $t0, NotVowel
    
    addi $t1, $t1, 1

    j loop

NotVowel:
    addi $t2, $t2, 1
    
    j loop
End:    
    la $a0, answer1
    li $v0, 4
    syscall
    
    move $a0, $t1
    li $v0, 1
    syscall     # print number of vowels

    la $a0,endl # syscall to print out
    li $v0,4    # a new line
    syscall     

    la $a0, answer2
    li $v0, 4
    syscall
    
    addi $t2, $t2, -1   

    move $a0, $t2
    li $v0, 1
    syscall     # print number of consonants

    la $a0,endl # syscall to print out
    li $v0,4    # a new line
    syscall 

    li $v0,10
    syscall     # Bye!

##############################################################
## Define the procedure vowelp
##############################################################

vowelp:

    li $v0, 0   # initialize $v0 to 0
    beq $a0,' ',loop
    beq $a0,'A',yes 
    beq $a0,'a',yes
    beq $a0,'E',yes
    beq $a0,'e',yes
    beq $a0,'I',yes
    beq $a0,'i',yes
    beq $a0,'O',yes
    beq $a0,'o',yes
    beq $a0,'U',yes
    beq $a0,'u',yes
    jr $ra
yes:    li $v0,1
    jr $ra
    

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

        .data
    string:     .space 20
    prompt1:    .asciiz "Enter a string: "
    
    answer1:    .asciiz "The number of vowels in the string is: \n"
    answer2:    .asciiz "The number of consonants is: \n"
    message:    .asciiz "The character entered is not a vowel."
    ans1:       .asciiz "The character entered is a vowel. "
    endl:       .asciiz "\n"       

##
4

0 回答 0