嗨,我正在尝试在 linux 上的 nasm 中实现一个合并排序算法,但是我遇到了分段错误,所以我在 konsole 中输入了这个“gdb mergesort core”,我得到了这个
"El núcleo se generó por «./mergesort».
El programa terminó con la señal 11, Segmentation fault.
#0 0x080481a5 in ?? ()
(gdb) bt
#0 0x080481a5 in ?? ()
#1 0x080481b1 in ?? ()
#2 0x080480af in ?? ()"
但我真的不明白如何处理它或发生分段错误的位置。有人可以帮我吗?
对不起,这是我第一次在这里粘贴代码,所以我不知道如何按应有的方式缩进所有内容
NASM 中的代码如下:
BITS 32
extern printf
section .data
section .text
global _start
global main,
main:
_start:
nop;
mov edi, sorted
mov esi, array
mov ecx, 10
rep movsd
push 10
push 0
push sorted
call mergesort
add esp, 12
push sorted
push 10
call print
add esp, 8
ret
merge:
push ebp
mov ebp, esp
push eax
push ecx
push edx
push edi
push esi
mov ecx, [ebp+20]
sub ecx, [ebp+12]
shl DWORD[ebp+12], 2
shl DWORD[ebp+16], 2
shl DWORD[ebp+20], 2
mov edx, temp
mov edi, [ebp+8]
add edi, [ebp+16]
mov esi, [ebp+8]
add [ebp+12], esi
add [ebp+16], esi
add [ebp+20], esi
mov esi, [ebp+12]
.next:
cmp esi, [ebp+16]
jnz .second
cmp edi, [ebp+20]
jnz .first
jmp .end
.first:
cmp esi, [ebp+16]
jnz .second
mov eax, [edi]
mov [edx], eax
add edx, 4
add edi, 4
jmp .next
.second:
cmp edi, [ebp+20]
jnz .third
mov eax, [esi]
mov [edx], eax
add edx, 4
add esi, 4
jmp .next
.third:
mov eax, [esi]
cmp eax, [edi]
jnl .forth
mov [edx], eax
add edx, 4
add esi, 4
jmp .next
.forth:
mov eax, [edi]
mov [edx], eax
add edi, 4
add edx, 4
jmp .next
.end:
mov esi, temp
mov edi, [ebp+12]
rep movsd
pop esi
pop edi
pop edx
pop ecx
pop eax
mov esp, ebp
pop ebp
ret
mergesort:
push ebp
mov ebp, esp
push eax
mov eax, DWORD[ebp+16]
sub eax, [ebp+12]
cmp eax, 2
JL .end
push edx
push ebx
xor edx, edx
mov ebx, 2
div ebx
pop ebx
pop edx
add eax, [ebp+12]
push eax
push DWORD[ebp+12]
push DWORD[ebp+8]
call mergesort
add esp, 12
push DWORD[ebp+16]
push eax
push DWORD[ebp+8]
call mergesort
add esp, 12
push DWORD[ebp+16]
push eax
push DWORD[ebp+12]
push DWORD[ebp+8]
call merge
add esp, 16
.end:
pop eax
mov esp, ebp
pop ebp
ret
print:
push ebp
mov ebp, esp
push ecx
push edx
mov edx, [ebp+12]
mov ecx, [ebp+8]
.args:
push ecx
push edx
push DWORD[edx]
push format
call print
add esp, 8
pop edx
pop ecx
add edx, 4
LOOP .args
push endl
call print
add esp, 4
pop edx
pop ecx
mov esp, ebp
pop ebp
ret
format:
db ' %2d', 0
endl:
db 10, 0
array:
dd 10, 9, 8, 4, 5, 6, 7, 3, 2, 1
section .bss
sorted:
resd 10
temp:
resd 10