所以对于我的任务,我应该用汇编程序编写 BubbleSort。我的汇编代码基于这个 Java BubbleSort 循环。出于某种原因,汇编程序一直认为数组 A 和 B 是一个大数组,并试图对整个事物进行排序。一旦用 A 完成排序并用 B 重新开始,我似乎无法让它停止排序。
while (Done == 0)
{
Done = 1; // 1 represents true.
i = 0;
while ( i < N-k)
{
if (A[i] > A[i+1])
{
Help = A[i];
A[i] = A[i+1];
A[i+1] = Help;
Done = 0; // Not sorted...
}
i++;
}
k = k + 1;
}
这是汇编程序代码。格式有点乱,但希望它仍然可读。
Start:
move.l #A, D0
move.l #5, D1
bsr BubbleSort * Sort array A
move.l #0, i
Print1:
move.l #5, D0
move.l i, D5
cmp.l i, D0
beq Start2 **********
move.l #A, A0
move.l i, D0
muls #4, D0
move.l 0(A0, D0.w), D0
jsr WriteInt
addi.l #1, i
bra Print1
Start2:
move.l #str, A0 ************
move.l #5, D0
jsr WriteLn
move.l #B, D0
move.l #10, D1
bsr BubbleSort * Sort array B
move.l #0, i
Print2:
move.l #10, D0
cmp.l i, D0
beq Stop
move.l #B, A0
move.l i, D0
muls #4, D0
move.l 0(A0, D0.w), D0
jsr WriteInt
addi.l #1, i
bra Print2
*D0 = address of int array to be sorted
*D1 = N
BubbleSort:
movea.l #A, A0
move.l #0, i
move.l #0, D *Done is 0
move.l D, D2 *pass Done to D2
move.l D1, N *N is number of elements
move.l #0, k
WhileStart:
cmp.l #0, D2 * compare if D2 == 0
BNE WhileEnd *if not 0, go to WhileEnd
move.l #1, D2 * D2=1
move.l #0, i * i = 0
move.l i, D3 *pass i to D3
move.l k, D4 * pass k to D4
move.l N, D7 * pass N to D7
sub.l D4,D7 * D7 = N-k
WhileStart2:
cmp.l D7, D3 *Compare i with N-k
BGE WhileEnd2
move.l i, D3
muls #4 D3
move.l 0(A0, D3.w), D5 *D5 = A[i]
move.l i, D4
add.l #1, D4 *i+1
muls #4, D4
move.l 0(A0, D4.w), D6 *D6 = A[i+1]
IfStart:
cmp.l D6, D5 *compare A[i] with A[i+1]
BLE IfEnd
move.l D5, 5000 * pass A[i] to memory location 5000
move.l D6, 0(A0, D3.w) *A[i] = A[i+1]
move.l 5000, 0(A0, D4.w) *A[i+1] = whatever was at location 5000 (old A[i])
move.l #0, D2 * D2=0 again
move.l i, D3 * passing i to D3
bra IfEnd *End of If loop
IfEnd:
move.l i, D3 *i++
add.l #1, D3
move.l D3, i
bra WhileStart2 *Go back and compare i with N-k
WhileEnd2:
move.l k, D4 *k = k + 1
add.l #1, D4
move.l D4, k
bra WhileStart * go back
WhileEnd:
rts *** I have added a rts to make sure your function returns....