Assembly Language Programming reading notes (2)-related tools 64-bit system chapter

Source: Internet
Author: User
Tags exit in

Assembly language programming, the 32-bit system should not have any problems, but under the 64-bit system, there will be some different places. Some program samples also compile errors or execute errors.

The system used by the blogger is CentOS v6.4 x64. This article mainly solves the 32-bit assembler how to assemble and connect in 64-bit environment, but does not discuss how to design 64-bit assembly language.

1.64-bit system compiled 32-bit C program

In the case of program TEST5.C, the program code is very simple, as follows:

test5.c #include <stdio.h>int main () {char str[4];str[0]= ' f '; str[1]= ' g '; str[2]= ' J '; str[3] = 0;  printf("CPU ID is%s\n", str); Exit (0);}


This C source program is not what 32 bit or 64 said, after the compilation with GCC, the 64-bit system to get 64-bit ELF files, the implementation will not have problems, as shown in:

With file Test5 It is very clear that 64-bit files are visible.

So how do you compile a 32-bit program? Use the-m32 parameter. You will find an error with the following error:

For warnings and exit incompatibility, you can include the header file Stdlib.h to resolve.

For gnu/stubs-32.h: No files or directories are required to install Glibc-devel and glibc-devel.i686. For CentOS, the installation command is:yum install glibc-devel and yum install glibc-devel.i686.

Yum Install Glibc-devel , such as :

Yum Install glibc-devel.i686 , such as:

After you compile with-m32, no more problems will occur. Such as:

With file Test5 It is very clear that 32-bit files are visible.

There are three main points to summarize:

The 64-bit system compiles 32-bit programs using the-m32 parameter, which is Gcc–m32–o output_file input_file.c

Hint implicitly declares an incompatible warning with the built-in function ' exit ', adding # include <stdlib.h> to resolve

Gnu/stubs-32.h: No file or directory error, need to install Glibc-devel and glibc-devel.i686 to resolve

2.32-bit assembler 1 under 64-bit system assembly). 64-bit assembly and 32-bit assembly differ

Assembly language 64-bit and 32-bit is very different, here is a copy of Intel's official 64-bit compilation of a simple introduction to the PDF document download: introduction_to_x64_assembly. Although the document is described in accordance with Microsoft's MASM format, but still can get some of the information we need, from this document can be known that the 64-bit register is different from the 32-bit, such as 64-bit register is RAX,RBX, low 32-bit is the use of EAX,EBX.

For system calls, 64-bit systems and 32-bit systems are vastly different, such as Sys_write system calls, 32-bit systems, and 64-bit systems, respectively:

Assembly call for 32-bit sys_write (stdout, str, length)

Assembly invocation of Sys_write (stdout, str, length) for 32-bit systems # sys_write (stdout, str, length) assembly call Movl $4,%eaxmovl $stdout,  %ebxmovl $str,%ecxmovl $length,%edxint $0x80

Assembly call for 64-bit sys_write (stdout, str, length)

Assembly invocation of Sys_write (stdout, str, length) for 64-bit systems # 64-bit Sys_write (stdout, str, length) assembly call Movq $  ,%raxmovq $stdout,%rdimovq $str,%rsimovq $length,%rdxsyscall 

Although for compatibility, 32-bit system calls can still run on 64-bit systems, but the two are vastly different. This article does not discuss how to write 64-bit assembly language, but to solve the 32-bit assembly code can be run in 64-bit environment.

2). 32-bit assembler with AS and LD assembled under 64-bit system

So how do you assemble a 32-bit assembler in a 64-bit system? Take the following example Cpuid2.s for example, the code is:

Cpuid2.s # Cpuid2.s file.section. DataOutput:    . Asciz "CPUID is '%s ' \ n". Section. BSS    . Lcomm Buffer, 12. Section. Text.globl _start_start:    NOP    movl $,%eax    cpuid    movl $buffer,%edi    movl%ebx, (%edi)    MOVL%edx, 4 (%edi)    movl%ecx, 8 (%edi)    pushl $buffer    pushl $output    printf    Addl $8,%esp    PUSHL $    exit 

The specific implementation of this code, the following chapters will be introduced. It is only now known to be used to output the CPU vendor ID string.

Under the 64-bit system, if as and LD as described in the book, errors can occur, such as:

That the push operation is invalid.

If you do not modify the source code for the 64-bit assembly, to solve this problem, you need to command 64-bit system according to 32-bit assembly, as parameter is--32, ld parameter is-M elf_i386. Such as:

File Cpuid2 can see files that are really 32 bits, and there is no problem with execution. In particular, ld-m elf_i386-dynamic-linker/lib/ld-linux.so.2-o cpuid2-l/lib-lc cpuid2.o This line of command is described here.

-M elf_i386: Indicates a connection in accordance with the Elf_i386 module, which is 32 bits.

-LC: Because the standard C library function is called printf and exit in the program, it is necessary to connect the C dynamic Library libc.so, so the parameter-LC is required to specify the connected library file, in general libxxx.so takes the-lxxx parameter.

-l/lib: The blogger's system has several libc.so, including 64-bit, 32-bit, arm structure, as shown, can be found many libc.so.

/lib/libc.so is the dynamic library required for the 32-bit x86 system, so using-l/lib to specify the path to the library file, the-L/LIB–LC specifies that the connection is/lib/libc.so.

-dynamic-linker/lib/ld-linux.so.2: Used to dynamically load libc.so dynamic libraries at run time. Otherwise, an error occurs when executing the generated executable file.

3). 64-bit system with GCC assembly 32-bit assembler

Using the above CPUID2.S as an example, the parameters of the Gcc-m32 are compiled into 32-bit system files, especially, because there is no main function, but instead of using _start to do the entry point, so you need to use the parameter –nostdlib, such as:

Description of the gcc-nostdlib-m32-o cpuid2 cpuid2.s-l/lib-lc .

-m32: Follow the 32-bit compilation.

-nostdlib: If you do not have this parameter, GCC will connect to the GNU library function, the function will be _start as the entry point, after executing a program, jump to main execution, and this assembler source program with _start do entry point, and there is no main, so, without this argument, You will be prompted for errors that do not have a main definition and duplicate definitions of _start. Once this parameter is added, the GNU function is not connected.

In summary, the practice of compiling 32-bit assembler under 64-bit systems is:

As--32–o output_file.o INPUT_FILE.S

Ld–m Elf_i386–dynamic-linker/lib/ld-linux.so.2–o Output_file–l/path–llibname INPUT_FILE.S

Or

Gcc–m32–nostdlib–o Output_file–l/path–llibname INPUT_FILE.S

Assembly Language Programming reading notes (2)-related tools 64-bit system chapter

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.