对于一个大学。任务我们要在C和汇编中实现一个mergesort程序并比较两者,现在C实现很好,但是我对汇编部分有一些问题。
汇编代码在第61行导致分段错误
movl %ecx, (%edx, %eax, 4) # tmp[i] = arr[n]
但我看不出为什么会导致它崩溃或如何修复它的原因。
asm.S(汇编,x86,AT& T语法)
#include "asmdef.h"
DECLARE_GLOBAL(mergesort)
FUNC (mergesort):
pushl %ebp # Saving caller's EBP
movl %esp, %ebp # Creating new stack frame
movl 24(%ebp), %eax
#movl
pushl %ebx # EBX register
pushl %esi # ESI register is used as j
pushl %edi # EDI register is used as n
# EAX register is used as i
# ECX register is used as mid
# EDX register is used as max
movl 8(%ebp), %ebx # EBX = arr[]
movl 12(%ebp), %esi # ESI(j) = min
movl 16(%ebp), %edi # EDI(n) = mid
addl $1, %edi # EDI(n) += 1
movl 16(%ebp), %ecx # ECX = mid
movl 20(%ebp), %edx # EDX = max
movl %esi, %eax # i = min
first_loop:
first_if:
cmpl %ecx, %esi # compare j with mid
jg second_if # jump if j bigger than mid
second_if:
cmpl %edx, %edi # Compares max with n
jg end_of_first_loop # Jumps if n bigger than max
third_if:
pushl %ecx # Pushing mid on stack meanwhile
pushl %edx # Pushing max on stack meanwhile
movl (%ebx, %esi, 4), %ecx # ECX = arr[j]
cmpl (%ebx, %edi, 4), %ecx # Compare arr[j] with arr[n]
jg first_else # Jump to first else
# Using EDX as tmp[]
movl %ecx, (%edx, %eax, 4) # tmp[i] = arr[j]
incl %esi # Increment j with 1
popl %edx # EDX = max
popl %ecx # ECX = mid
incl %eax # Increment i with 1
jmp first_loop
first_else:
pushl %edx # Pushing max on stack
pushl %ecx # Pushing mid on stack
movl (%ebx, %edi, 4), %ecx # ECX = arr[n]
movl %ecx, (%edx, %eax, 4) # tmp[i] = arr[n]
incl %edi # Increment n with 1
popl %ecx # ECX = mid
popl %edx # EDX = max
incl %eax # Increment i with 1
jmp first_loop
end_of_first_loop:
jmp fourth_if
fourth_if:
cmpl %ecx, %esi # Compares mid and j
jg second_loop # Jumps to else if j is not bigger
jmp second_else
second_loop:
pushl %esi # Pushing j on stack
pushl %ecx # Pushing mid on stack
#ESI = tmp meanwhile
#ECX = k meanwhile
movl %edi, %ecx # m = n
fifth_if:
cmpl %edx, %ecx # compares n <= max
jg end_1
pushl %edx # push max to stack
movl (%ebx, %ecx, 4), %edx # EDX = arr[n]
movl %edx, (%esi, %eax, 4) # tmp[i] = arr[n]
popl %edx # EDX = max
incl %eax # Increments i with 1
incl %ecx # Increments n with 1
jmp fifth_if
end_1:
popl %ecx # ECX = mid
popl %esi # ESI = j
jmp last_loop
second_else:
third_loop:
pushl %edi # Pushing n to stack
pushl %edx # Pushing max to stack
movl %esi, %edi # n = j
sixth_if:
cmpl %ecx, %esi # if n > mid jump
jg end_2
pushl %ecx # Pushing mid to stack
movl (%ebx, %edi, 4), %ecx # ECX = arr[n]
movl %ecx, (%edx, %eax, 4) # tmp[i] = arr[n]
popl %ecx # ECX = mid
incl %eax # Increments i with 1
incl %edi # Increments n with 1
jmp sixth_if
end_2:
popl % edx # EDX = max
popl %edi # EDI = n
jmp last_loop
last_loop:
pushl %esi # push j to stack
pushl %eax # push i to stack
movl 12(%ebp), %esi # n = min
seventh_if:
cmpl %edx, %esi # if n > max jump
jg end_3
pushl %edx # push max to stack
movl (%edx, %esi, 4), %eax # EAX = tmp[n]
movl %eax, (%ebx, %esi, 4) # arr[n] = tmp[n]
popl %edx # EDX = max
incl %esi # Increment n with 1
jmp seventh_if
end_3:
popl %eax # EAX = i
popl %esi # ESI = j
jmp end_final
end_final:
popl %edi
popl %esi
popl %ebx
popl %ebp
ret
amsdef.h
#ifdef __CYGWIN__
#define FUNC(x) _##x
#define DECLARE_GLOBAL(x) .globl FUNC(x);\
.def FUNC(x);\
.scl 2;\
.type 32;\
.endef
#else
#define FUNC(x) x
#define DECLARE_GLOBAL(x) .globl FUNC(x);\
.type FUNC(x), @function
#endif
main.c(相关部分)
#include <stdio.h>
#include <stdlib.h>
extern void mergesort(int arr[], int min, int mid, int max, int size);
void _part(int arr[], int min, int max, int size) {
int mid;
if(min < max) {
mid = (min + max) / 2;
_part(arr, min, mid, size);
_part(arr, mid + 1, max, size);
mergesort(arr, min, mid, max, size);
}
}
int main() {
int arrsize = 150;
int arr[arrsize];
int i, j;
for(i = 0 ; i < arrsize ; i++) {
j = rand()%1000;
arr[i] = j;
}
_part(arr, 0, arrsize - 1, arrsize);
printf("\n\t merge sorting completed \n");
for(i = 0 ; i < arrsize ; i++) {
printf("%d ", arr[i]);
}
return 0;
}