您好我正在尝试在linux上的nasm中实现mergesort算法,但是我遇到了分段错误,因此我输入了konsole这个“gdb mergesort核心”,我得到了这个
"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
答案 0 :(得分:0)
首先,为什么要在code
部分定义数据?
这样:
format:
db ' %2d', 0
endl:
db 10, 0
array:
dd 10, 9, 8, 4, 5, 6, 7, 3, 2, 1
应该在.data
部分。
通过将-F stabs
添加到NASM的命令行(在Linux中)将调试符号添加到目标文件中我不确定Windows的选项。
nasm -f elf -F stabs $(APP).asm
这将在gdb backtrace中显示您的函数名称。
问题似乎出现在print
函数中,请仔细查看 !你递归地调用它。
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
我认为您的意思是致电printf
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 printf
add esp, 8
pop edx
pop ecx
add edx, 4
LOOP .args
push endl
call printf
add esp, 4
pop edx
pop ecx
mov esp, ebp
pop ebp
ret
看到区别?修复该问题,它似乎正确打印出来!