Linux application experience and skills: Nasm compilation for Linux

Source: Internet
Author: User
Article Title: Linux Application Experience and tips: Use Nasm for compilation in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
With the popularity of Linux, how to program on the Linux platform, especially Assembly, has become a concern, but there is little information about this. After visiting websites and forums, I will share the collected information with you based on my experience.
  
   About Nasm
  
In Linux, there are many compilation compilers with different syntaxes. GAS is a basic compilation tool. However, GAS does not use the Assembly syntax we usually use in the DOS system. It uses the AT&T syntax format, which inherits the characteristics of Unix, the syntax format is quite different from that of Intel.
  
As a compilation fan who transformed from DOS to Linux, AT&T's format is not easy to accept, and it is also very difficult to learn. I would like to introduce another compilation tool Nasm, which is similar to the Intel x86 Assembly style Masm used in DOS. It is the most similar Assembly Tool for Linux syntax and DOS.
  
The following uses the hello. asm Applet as an example to describe how to compile with Nasm in Linux.
  
Hello. asm
Hello world for Linux
Section. text
Extern puts
Global main
  
Main:
Push dword msge;
Call puts;
Add esp, byte 4;
Ret;
  
Msge:
Db "Hello World !", 0
  
The Nasm compilation command is as follows:
  
Nasm-f elf hello. asm
Gcc-o hello. o
  
"Nasm-f elf hello. asm" is to compile hello. asm into an elf object file. "Gcc-o hello. o" will compile hello. asm into a binary executable file hello.com.
  
The Nasm-f command has two main parameters: aout and elf. If you are not sure whether the Linux system should use aout or elf, you can enter the file Nasm command in the Nasm directory. If the output is "Nasm: ELF 32-bit LSB executable i386 (386 and up) Version 1 "should use elf; aout should be used if" Nasm: Linux/i386 demand-paged executable (QMAGIC) "is output. Use the Nasm-h command to obtain the complete description of the Nasm command line.
  
   Program description
  
Hello. asm calls the puts function in Linux. The principle is the same as that of calling the C language function in DOS. It first uses extern to declare puts as an external function, and then pushes the parameter (that is, the msg address) into the stack, finally, the call function is output. Let's look at a program:
  
Section. text
Global main
  
Main:
Mov eax, 4; called on 4
Mov ebx, 1; ebx returns 1 to indicate output
Mov ecx, msge; the first address of the string is sent to ecx
Mov edx, 14; the length of the string is sent to edx
Int 80 h; Output string
Mov eax, 1; 1 call
Int 80 h; End
  
Msge:
Db "Hello World !", 0ah, 0dh
  
This program is very similar to the DOS program and the result is the same as hello. like asm, it uses 80 h interrupt in Linux, equivalent to 21h interrupt in DOS, but because Linux is a 32-bit operating system, it uses registers such as eax and ebx.
  
   Differences between Nasm and Masm
  
1. Case sensitivity
  
Like Linux, Nasm is case-sensitive. Hello And hello are different identifiers. However, to use Masm for compilation in DOS or OS/2, you must add the UPPERCASE parameter to make it case sensitive.
  
2. Use []
  
The use of [] by Nasm is also different from that of Masm. Nasm requires that all expressions and memory operands must be written in. The following are two examples.
  
Masm Syntax:
  
Mov ax, bar
Mov ax, es: [di]
Mov ax, [di] + 1
  
Nasm Syntax:
  
Mov ax, [bar]
Mov ax, [es: di]
Mov ax, [di + 1]
  
3. variable types
  
The variable type is not stored in Nasm. In Masm, even variables in the [] addressing mode must specify the variable type. Nasm does not support LODS, MOVS, STOS, SCAS, CMPS, INS, OUTS, and Other types. It only supports specified types of operations such as lodsb and lodsw, and assume operations are not supported in Nasm, the segment address depends entirely on the value of the stored segment register.
  
For detailed usage and syntax of Nasm, refer to the Nasm user manual.
  
   Summary
  
Linux, as a multi-user operating system, is very different from DOS, especially when it comes to operating system principles. The Linux operating system is actually an interface between Abstract resource operations and specific hardware operations.
  
For a multi-user operating system such as Linux, it needs to avoid direct access to hardware and mutual interference between users. Therefore, Linux takes over BIOS calls and port input and output, systemCall is required if you want to access hardware through Linux. It is actually a collection of many C functions and can be called in the assembler. The Calling method is the same as the compilation method in DOS, and additional library functions are not linked when Nasm is used for assembly.
Related Article

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.