The standard assembler of Linux platform is GAS, which is the background assembly tool relied on by GCC, and is usually included in binutils package,
--gstabs tells the assembler to add a symbol table to the generated object code. First, we complete the assembly:
as -gstabs -o hello.o hello.s
The target code generated by assembler must be processed by linker to generate executable code. Linux uses ld as the standard linker, which is also included in binutils package. We then link:
ld -o hello hello.o
With the symbol table, we can debug.
Run it first to see the effect:
./hello hello,world ABCD GDB Do as LINUX An important debugging tool for programmers, which is also applicable to programs written in assembly, we use GDB Perform some simple debugging operations on the above code //First open the hello program: gdb hello GNU gdb (GDB) 7.1-ubuntu Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are 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 was configured as "x86_64-linux-gnu". For bug reporting instructions, please see: <http://×××w.gnu.org/software/gdb/bugs/>... Reading symbols from /home/deepfuture-lx/private/mytest/hello...done. //Then, we can use the list command to list the source code (gdb) list 1 .section .data#Initialized variables 2 output: 3 .ascii "hello,world\n" 4 #The string to print,. Data is the variable of the initialization value. output is the label, indicating the start position of the string, ascii is the data type 5 .section .bss#Uninitialized variable, buffer filled by 0 6 .lcomm num,20 7 #lcomm is the local memory area, that is, it can't be accessed outside the local assembly. . comm is the general memory area. 8 .section .text#Assembly language instruction code 9 .globl _start#Start entry 10 _start: //Use the break command to set breakpoints (gdb) break 17 Breakpoint 1 at 0x4000c6: file hello.s, line 17. //Run to breakpoint (gdb) run Starting program: /home/deepfuture-lx/private/mytest/hello hello,world Breakpoint 1, _start () at hello.s:17 //Continue with next statement 17 movl $0,%eax (gdb) next 18 movl $num,%edi //Display the values of all registers (gdb) info registers rax 0x0 0 rbx 0x1 1 rcx 0x60011c 6291740 rdx 0xc 12 rsi 0x0 0 rdi 0x0 0 rbp 0x0 0x0 rsp 0x7fffffffe2d0 0x7fffffffe2d0 r8 0x0 0 r9 0x0 0 r10 0x0 0 r11 0x0 0 r12 0x0 0 r13 0x0 0 r14 0x0 0 r15 0x0 0 rip 0x4000cb 0x4000cb <_start+27> eflags 0x202 [ IF ] cs 0x33 51 ss 0x2b 43 ds 0x0 0 es 0x0 0 fs 0x0 0 ---Type <return> to continue, or q <return> to quit--- gs 0x0 0 (gdb) next 19 movl $65,1(%edi)#ascii of A //Output the value of edi register in hexadecimal format. /x is hexadecimal, / d is hexadecimal, / t is binary (gdb) print/x $rdi $3 = 0x600128 //Continue operation (gdb) next 20 movl $66,2(%edi)#ascii of B //Display the value of a memory location, x / N Y z, where n is the number of fields, y is the format (c is character, d is decimal, x is hexadecimal), z is the field length (b is byte, n is 16 bit word, w is 32-bit word) (gdb) next 21 movl $67,3(%edi)#ascii of C (gdb) x/3cb &num 0x600128 <num>: 0 '\000' 65 'A' 66 'B' (gdb) next 22 movl $68,4(%edi)#ascii of D (gdb) next 23 movl $10,5(%edi)#ascii of \n (gdb) next 25 movl $4,%eax#The system function called is write (gdb) x/4cb &num 0x600128 <num>: 0 '\000' 65 'A' 66 'B' 67 'C' //Quit gdb (gdb)quit