Under Linux, the assembly language uses the/T syntax, which can be used with the GNU tool, including assembler gas, connector ld, compiler gcc, debugger gdb or kdbg,objdump disassembly function, and a profile gprof. A simple example of the use of each tool in assembly language development is described in brief.
These tools are to be used in the Linux environment, first set up a Linux development environment, you can refer to the article "Windows7 64-bit system installed VMware Centos 64-bit system to build the development environment."
Suppose you have the following simple C program test1.c.
#include <stdio.h>
int Main ()
{
printf ("Hello, world!\n");
Exit (0);
}
1. Introduction to GCC Usage
GCC is used to compile source files, plus parameters to generate intermediate files.
Compile test1.c with gcc to execute file test1:
Use GCC to compile test1.c into file test1-g with debug parameter-G:
Use GCC to generate the TEST1.C assembly language file TEST1-S.S, with the parameter-s:
Use GCC to generate the target code TEST1.O, with the parameter-C:
Question: gcc with adjustable parameters in addition to-G, there are-gstabs,-gstabs+,-ggdb and mode-related parameters, man gcc has been described below. From the description, I still do not understand the specific differences between these parameters and when to use which, if any friend know, please give explanations.
-G produce debugging information in the operating system ' s native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging
Information.
-gstabs
Produce debugging information in stabs format (if-is supported), without GDB extensions. This is the format of used by DBX in most BSD
Systems. On MIPS, Alpha and System V Release 4 Systems This option produces stabs debugging output which are not understood by DBX O R SDB. On
System V Release 4 Systems This option requires the GNU assembler.
-gstabs+
Produce debugging information in stabs format (if-is-supported), using GNU extensions understood only by the GNU Debu Gger (GDB). The use
Of these extensions is likely to make other debuggers crash or refuse to read the program.
-ggdb
Produce debugging information for use by GDB. This means to use the most expressive format available (DWARF 2, stabs, or the native format if
Neither of those is supported), including GDB extensions if at all possible.
Assembly Language Programming This book is the use of-gstabs this parameter. Why not use other parameters?
Finally, the size of the test1 generated by test1.c with these four parameters is recorded as follows, and the resulting file size is not the same as the visible parameter. -G and-ggdb produce the same size as the result.
2. Use of objdump for disassembly
The resulting target file test1.o and executable file test1 can be used Objdump disassembly.
3. Gas compilation usage
Gas assembles the assembler into the target file, the command is as, and finds the following assembler Test1.s:
. section. Text
. globl _start
_start:
MOVL $,%eax
MOVL,%EBX
int $0x80
The following assembly generates TEST1.O files:
4. Use of LD
Connect the front test1.o to the executable file test1.
5. GDB and kdbg usage
GDB uses the command mode, used to like the IDE debugging such as VS, feel gdb is very bad, especially for the more complex project, kdbg easy to use.
From the interface, more convenient operation, do not remember the complex commands, at a glance, you can focus on the software to check the wrong. Note that the compilation or assembly must have debugging parameters, such as Gcc–gstabs or as–gstabs, otherwise it is not adjustable.
KDBG requires KDE and QT support, and in the SECURECRT after logging in to Linux with SSH2, you will be prompted to connect to X server and need to enter the command to run after the graphical interface is started.
Question: How to execute in SECURECRT?
6. Gprof usage of the profile device
The profiler is used to analyze the execution time of all functions in the program, compile with the-PG parameter, but consult the help, as and LD do not support the-PG parameter, so you can only use GCC-PG.
Use the following test2.c as an example:
/*********************************************
* TEST2.C
*********************************************/
#include <stdio.h>
void func1 (void)
{
int I, J;
for (i=0,j=0; i<1000000; i++)
j + +;
}
void Func2 (void)
{
int I, J;
Func1 ();
for (i=0,j=0; i<2000000; i++)
j + +;
}
int Main ()
{
int i;
for (i=0; i<100; i++)
Func1 ();
for (i=0; i<200; i++)
Func2 ();
return 0;
}
First to take the parameter-PG compiler to generate the executable file Test2, and then execute test2, the execution will generate a new Gmon.out file, execute gprof test2 executable can output profile, can be redirected to Test2-gprof.txt for easy viewing. As follows:
The final excerpt from the Test2-gprof.txt file is as follows:
Each sample counts as 0.01 seconds.
% cumulative self to total
Time Seconds seconds calls ms/call ms/call Name
58.24 0.54 0.54 2.68 3.99 Func2
42.86 0.93 0.39 1.31 1.31 func1
Call graph (explanation follows)
for 1.08% of 0.93 seconds
Time Self children called name
<spontaneous>
[1] 100.0 0.00 0.93 main [1]
0.54 0.26 200/200 Func2 [2]
0.13 0.00 100/300 func1 [3]
-----------------------------------------------
0.54 0.26 200/200 main [1]
[2] 85.9 0.54 0.26 Func2 [2]
0.26 0.00 200/300 func1 [3]
-----------------------------------------------
0.13 0.00 100/300 main [1]
0.26 0.00 200/300 Func2 [2]
[3] 42.4 0.39 0.00 func1 [3]
-----------------------------------------------
The time that can be parsed to func1 and Func2 execution.
%time: Represents the percentage of the total elapsed time that the function takes.
Cumulative seconds: Represents the time spent in a row and above.
Self seconds: Represents the time at which the function itself executes.
Calls: The number of times the function was called.
Self Ms/call: The execution time that the function uses for each invocation. Does not contain the time that is performed by other functions that are called in the function.
Total Ms/call: Represents the time that the function is used to execute each call, including when the other function was called.
Name: function name.
Question: Assembler. s how to produce a profile?
Assembly Language Programming reading notes (1)-related tools