MIPS ASCII代码的顺序不正确

时间:2014-03-18 11:14:41

标签: assembly ascii mips

我在汇编中有以下代码:

.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

此代码的最终结果应该是以下内容: SMP / AC Programando o MIPS

然而,我在Progamando这个词后面有一个小广场。

我的老师告诉我,这种情况正在发生,因为字母的ASCII码不正确。 &#34; P&#34;在&#34; r&#34;地方,&#34; r&#34;是在&#34; o&#34;地点等等。

我得到他说的话,但是我无法在这个程序中找到错误。有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

在循环的第一次迭代中,您在进入循环之前打印$t2中发生的任何事情:

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         

这些行应该改为:

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

此外,此行的评论不正确:

addi    $t1,$t1,1   # decrement counter

递增计数器。


如果输出真的应该是&#34; SMP / AC Programando o MIPS&#34; (即单行)你不应该在循环之前输出换行符。