简单循环中的选择性系统调用和值粉碎

Selective Syscall and values crushings in a simple loop

这是一个非常简单的循环,尽管在第七次重复中(当总数量大于 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


$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 码,66 是 'B' 的 ASCII 码,等等...您可以看到您的数据正在被覆盖。

一种解决方案是在 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...