这是一个非常简单的循环,尽管在第七次重复中(当一般数量大于7时,系统调用开始出现行为错误并且它不打印“enter_num”标记,也就是其在数据段上的破碎值)这点。 我不明白原因..
.data
.asciiz
Amount:"Enter amount of numbers "
enter_num:"Enter number "
of:" of "
space:" "
.text
la $a1 0x10010000 #Loads address to $a1
#Ask for general number amount
la $a0 Amount
li $v0 4
syscall
li $v0 5
syscall
add $t1 $t1 $v0 #store the general amount of numbers in $t1
#Define counter $t2
li $t2 1
#store first general number
sb $t1 0($a1)#store byte in address
addi $a1 $a1 1 #Promote $a2 block address by 1 step
loop:
#Ask for array numbers
la $a0 enter_num
li $v0 4
syscall
la $a0,($t2)
li $v0 1
syscall
la $a0 of
li $v0 4
syscall
la $a0,($t1)
li $v0 1
syscall
la $a0 space
li $v0 4
syscall
li $v0 5
syscall
sb $v0 0($a1)#store byte in address
addi $a1 $a1 1#Promote $a1 address by 1 step
beq $t2 $t1 finish #loop ends when we reach the general amount number
addi $t2 $t2 1#promote $t2 counter
j loop
答案 0 :(得分:0)
$a1
指向0x10010000
,它是数据段的基础(存储ASCII字符串的位置)。通过在那里写入数据,您可以有效地覆盖字符串。
试试这个:选择30个数字,输入0到数字#24,然后输入数字#25为65,数字#26为66,等等......你应该得到这样的数字:
Enter number 1 of 30 0
...
Enter number 24 of 30 0
Enter number 25 of 30 65
Anter number 26 of 30 66
ABter number 27 of 30 67
ABCer number 28 of 30 68
ABCDr number 29 of 30 69
ABCDE number 30 of 30 70
65是' A'的ASCII代码,' B'等等...您可以看到您的数据被覆盖。
一种解决方案是在space
之后立即存储您的号码。这适用于spim:
.data
Amount: .asciiz "Enter amount of numbers "
enter_num: .asciiz "Enter number "
of: .asciiz " of "
space: .asciiz " "
numbers: .space 128 # <- some room for numbers here
.text
.globl main
main:
#la $a1 0x10010000 # <- replace this...
la $a1 numbers # <- ...with this
# ...rest of the code here...