问题
Using GDB to debug assembly programs produced by NASM/MinGW doesn't stop at breakpoints?
Assembling and linking the program below produce unexpected results when debugging using GDB.
nasm -g -f win32 insertion_sort_static.asm
ld insertion_sort_static.obj -o test
Now, when I run GDB and set a breakpoint at _exit
, the program doesn't break at the specified point. Instead the program terminate.
gdb test
(gdb) break exit
Breakpoint 1 at 0x401034
(gdb) run
Starting program: C:\Users\nze\Desktop\asm\sorting\insertion_sort/test
[New Thread 6548.0x2c9c]
[Inferior 1 (process 6548) exited with code 01]
(gdb)
On the other hand, if I set break start
, the program break three instructions below _start
:
(gdb) break start
Breakpoint 2 at 0x401003
(gdb) run
Starting program: C:\Users\nze\Desktop\asm\sorting\insertion_sort/test
[New Thread 4520.0x28e8]
Breakpoint 2, 0x00401003 in start ()
(gdb) disassemble
Dump of assembler code for function start:
0x00401000 <+0>: push %ebp
0x00401001 <+1>: mov %esp,%ebp
=> 0x00401003 <+3>: xor %ecx,%ecx
What is the cause of these troubles? It is really bothering me, and I can't seem to find a solution.
;; insertion_sort(int[] num, int len)
BITS 32
section .data
_array: dd 4, 2, 8, 6, 1
_len: equ ($ - _array) / 4
section .text
global _start
_start:
push ebp
mov ebp, esp
xor ecx, ecx
_outer:
inc ecx
cmp ecx, _len
jge _exit
mov ebx, ecx
dec ebx
lea esi, [_array + ecx * 4]
lea edi, [_array + ebx * 4]
_inner:
cmp ebx, 0
jl _outer
mov eax, [edi]
cmp eax, [esi]
jle _outer
xchg eax, dword [esi] ; swap [esi] and [edi]
mov dword [edi], eax
sub esi, 4
sub edi, 4
dec ebx
jmp _inner
_exit:
mov esp, ebp
pop ebp
ret
来源:https://stackoverflow.com/questions/38773556/using-gdb-to-debug-assembly-programs-produced-by-nasm-mingw-doesnt-stop-at-brea