0

Hello I have a program that will alphabetize a 20 character string the user enters, while also filtering out characters that are not lower case letters. I have the program alphabetizing with a recursive loop, then have a more simple filter set up to weed out the characters that are not lower case letters. I can get the program to correctly alphabetize and filter out the characters, once that happens the program asks if the user wants to try again. This is where I am encountering a problem, when the previous string is longer than the subsequent one there are left over characters printed out.

For example, if the 1st string alphabetizes to abcdefgxyz and the 2nd should only be abcdefg, the output will still show the xyz.

Any help will be greatly appreciated!

Here is my code

        .text
main:   la $a0, prompt    
        li $v0, 4
        syscall

        la $a0, input   
        li $a1, 21      
        li $v0, 8
        syscall

        la $t0, input
        la $t7, input
        la $t5, final

loop:   lb $t1, 0($t0)          
        lb $t2, 1($t0)
        ble $t1, $t2, incr
        beqz $t2, filter
        jal swap        
        j loop          

incr:   addi $t0, $t0, 1
        j loop

filter: lb $t3, 0($a0)
        blt $t3, 0x61, next
        bgt $t3, 0x7a, next
        sb $t3, 0($t5)
        add $t5, $t5, 1

next:   beqz $t3, output
        addi $a0, $a0, 1
        j filter

output: la $a0, display
        li $v0, 4
        syscall
        la $a0, final
        syscall
        la $a0, again
        syscall
        li $v0, 12
        syscall

        beq $v0, 0x79, clr
        j end

clr:    li $a0, 0x0a
        li $v0, 11
        syscall
        j main

end:    la $a0, termin
        li $v0, 4
        syscall
        li $v0, 10       
        syscall

swap:   sub $sp, $sp, 4         
        sw $ra, ($sp)           
        sb $t1, 1($t0)          
        sb $t2, 0($t0)
        beq $t0, $t5, return    
        sub $t0, $t0, 1         
        lb $t1, 0($t0)          
        lb $t2, 1($t0)
        ble $t1, $t2, return    
        jal swap        

return: addi $t0, $t0, 1        
        lw $ra, ($sp)
        addi $sp, $sp, 4
        jr $ra


        .data
prompt: .asciiz "Please enter a string to alphabetize and filter: "
display:.asciiz "\nThe alphabetized and filtered string is: "
again:  .asciiz "\nDo you want to try again (y for yes)? "
termin: .asciiz "\nProgram terminated."
input:  .space 21
final:  .space 21

1 Answers1

0

It looks like your syscall with $v0=8 doesn't null terminate your input string. Perhaps there is a different syscall that will do that.

If not, then you should overwrite the input buffer with zeros before you call syscall with $v0=8.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123