我正在使用 ARMv8 进行编码。我几乎完成了我的代码,除了我遇到了问题。当我运行代码时,我收到“分段错误(核心转储)”错误。问题的出现是因为当用 //THIS ONE A 注释的行执行时,它将一个非常大的数字存储到 x24 中,而它应该存储一个介于 0-50 之间的数字。因此,在标记为 //THIS ONE B 和 C 的行中,代码尝试指向 x29 + 2^40 左右的位置,而不是 x29 + (0-50)。
我尝试通过代码查找错误数字存储在 i_s 指针中的位置,但我找不到它。我还尝试了将 B 和 C 行中的 x24 更改为 x21 的代码,它运行得非常好。
最让我困惑的部分是,在代码中出现这个问题之前,我在 testOut 标记之后有几乎相同的代码行。唯一的区别是它在哪里工作,我存储到 x21,而它不工作的地方,它存储到 x24。并且 i_s 所指的值从工作负载到破坏负载没有变化。
注意:有问题的行在代码底部附近
define(SIZE, 50)
define(v_base_r, x19) //stack location of index 0
define(ind_r, x20) //index of array
i_size = 4
j_size = 4
min_size = 4
temp_size = 4
v_size = 50*4
alloc = -(16+i_size+j_size+min_size+temp_size+v_size) & -16
dealloc = -alloc
i_s = 16
j_s = 20
min_s = 24
temp_s = 28
v_s = 32
fmt1: .string "v[%d]: %d\n" //i, v[i]
fmt2: .string "\nSorted array:\n"
fmt3: .string "v[%d]: %d\n" //i, v[i]
.balign 4
.global main
main: stp x29, x30, [sp, alloc]!
mov x29, sp
add v_base_r, x29, v_s
mov ind_r, 0 //initialize index to 0
b inittest
init:
bl rand
and w0, w0, 0xFF
str w0, [v_base_r, ind_r, lsl 2]//stores current rand()&&0xFF into v[ind_r]
adrp x0, fmt1
add x0, x0, :lo12:fmt1
mov x1, ind_r
ldr w2, [v_base_r, ind_r, lsl 2]
bl printf //Printing "v[index]: (value at index)"
add ind_r, ind_r, 1 //repeats for index + 1
inittest:
cmp ind_r, SIZE
b.lt init
mov x21, 0
str x21, [x29, i_s] //initialize i to 0
b testOut
forOut:
str x21, [x29, min_s] //x21 is still holding the value of i from testOut
add x22, x21, 1
str x22, [x29, j_s] //initialize j as j = i+1
b testIn
forIn:
ldr x21, [x29, min_s]
ldr w23, [v_base_r, x22, lsl 2] //x22 still stores value of j from testIn
ldr w24, [v_base_r, x21, lsl 2] //x23 and x24 store values in
//v[j] and v[min], respectively
cmp w23, w24
b.ge keep
str x22, [x29, min_s] //value of j (x22) is stored into min
keep:
add x22, x22, 1 //x22 still stores j, so we can increment
str x22, [x29, j_s] //and then store as new j for next iteration
testIn:
ldr x22, [x29, j_s]
cmp x22, SIZE //j < SIZE
b.lt forIn
ldr x21, [x29, min_s]
**ldr x24, [x29, i_s]** //THIS ONE A
ldr w23, [v_base_r, x21, lsl 2]
str w23, [x29, temp_s] //temp = v[min]
**ldr w23, [v_base_r, x24, lsl 2]** //THIS ONE B
str w23, [v_base_r, x21, lsl 2] //v[min] = v[i]
ldr w23, [x29, temp_s]
**str w23, [v_base_r, x24, lsl 2] //v[i] = temp** //THIS ONE C
add x22, x22, 1 //x22 still stores i, so we can increment
str x22, [x29, i_s] //and then store as new i for next iteration
testOut:
ldr x21, [x29, i_s]
cmp x21, SIZE-1 //i < SIZE-1
b.lt forOut