0

I have a little problem with my MIPS code, because this not work with "a", I explain better... If I write:

  1. Heeeeello world --> I obtain this output "The 'e' has the biggest number of occurrences, 5"
  2. hi Maaaaaark --> I obtain this output "The '' has the biggest number of occurrences, "

Can someone give me a solution? this is my code:

.data
m1: .asciiz "Give me a string: "
m2: .asciiz "\nThe letter '"
m3: .asciiz "' has the biggest number of occurrences, is present "
m4: .asciiz " times."
     .align 2
myArray: .space 104 

.text
.globl main

main:

li $v0, 4
la $a0, m1
syscall

addi $sp, $sp, -256

move $s0, $sp

move $a0, $sp
li $a1, 255
li $v0, 8
syscall


move $a0, $s0

jal check_case



move $a0, $v0
move $a1, $v1

jal analizza_stringa


move $a0, $v0
move $a1, $v1

jal stampa_risultato

end_program:

li $v0, 10
syscall



check_case:

move $t0, $a0

while: 

    lb $t1, ($t0)

    beq $t1, 32, ignore_value_and_continue


        beq $t1, 10, exit_check_case

        bge $t1, 97, continue_check_case

        bge $t1, 65, change_case

            change_case:

            addi $t2, $t1, 32

            sb $t2, ($t0) 

        ignore_value_and_continue:

            continue_check_case:

            addi $t2, $t2, 1

            addi $t0, $t0, 1

            j while

exit_check_case:

    move $v0, $t0

    move $v1, $t2

    jr $ra


analizza_stringa:

li $t1, 0
li $t2, 0
li $t3, 0
li $t4, 0

move $t7, $a0

sub $a0, $a0, $v1 
sub $t7, $t7, $v1

while_string:

lb $t0, ($a0)

beq $t0, $zero, check_best 

    beq $t1, 32, ignore_value_and_continue2

    ignore_value_and_continue2:

    beq $t0, 10, check_best

        subi $t3, $t0, 97

        mul $t4, $t3, 4

        move $t6, $t7

    while_occorrenza:

         beq $t1, 10, continue

         lb $t1,($t6) 

         bne $t1, $t0, continue2 

         addi $t5, $t5, 1

         continue2: 

         addi $t6, $t6, 1

         j while_occorrenza

    continue:

    sw $t5, myArray($t4)

    li $t5, 0
    li $t3, 0
    li $t1, 0   

addi $a0, $a0, 1

j while_string

check_best:

li $t0, 0

lw $t1, myArray($t0)

addi $t0, $t0, 4

while_check_best:

    beq $t0, 104, exit_check

    addi $t5, $t5, 1

    lw $t4, myArray($t0)        

    bge $t4, $t1, scambia

    j continue_check

    scambia:

        blt $t4, $t2, continue_check  

        move $t2, $t4

        add $t3, $t5, 97

    continue_check:

    addi $t0, $t0, 4

    j while_check_best

exit_check:

    move $v0, $t3

    move $v1, $t2

    jr $ra


stampa_risultato:

move $t0, $a0

li $v0, 4
la $a0, m2
syscall


li $v0, 11
move $a0, $t0
syscall


li $v0, 4
la $a0, m3
syscall


li $v0, 1
move $a0, $a1 
syscall

li $v0, 4
la $a0, m4
syscall


jr $ra
Marco
  • 49
  • 1
  • 11
  • This looks overly complicated to me. Declare an array with 256 entries and initalize them all to 0. For each byte in the string do `occurrences[string[i]] += 1`. Then sort the array to get the most common character. – Michael Feb 12 '17 at 14:18
  • Hi Michael, thank you for the answer... I wouldn't change all my code, this work with every letter, but not with the "a". On the register the occurrences for the "a" letter is present, but is not printed on screen... – Marco Feb 12 '17 at 14:23
  • Then use the simulator's debugger to set a breakpoint right before you print the values, and inspect the contents of the registers. And work your way backwards from there until you find the problem. – Michael Feb 12 '17 at 14:28

0 Answers0