mips - 您如何在 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
解决方案
您已声明str
相当于:int str = 80;
您想使用.space 80
而不是.word 80
.
您还在初始读取字符串系统调用之前放入&str
,$v0
而它属于$a0
.
推荐阅读
- java - 无法索引类 module-info.class
- nginx - 带有 Nginx conf 的未知指令“proxy_cache_revalidate on”
- swift3 - 如何使用 swift 程序从 htmlString 中删除反斜杠?
- git - 从 commit-id 中检索 gerrit 链接
- php - 查询返回错误的数组编码器
- common-lisp - 打开文件,如果文件不存在,则执行其他操作
- mean - Microsoft SEAL:查找 PolyCRT 密文的均值
- javascript - 使用 javascript 从字符串返回 SHA-256 哈希
- javascript - 根据javascript中的匹配条件从数组数组中查找匹配项
- javascript - 将元素作为字符串输出 undefined