"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet, the article only for learning Exchange, do not use for commercial purposes"
Just like in C, there are some errors in writing all the language programs, and we can use the debugger to run the program step-by-stage to monitor how the data is handled when an error occurs. This section uses the GNU debugger to check the previous Hello program to monitor changes in the value of registers and memory during processing. To debug an assembly language program, you need to recompile the source code with the-gstabs parameter at compile time, and the executable file compiled with this parameter is slightly larger than before because additional information is added. The previous section of the program does not use the-gstabs parameter assembly to generate the file as follows:$ ls-lh Hello-rwxrwxr-x. 1 allen Allen 628 April 15:28 Helloafter using-gstabs, the compiled files are generated as follows:$ ls-lh Hello-rwxrwxr-x. 1 Allen Allen 888 April 15:29 Hellosince the GNU debugging ignores the start breakpoint, a null instruction NOP is required at the Start tab.
when the program contains the necessary debugging information, we can run it in GDB:$gdb HelloGNU gdb (gdb) Red Hat Enterprise Linux (7.2-60.EL6)
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I686-redhat-linux-gnu".
For bugs reporting instructions, please see:
Reading symbols From/home/allen/as/1_hello/hello...done.
(GDB)
in GDB, use the command break (b) to specify a breakpoint: (GDB) break *_start
Note:breakpoint 1 also set at PC 0x8048074.
Breakpoint 2 at 0x8048074:file Hello.s.
(GDB)
Use the command Run (R) to start the program:(GDB) RunStarting program:/home/allen/as/1_hello/helloBreakpoint 1, _start () at Hello.s:12movl $len,%edx(GDB)
you can see that the program starts, stays at the breakpoint we set earlier, and lets the program step through using the next (n) and step (s) commands:(gdb) Stepmovl $msg,%ecx(gdb) NextMOVL $,%ebx(GDB) smovl $4,%eax(GDB) n$0x80 int(GDB)
You can view the value of the check data element in a single-step pilot run. The most commonly checked elements are registers and memory locations, and the Info Register command allows you to display values for all registers, use the Print command to display the value of the specified register or variable, and use the X command to display the contents of the specified memory location. (GDB) Info registerseax 0x4 4ecx 0x8049098 134516888edx 0xdebx 0x1 1ESP 0xbffff3d0 0xbffff3d0EBP 0x0-0x0ESI 0x0 0EDI 0x0 0eip 0x8048088 0x8048088 <_start+20>eflags 0x212 [AF IF]CS 0x73SS 0x7b 123DS 0x7b 123ES 0x7b 123FS 0x0 0GS 0x0 0(GDB)the value of the register can also be displayed using the command print plus the specified register:(gdb) print $eax$ = 4(gdb) print $ebx$4 = 1(gdb) print $ecx$ $ = 134516888(gdb) print $edx$6 =(GDB)The Print command plus modifier can change the PRITN command output format:print/d Display decimal valuesprint/t Display binary valuesprint/x Displays the hexadecimal value(gdb) print/x $edx$9 = 0xd
The x command can also modify the output with modifiers in the following format:X/nyzN is the number of fields to display;y is the output format, which can be C (character), D (Decimal), x (hexadecimal);Z is the length of the field to be displayed, which can be B (bytes), H (16-bit word), W (32-bit word). (gdb) X/13CB &msg0x8049098 <msg>: 104 ' h ' 101 ' E ' 108 ' l ' 108 ' l ' 111 ' o ' + ' "119 ' W ' 111 ' O '0x80490a0 <msg+8>: ' R ' 108 ' l ' d ' 33 '! ' Ten ' \ n '(GDB)where MSG before the & indicates that it is a memory location.
The command cont allows the program to continue in its normal way, and runs directly to the end if there are no breakpoints behind it. (GDB) contcontinuing.Program exited normally.(GDB)
Finally, you can use the Quit command to exit GDB debugging. (GDB) Quit$
Linux Platform x86 compilation (V): Debugging Assembler with GDB