所以我试图将冒泡排序程序从装配转换为Y86。我从这个C代码开始,然后将其转换为汇编:
#include <stdio.h>
void bubble(int *, int);
int main(){
int count = 5;
int data[5]= {3, 2, 6, 1, 9,};
bubble(data, count);
return 0;
}
void bubble(int *data, int count){
int i, last;
for(last = count-1; last > 0; last--){
for (i=0; i < last; i++)
if (data[i+1] < data[i]){
/* Swap adjacent elements */
int t = data[i+1];
data[i+1] = data[i];
data[i] = t;
}
}
}
这是我的Y86代码:
init: irmovl Stack, %esp # Set up stack pointer
irmovl Stack, %ebp # Set up base pointer
call main # call main program
halt # Terminate program
main:
pushl %ebp
rrmovl %esp, %ebp
irmovl -16, %ecx
andl %ecx, %esp
irmovl 48, %ecx
subl %ecx, %esp
irmovl 5, %esi
rmmovl %esi, 44(%esp)
irmovl 3, %esi
rmmovl %esi, 24(%esp)
irmovl 2, %esi
rmmovl %esi, 28(%esp)
irmovl 6, %esi
rmmovl %esi, 32(%esp)
irmovl 1, %esi
rmmovl %esi, 36(%esp)
irmovl 9, %esi
rmmovl %esi, 40(%esp)
mrmovl 44(%esp), %eax
rmmovl %eax, 4(%esp)
mrmovl 24(%esp), %ecx
addl %ecx, %eax
rmmovl %eax, (%esp)
call bubble
irmovl 0, %eax
rrmovl %ebp, %esp
popl %ebp
ret
bubble:
pushl %ebp
rrmovl %esp, %ebp
irmovl 16, %esi
subl %esi, %esp
mrmovl 12(%ebp), %eax
irmovl 1, %esi
subl %esi, %eax
rmmovl %eax, -8(%ebp)
jmp L4
L8:
irmovl 0, %ebx
rmmovl %ebx, -12(%ebp)
jmp L5
L7:
mrmovl -12(%ebp), %eax
irmovl 1, %ecx
addl %ecx, %eax
addl %eax, %eax
addl %eax, %eax
addl %eax, %eax
mrmovl 8(%ebp), %esi
addl %esi, %eax
mrmovl (%eax), %edx
mrmovl -12(%ebp), %eax
addl %eax, %eax
addl %eax, %eax
addl %eax, %eax
mrmovl 8(%ebp), %esi
addl %esi, %eax
mrmovl (%eax), %eax
subl %eax, %edx
jge L6
mrmovl -12(%ebp), %eax
irmovl 1, %esi
addl %esi, %eax
addl %eax, %eax
addl %eax, %eax
addl %eax, %eax
mrmovl 8(%ebp), %esi
addl %esi, %eax
mrmovl (%eax), %eax
rmmovl %eax, -4(%ebp)
mrmovl -12(%ebp), %eax
irmovl 1, %esi
addl %esi, %eax
addl %eax, %eax
addl %eax, %eax
addl %eax, %eax
mrmovl 8(%ebp), %esi
addl %esi, %eax
mrmovl -12(%ebp), %edx
addl %edx, %edx
addl %edx, %edx
addl %edx, %edx
mrmovl 8(%ebp), %esi
addl %esi, %eax
mrmovl (%edx), %edx
rmmovl %edx, (%eax)
mrmovl -12(%ebp), %eax
addl %eax, %eax
addl %eax, %eax
addl %eax, %eax
mrmovl 8(%ebp), %esi
addl %esi, %eax
mrmovl -4(%ebp), %edx
rmmovl %edx, (%eax)
L6:
irmovl 1, %esi
mrmovl -12(%ebp), %ebx
addl %esi, %ebx
L5:
mrmovl -12(%ebp), %eax
mrmovl -8(%ebp), %ebx
subl %ebx, %eax
jl L7
irmovl 1, %esi
mrmovl -8(%ebp), %ebx
subl %esi, %ebx
L4:
irmovl 0, %esi #i = 0
mrmovl -8(%ebp), %ebx
subl %esi, %ebx
jg L8
rrmovl %ebp, %esp
popl %ebp
ret
#stack starts here
.pos 0x200
Stack: .long 0
似乎永远不会提升标志旗,我不太清楚为什么会这样。当我运行模拟器时,数组的元素永远不会被排序,它们只是保持在相同的位置。如何解决这个问题的任何提示将非常感谢!