0
好回來後,我想實現在ARM7大會選擇排序算法。它從數組的最後一個元素開始,一直到第一個元素。它適用於第一次交換,但在繼續之前退出。我和我的教授交談,他說要使用堆棧變量,以便lr不會被覆蓋等,但我仍然不明白該怎麼做。我找到了這樣做的方法,但似乎找不到任何我知道如何在我的代碼中實現的東西。從ARM7嵌套子程序
我的代碼如下:(A1是字符指針長度A2的陣列兩者都是通過C傳遞。)
sort2:
stmdb sp!, {v1-v8, lr} @ Copy registers to stack
mov v1, a2 @store number of elements in v1,v2 (a2 is passed in via C)
mov v2, a2
sub v1, v1,#1 @subtract 1 from a2 to get number last element in array
sub v2, v1,#1 @subtract 1 from v1 to get element just below last element
bl loop2
ldmia sp!, {v1-v8, pc}
loop2:
ldrb v4, [a1,v1] @store value of last element in array a1(passed in via C) into r4
ldrb v5, [a1,v2] @store value of next to last element in array a1(passed in via C) into r4
cmp v4, v5
blt swap2 @if last element is less than current element a[v2], swap them
cmp v2, #0 @if v2 = 0, we cycled through one iteration
subeq v1, v1,#1 @so now move on to a[v1-1] element in array
moveq v2, v1 @if v2 = 0, move the value of v1 into v2
sub v2, v2,#1 @subtract 1 from v2 to get element below a[v1]
cmp v1, #0 @if v1 = 0, were done
bne loop2
mov pc, lr @return to sort2
swap2:
mov v8, lr @store return address in v8
ldrb v6, [a1,v2] @store value of a[v2] into v6
strb v4, [a1,v2] @store v4 into a[v2]
strb v6, [a1,v1] @store value of v6 into a[v1]
mov lr, v8 @falls through and exits here instead of returning to loop2
所以我需要的'blt'調用之前'STMDB SP !, {V1-V8,LR}'加入到'loop2'(和開關'BLT到bllt')?然後在'swap2'結尾有'mov pc,lr'? – vol4life27