Domanda

I have the following code in Assembly:

.data
lab_n:     .word 0x50, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x61, 0x6e, 0x64, 0x6f, 0x20, 0x6f, 0x20, 0x4d, 0x49, 0x50, 0x53, 0x0d
count:     .word 19
msgtexto:  .asciiz "SMP/AC "
separator: .asciiz "->"

.text

main:
        la  $a0,msgtexto    # $a0 <- msgtexto
        jal print_string    # print it
        la  $a0,count
        lw  $t0,0($a0)

        li  $a0,0x0A        # $a0 <- '\n'
        jal print_char      # print it

        li  $t1,0           # to be used as a counter
        la  $a1, lab_n
loop:   

        move $a0,$t2
        jal  print_char     # print corresponding char      

        lw    $t2, 0($a1)   
        move  $a0,$t2       # $a0 <- lab_n[i]  , i.e. offset=4*i

        addi  $t1,$t1,1     # decrement counter
        addi  $a1,$a1,4
        blt   $t1,$t0,loop

finish:
        li      $v0, 10     # Exit the program
        syscall



# funções com chamadas ao sistema para imprimir inteiros, caracteres e strings
# (o argumento de entrada é colocado em $a0 antes da chamada da função)

# print_int(int), $4 = $a0 = integer
print_int:
        li $v0,1
        syscall
        jr $ra

# print_char(char), $4 = $a0 = char
print_char:
        li $v0,11
        syscall
        jr $ra


# print_string(char *), $4 = $a0 = string
print_string:
        li $v0,4  
        syscall
        jr $ra

The final result of this code is supposed to be the following: SMP/AC Programando o MIPS

However, I'm getting a little square behind the word Progamando.

My teacher told me that this is happening because the ASCII code of the letters is not in the correct order. The "P" is in "r" place, the "r" is in the "o" place and so on and so on.

I get what he said, but I can't find the bug in this program. Can someone help me?

È stato utile?

Soluzione

On the first iteration of the loop you're printing whatever happened to be in $t2 before you entered the loop:

move    $a0,$t2         # <- $t2 hasn't been loaded with a value from lab_n yet
jal     print_char      # ..but you still print it

lw  $t2, 0($a1)         # ..and here you finally load a value from lab_n
move    $a0,$t2         

Those lines ought to be changed to:

lw  $a0, 0($a1)         # load character
jal print_char          # print it

Also, the comment on this line is incorrect:

addi    $t1,$t1,1   # decrement counter

You're incrementing the counter.


And if the output really is supposed to be "SMP/AC Programando o MIPS" (i.e. a single line) you shouldn't output a linefeed character before the loop.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top