MIPS组件 - 从输入字符串中删除元音

时间:2014-03-11 05:56:42

标签: string assembly mips

以下是我的MIPS汇编程序代码,该程序旨在从输入字符串中删除元音,然后打印新字符串。就目前而言,程序根本不会删除元音,只是重新打印输入的相同字符串。

    .text
    .globl  main

main:
    # display prompt
    li      $v0, 4
    la      $a0, prompt
    syscall
    # accept input string
    li      $v0, 8
    la      $a0, str
    li      $a1, 82
    syscall

    li      $t0, 0          # add a null to the bottom of the stack
    subu    $sp, $sp, 4
    sw      $t0, ($sp)
    li      $t1, 0          # initiate index

pushloop:
    lbu     $t0, str($t1)   # (I think the program is placing a zero in $t0 here, thus causing it to branch immediately to poploop, which then branches to the end since only the null has been pushed onto the stack)
    beqz    $t0, poploop    # once we reach null char, finish
    subu    $sp, $sp, 4
    sw      $t0, ($sp)
    addiu   $t1, $t1, 1
    j       pushloop
    nop
    # $t1 is not reset

poploop:
    lw      $t0, ($sp)
    addu    $sp, $sp, 4
    beqz    $t0, done       # once we reach null char, finish
    nop
    # check if vowel
    li      $t2, 0x61       # a
    beq     $t0, $t2, vowel
    nop
    li      $t2, 0x65       # e
    beq     $t0, $t2, vowel
    nop
    li      $t2, 0x69       # i
    beq     $t0, $t2, vowel
    nop
    li      $t2, 0x6F       # o
    beq     $t0, $t2, vowel
    nop
    li      $t2, 0x75       # u
    beq     $t0, $t2, vowel
    nop
    # if not a vowel, store it at current index in string
    sb      $t0, str($t1)
    j       decrement
    nop
vowel:  # if vowel, remove character
    li      $t0, 0
    sb      $t0, str($t1)
decrement:
    addiu   $t1, $t1, -1        # decrement index
    j       poploop
    nop
done:   
    li      $v0, 4
    la      $a0, str
    syscall
    li      $v0, 10         # exit program
    syscall
    nop

    .data
str:    .space 82
prompt: .asciiz "Input a string:\n"

1 个答案:

答案 0 :(得分:1)

因此。我看了你写的是什么,我已经修好了。

我的第一个想法是我不知道你在处理堆栈和堆栈指针($sp)。它似乎没有必要,所以我把它拿出来了。

接下来是方法错了。您的方法是搜索字符串并用0替换每个小写元音(或至少'a','e','i','o'和'u')。这不起作用。

如果您考虑一个典型的C字符串,它们会被0删除,所以如果您使用字符串My name is Jeoff并应用您的算法,您将获得My n\0m\0 \0s J\0\0ff当然会打印为My n

所以相反,我选择了一个单独的算法,它选择不在元音的情况下存储任何东西,而是将所有后续字符移过1.这样做我们可以轻松地从字符串中删除所有元音而不需要辅助缓冲区

看看下面的内容:

    .text
    .globl  main

main:
    # display prompt
    li      $v0, 4
    la      $a0, prompt
    syscall
    # accept input string
    li      $v0, 8
    la      $a0, str
    li      $a1, 82
    syscall

    li      $t1, 0          # initiate index
    li      $t3, 0          # vowel count

poploop:
    lb $t0 str($t1)

    # check if vowel
    li      $t2, 'a'       # a
    beq     $t0, $t2, vowel
    nop
    li      $t2, 'e'       # e
    beq     $t0, $t2, vowel
    nop
    li      $t2, 'i'       # i
    beq     $t0, $t2, vowel
    nop
    li      $t2, 'o'       # o
    beq     $t0, $t2, vowel
    nop
    li      $t2, 'u'       # u
    beq     $t0, $t2, vowel
    nop

    # if not a vowel, store it at current index in string less vowel count
    sub     $t2, $t1, $t3
    sb      $t0, str($t2)
    j       next
    nop
vowel:  # if vowel, inc count
    addi $t3, $t3, 1
next:
    addi $t1, $t1, 1

    beqz    $t0, done       # once we reach null char, finish
    nop
    j       poploop
    nop

done:   
    li      $v0, 4
    la      $a0, str
    syscall
    li      $v0, 10         # exit program
    syscall
    nop

    .data
str:    .space 82
prompt: .asciiz "Input a string:\n"