简体   繁体   中英

MIPS assembly - removing vowels from an input string

The following is my code for a MIPS assembly program that is intended to remove vowels from an input string and then print the new string. As it stands, the program simply does not remove the vowels and just reprints the same string that was input.

    .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"

So. I took a look at what you've writen and I've fixed it.

My first thought is I don't know what you are doing with the stack and stack pointer ( $sp ). It didn't seem necessary so I took it out.

Next is that the approach is wrong. Your approach is to search the string and replace every lower-case vowel (or at least 'a', 'e', 'i', 'o' and 'u') with a 0 . This will not work.

If you think about a typical C string, they are delimeted by a 0 , so if you take the string My name is Jeoff and apply your algorithm you will get My n\\0m\\0 \\0s J\\0\\0ff which of course will print as My n .

So instead, I chose a separate algthm that chooses to not store anything in the case of a vowel and instead shift all following characters over by 1. In so doing we can easily remove all vowels from a string without requiring a secondary buffer.

Take a look below:

    .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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM