The platform we use is the Linux system, specifically the CentOS-64-bit version. The following is the source code for the first assembler program:
# Purpose: Exit the Linux kernel and return a simple status code # # Input: No # # output: No output on the console, you can use the echo $? To view the status code # # variable: # %eax Save the system call number # %EBX Save the return status #.section. Data.section. Text.globl _start_start: movl $ ,%eax #这是用于退出程序的Linux内核命令号 (System call) MOVL $8, %EBX #这是返回给操作系统的状态码 #改变这个数字, return the value of echo $ to a different int $0x80 #将内核唤醒 to run the exit command
The assembly, link, and output of the assembler are:
Linux system, the shell command echo $? Represents the result of printing the last command execution, the range of the result value is 0-255, typically 0 as the command to successfully execute the return code. In this example we are using 8, not 0, as a demo. Here we analyze the specific meaning of the code in the program: (1) "#" is the beginning of the program's comments, will not be compiled or executed; (2) with '. ' The assembly instructions or pseudo-operations are not translated into machine instructions; . Section. Data defines the beginning of a program's data segment. The data segment lists all the memory storage space that is required for program data. The. Section. Text defines the beginning of a program's text segment. The text segment is the part that holds the program directives. The. Globl _start _start is a special symbol that is always marked with a. Globl, because it marks the beginning of the program, that is, the entry address of the program. _start: Defines the value of the _start label. (3) Program instructions: MOVL $,%eax Move the number 1 into the EAX register. The operands of $ and%eax are called instruction Movl. Where $ is addressed immediately, and if there is no $ symbol, it is addressed directly;%eax is called a register, and there are several general-purpose registers in the x86 system:%EAX %ebx %ecx %edx %edi %esi There are several dedicated registers: %ebp %esp %eip %eflags
This directive puts 1 in register%eax because 1 corresponds to system call exit in the system call; MOVL $8,%EBX When the program execution ends, the status code of the program exit is saved in the%EBX register, which can be seen by the echo $ command. int $0x80 int represents an interrupt, 0x80 is the interrupt number to be used, and the interrupt will cause a system call. Exit is called in the program.
Examples of Linux compilation (i)