mardi 21 février 2017

Output Error in MIPS

I am running a program where the user is prompted for a string, and then the string is taken and modified. A random spot in the string is selected using a random number generator, and that char is replaced with a *. This is supposed to replace four characters in the string, not allowing a duplicate spot to be "changed".

This runs and replaces characters with stars, but when the first star spot is picked and replaced, any text to the left of the character (which is now a star) becomes cut off, not outputting the full string.

Here is my code thus far, I have been debugging for a while but may just need a fresh pair of eyes. Any help is appreciated. Thanks all

    .data
    question: .asciiz "Enter your string?\n"
    input_str: .space 64
    output: .asciiz "Here is your output:\n"

.text

main:
    #display question
    la $a0, question
    li $v0, 4
    syscall

    #read in string
    la $a0, input_str
    li $a1, 63
    li $v0, 8
    syscall

    move $s2, $a0 #our string is in $a2
    li $t0, 0
    j find_length




find_length:
    addi $a0, $a0, 1 #increment string pointer
    lb $t1, 0($a0)
    beq $t1, $zero, exit
    addi $t0, $t0, 1 #increment length counter
    j find_length
exit:
    subi $t0, $t0, 1 #make length n-1
    addi $t3, $t3, 1 #initialize loop counter to 0, run 4 times for astericks
    j loop2

loop2:
    bgt $t3, 4, exit2 

    #generate random number
    la $a1, ($t0)
    li $v0, 42
    syscall

    add $s0, $s2, $a0 #adding random num to register
    lb $s4, ($s0)
    beq $s4, 0x2a, loop2 #check if already astericks, if so then regenerate number
    li $s1, 0x2a #asterick in hex
    sb $s1, ($s0) #replace val with star
    addi $t3, $t3, 1 #increment counter
    j loop2

exit2:
    la $a0, output
    li $v0, 4
    syscall 

    la $a0, ($s0)
    li $v0, 4
    syscall




Aucun commentaire:

Enregistrer un commentaire