assembly - Merge sort segmentation Fault NASM -
hi trying implement mergesort algorithm in nasm on linux, getting segmentation fault, typed in konsole "gdb mergesort core" , got
"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 ?? ()"
but don`t understand or segmentation fault taking place. me please?
im sorry first time pasting code here don`t know how indent way should
the code in nasm following:
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
first, why have data defined in code
section? this:
format: db ' %2d', 0 endl: db 10, 0 array: dd 10, 9, 8, 4, 5, 6, 7, 3, 2, 1
should in .data
section.
add debug symbols object file adding -f stabs
command line nasm (in linux is) unsure of option windows. nasm -f elf -f stabs $(app).asm
this show function names in gdb backtrace.
the problem seems in print
function, @ carefully! recursively calling it.
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
i think mean call 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
see difference? fix issue , seems print out correctly!
Comments
Post a Comment