MIPS - 系统调用打印错误的 asciiz

MIPS - syscall prints the wrong asciiz

我正在编写一个程序,按升序从用户那里读取 4 个整数到一个数组。如果值没有升序,程序会打印一条错误消息(.data 中的“更大”标签)并从用户那里读取另一个整数。 读取整数后,我迭代并在每个整数后用逗号打印它们。

当我调用逗号打印时,我得到一个 space + “更大”标签的值(错误消息)而不是逗号。 代码:

.data
array:  .byte   4
comma:  .asciiz ",\n"
bigger: .asciiz "The array inputs should be in an ascending order!\n"

.text
main:   # main program entry    
la $a0, array       # load array address
jal get_array       # call get_array procedure with the array address
addi $t0, $zero, 0  # set loop counter to 0
la   $t1, array     # load array address
loop: beq $t0, 4, endLoop # for $t0 < 4
      li $v0, 1     # v0 integer syscall
      lb $a0, 0($t1)    # load current byte of the array    
      syscall       # print it
      la $a0, comma # load address of comma string
      li $v0, 4     # v0 string syscall    
      syscall       # print comma
      addi $t1, $t1, 1  # array address pointer++
      addi $t0, $t0, 1  # loop counter++
      j loop        # loop
endLoop:
      li $v0, 10    # exit program
      syscall

get_array:          # read asceding integer array from the user
    addi $t0, $zero, 1  # inserts count
    la   $t1, ($a0)     # array address pointer
    addi $v0, $zero, 5  # read integer syscall
    syscall         # read integer from the user
    sb   $v0, 0($t1)    # save integer in current array address pointer
    addi $t1, $t1, 1    # array address pointer++
loop1:  beq  $t0, 4, endLoop1   # for $t0 < 4
    addi $v0, $zero, 5  # read integer syscall
    syscall         # read integer from the user
    lb   $t2, -1($t1)   # read the previous array value
    slt  $t3, $t2, $v0  # check if current value is bigger than previous value
    beq  $t3, 0, invalid    # if not go to invalid
valid:  sb   $v0, 0($t1)    # valid - save new value in current array address pointer
    addi $t1, $t1, 1    # array address pointer++
    addi $t0, $t0, 1    # inserts count++
    j    loop1      # loop
invalid:            # invalid - new value doesnt keep array ascending
    addi $v0, $zero, 4  # print string syscall 
    la $a0, bigger      # print error message
    syscall         # print error message       
    j    loop1      # loop
endLoop1:
    jr $ra          # return to caller

示例: 对于输入:

1
2
3
4

印刷品是:

1  The array inputs should be in an ascending order!
2  The array inputs should be in an ascending order!
3  The array inputs should be in an ascending order!
4  The array inputs should be in an ascending order!

预期结果:

1,
2,
3,
4

array: .byte 4 不保留 space 4 个字节;它为初始值为 4 的单个字节保留 space。

你想要的是.space,即:array: .space 4


现在最终发生的事情是您的最后 3 个输入覆盖了 comma 字符串,包括它的 NUL 终止符。