如何解决MIPS-syscall打印错误的asciiz
我正在编写一个程序,该程序以升序从用户读取4个整数到数组。如果值不升序,则程序将显示错误消息(.data中的“更大”标签),并从用户读取另一个整数。 读取整数后,我对每个整数进行迭代并用逗号打印。
当我调用逗号打印时,我得到一个空格+“更大”标签(错误消息)的值而不是逗号。 代码:
.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,1 # inserts count
la $t1,($a0) # array address pointer
addi $v0,5 # read integer syscall
syscall # read integer from the user
sb $v0,0($t1) # save integer in current array address pointer
addi $t1,1 # array address pointer++
loop1: beq $t0,endLoop1 # for $t0 < 4
addi $v0,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,invalid # if not go to invalid
valid: sb $v0,0($t1) # valid - save new value in current array address pointer
addi $t1,1 # array address pointer++
addi $t0,1 # inserts count++
j loop1 # loop
invalid: # invalid - new value doesnt keep array ascending
addi $v0,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
不会为4个字节保留空间;它为初始值为4的单个字节保留空间。
您想要的是.space
,即array: .space 4
现在最终发生的是,您输入的最后3个覆盖了comma
字符串,包括其NUL终止符。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。