"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet, the article only for learning Exchange, do not use for commercial purposes"
The most common way to use assembly-language pen programming is to write assembler functions in high-level languages (C and C + +) programs, which are written directly into C and C + + language programs called inline compilations. the GNU C compiler uses the ASM keyword to indicate the paragraph of the source code written in assembly language. The basic format of the ASM segment is as follows:ASM ("as Code");assembly instructions in parentheses must be in parentheses, with more than one instruction, to separate assembly-language code from each line with a new line, because the compiler literally obtains assembly code in the ASM segment, and places them in assembly code generated for the program, sometimes using tab characters to indent instructions so that they differ from the labels. The following is an example of a simple inline assembly:ASM ("Movl $,%eax\n\t movl $0,%ebx\n\tint $0x80");The example includes 3 instructions, which can be confusing when using many assembly instructions, so the instructions are generally placed in a separate line.
ASM ("Movl $,%eax\n\t" "Movl $0,%ebx\n\t" " int $0x80");
c Global variables can be used to pass data into and out of inline assembly language, note that local variables cannot be used.
#include <stdio.h>int result = 10;int Main (int argc, const char *argv[]) { asm ("Addl $, result\n\t" " Subl, result\n\t "); printf ("The result is%d\n", result); return 0;}
use GCC to generate the assembly code as follows:
. File "inline-as.c". Globl result.data.align 4.type result, @object. Size result, 4result:.long 10.section. Rodata. Lc0:.string "The result is %d\n". Text.globl main.type Main, @functionmain:p USHL%ebpmovl%esp,%ebpandl $-16,%espsub L $16,%esp#app# 7 "INLINE-AS.C" 1addl $, Resultsubl $, result# 0 "" 2#NO_APPMOVL result,%EDXMOVL $. LC0,%eaxmovl%edx, 4 (%ESP) Movl%eax, (%ESP) call PRINTFMOVL $,%eaxleaveret.size Main,.-main.ident "GCC: (GNU) 4.4.7 201 20313 (Red Hat 4.4.7-3) ". Section. Note. Gnu-stack, "", @progbits
c file Compilation execution results are:
$./inline-asthe result is 9$
The code between #APP和 #noapp in the disassembled assembly code is the inline assembly code specified for the ASM segment. the ANSI C specification uses the keyword __asm__ to replace the keyword ASM when using inline assembly statements, because the ANSI C keyword ASM is used for other purposes. as follows:
__asm__ ("Addl $, result\n\t" "Subl $, result\n\t");
The Basic ASM format provides a simple style for creating assembly code, but there are some limitations. First, all input and output values must use the global variables of the C program, as seen in the example above. Second, the value of any register is not changed in the inline assembly code. The GNU compiler provides an extensible format for ASM segments to help resolve these issues. The extended format takes the new format, as follows:
<span style= "Font-family:microsoft Yahei;" >asm ("As code": Output location:input operands:changed registers);</span>
The format consists of 4 parts, separated by a colon: assembly code, output location, input operand, changed register. In the extended ASM format, not all parts must appear.
Most programmers define inline assembly code as macro functions that are defined in the same way as the C language. Define the inline assembly macro function as follows:
#define CAL ({ asm ("Addl $, result\n\t" "Subl $, result\n\t"); })
here the ASM statement must be in a pair of curly braces to indicate the beginning and end of the statement, or the compiler will generate an error message. Here is a simple example of using a macro:
#include <stdio.h> #define CAL ({ asm ("Addl $ result\n\t" " Subl $ result\n\t"); }) int result = 10;int Main (int argc, const char *argv[]) { CAL; printf ("The result is%d\n", result); return 0;}
Linux Platform x86 compilation (18): Inline assembly