AT & amp; T assembly language-simple example and tool demonstration

Source: Internet
Author: User

Today, let's use the specific instance code to use the usage of the tool mentioned yesterday.

The main purpose of these examples is to familiarize yourself with the usage of assembly-related tools and the application of the assembly program template just mentioned yesterday.

The tools we use mainly include as, ld, gcc, and gdb. Of course, they run in linux.

Let's just give an example. Well, the following example is for reference or from Richard Blum in assembly language programming.

 

Example 1: Print "hello, world! "

#hellowrold.s print "hello,world!".section .dataoutput:.ascii "hello,world\n".section .text.globl _start_start:movl $4, %eaxmovl $1, %ebxmovl $output,%ecxmovl $12,%edxint  $0x80movl $1, %eaxmovl $0, %ebxint  $0x80

The Code is as follows:

First, declare a string in the data segment:

output:.ascii "hello,world\n"

. Asscii Declaration uses ASCII characters to declare a text string. String elements are pre-defined and placed in memory, and their initial memory position is indicated by the tag output.

The following is the script code segment of the Declaration program and the general starting label. _ start is the default starting code of the linker:

.section .text.globl _start_start:
The following code directly calls the write System Call to display text content:

        movl $4, %eaxmovl $1, %ebxmovl $output,%ecxmovl $12,%edxint  $0x80
Parameters called by the write System in Linux:

EAX contains the system call value, and write is 4

EBX contains the file descriptor to be written. in Linux, 0 indicates the standard input, 1 indicates the standard output, and 2 indicates the error output. Here, 1 is passed into EBX, that is, the standard output.

ECX contains the start of a string

EDX contains the length of the string

The following is a system call. 1 indicates that the function is exited and the return value is 0.

        movl $1, %eaxmovl $0, %ebxint  $0x80

The compilation and running results are as follows:


First, explain the compilation parameters,

Step 1: first compile the binary file as -- 32-o hellowrold. o hellowrold. s

As indicates to use as assembler,

-- 32 indicates compiling the target code into a ia-32 code format

-O hellowrold. o indicates that the target file is hellowrold. o (it seems that the wrong file name is Orz)

Hellowrold. s is the source code (originally set to helloworld. s, if it is wrong, it will be wrong)

Step 2: link hellowrold. OTO an executable file.

Ld-m elf_i386-o hellowrold. o

Ld indicates that ld is used to link

-M elf_i386 indicates that a 32 elf-bit elf file is generated.

-O hellowrold indicates that the generated file is hellowrold.

Hellowrold. o is a binary file generated in the first stage.


Try the gdb debugging tool again. The assembler as has an additional parameter-g, which indicates that the debug code is generated. Gdb hellowrold:

The usage of gdb mainly includes: list display code, break setting segment, info register display the values of all registers, print the values of specific variables, and x Display the values of specific memory locations, step: run the code.

Demo:

List, listing code


Break is used to set breakpoints. Here it is set in a specific tag. break can be used to set breakpoints in the following ways:

1. Reach a tag

2. Reach a line number in the source code

3. The data value reaches a specific value.

4. After the number of times the function has been executed


Print prints the corresponding value. The print output format is as follows:

Print/d Output decimal Value

Print/t output binary value

Print/x output hexadecimal value


Info register print all register values


Of course, we only need to change the code entry tag _ start to main to use gcc for compilation.

gcc -m32 -o hellowrold hellowrold.s  


The compilation is successful.

Example 2: The following example calls the c function library in the assembly language.

.section .dataoutput:.ascii "The number is %d\n".section .bss.lcomm buffer,18.section .text.globl _start_start:pushl $520pushl $outputcall  printfaddl  $8,%esppushl $0call  exit
Compile the code using the following method. We can see that there are several more parameters in the ld link.

Let me explain the meanings of the two extra parameters one by one.

We know that in linux, there are two methods to connect a C function to an assembly language program. Static linking in the first method. The static link directly connects the function target code to the executable program file of the application. This will create a huge executable program, and if you run multiple instances of the program at the same time, it will generate an internal waste (each function has its own copy of the same function)

The second method is dynamic link.

In Linux, the standard C dynamic library is located in lib. so. in File x, in my system (ubutnu 14.04), this file is libc. so.6, since I am running in compatible mode, so my system has two of the files, one is 32-bit (/lib/i386-linux-gnu/libc. so.6), there is a 64-bit (/lib/x86_64-linux-gnu/libc. so.6 ). When using gcc, gcc automatically links the C language to this library. We use ld to link libc. so file, must use the-l parameter of the gnu connector, do not specify the full library name. The connector assumes that the libxso file exists at the position it can find. In the base, x is the library name specified by the command line parameter, and we use c.

-lc

Theoretically, we can run it without adding the parameter-dynamic-linker. In fact, the compilation is successful, but it cannot run.

bash: ./print: No such file or directory
Why?
The problem is that the connector can parse the C function, but the function itself is not included in the final executable program. The linker assumes that the program can find the library file during runtime, so there is no error in compiling. But in fact, our program cannot find the library file. To solve this problem, you must specify the program to load the dynamic library when the program is running. For LINUX, this program is linux. so.2. In my system, it is located under/lib. To specify this program, you must use the-dynamic-linker of the gnu linker.

-dynamic-linker
In fact, we can also directly compile with gcc, as long as the _ start label is changed to main, the following method can be compiled:

Gcc-o print. s



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.