asm: Writing Inline Assembly

A usual IA includes these parts:

asm [volatile] ( AssemblerTemplate
               : OutputOperands
             [ : InputOperands
             [ : Clobbers ] ])
  • the first line includes asm [volatile], it means the contents in the following parenthesis is IA.
  • the AssblerTemplate is some assembler snippet, separated by ‘;‘, surrounded by "". for example, "mov %1 %%eax; mov %%eax %0" means to mov %1‘s content into %0. [first into %%eax, then into %0], note that the %N‘s N is the order of the CVars displays in the OutputOprands, and if N is larger than the total counts of the OOs, the exceeding is taken from vars above the assembly part, this part will be explained in the example.
  • the OutputOprands is consist of 3 parts, the alias name, the constraints and the variable, this can be duplicated and separated by ‘,‘. like [asmVarName1] "=m"(CVar1),[asmVarName2] "+r"(CVar2)
  • the InputOperands is almost the same as the OOs, but note that the constraints part should not begin with "+" "=" or other modifiers.
  • the Clobber part, declare the parts of register name to be used, or if using memory, declare memory usage.

example:

#include <stdio.h>
int main()
{
    //changes a and b‘s value
    int a = 10, b = 0;
    asm(//note this program should be compile with gcc
        "xchg %1,%%eax;xchg %%eax,%0"
        :"=r"(b),"=m"(a)
        :"r"(a)
        :"%eax" //note register declare with one ‘%‘
    );
    return 0;
}

note the register %eax is not initialized. a‘s value should be unknown. b‘s value should be 10.

the question is, in assembler, mov a,b means pass b‘s value into a, however, here is the opposite, to pass a‘s value into b..

时间: 2024-08-02 14:02:33

asm: Writing Inline Assembly的相关文章

Linux C中内联汇编的语法格式及使用方法(Inline Assembly in Linux C)---- asm [volatile](**)

在阅读Linux内核源码或对代码做性能优化时,经常会有在C语言中嵌入一段汇编代码的需求,这种嵌入汇编在CS术语上叫做inline assembly.本文的笔记试图说明Inline Assembly的基本语法规则和用法(建议英文阅读能力较强的同学直接阅读本文参考资料中推荐的技术文章 ^_^). 注意:由于gcc采用AT&T风格的汇编语法(与Intel Syntax相对应,二者的区别参见这里),因此,本文涉及到的汇编代码均以AT&T Syntax为准. 1. 基本语法规则 内联汇编(或称嵌入汇

Linux C中内联汇编的语法格式及使用方法(Inline Assembly in Linux C)

在阅读Linux内核源码或对代码做性能优化时,经常会有在C语言中嵌入一段汇编代码的需求,这种嵌入汇编在CS术语上叫做inline assembly.本文的笔记试图说明Inline Assembly的基本语法规则和用法(建议英文阅读能力较强的同学直接阅读本文参考资料中推荐的技术文章 ^_^). 注意:由于gcc采用AT&T风格的汇编语法(与Intel Syntax相对应,二者的区别参见这里),因此,本文涉及到的汇编代码均以AT&T Syntax为准. 1. 基本语法规则 内联汇编(或称嵌入汇

[转载]【Linux学习笔记】Linux C中内联汇编的语法格式及使用方法(Inline Assembly in Linux C)

在阅读Linux内核源码或对代码做性能优化时,经常会有在C语言中嵌入一段汇编代码的需求,这种嵌入汇编在CS术语上叫做inline assembly.本文的笔记试图说明Inline Assembly的基本语法规则和用法(建议英文阅读能力较强的同学直接阅读本文参考资料中推荐的技术文章 ^_^).        注意:由于gcc采用AT&T风格的汇编语法(与Intel Syntax相对应,二者的区别参见这里),因此,本文涉及到的汇编代码均以AT&T Syntax为准. 1. 基本语法规则    

meaning of &quot;%U0%X0&quot; in PowerPC in the GCC inline asm

There are idiosyncrasies in the GCC inline assembly syntax.in the line, __asm__ __volatile__("stw%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i)); the stw assembly instruction takes 2 arguments and the %U0%X0 are constraints on

error: invalid &#39;asm&#39;: invalid operand for code &#39;w&#39;

google 出结果 http://stackoverflow.com/questions/15623609/including-curl-into-the-android-aosp ..............................................................................................................................................................

讲讲c++ Session 2 内联(inline)函数

定义:内联函数是一种内联扩展,即通过在每个函数调用的地址插入功能代码,从而节省开销来函数调用,避免跳转到一个子程序. inline关键字类似于宏,编译器在它被称为每个地方放置了内联函数的新副本,内联函数的运行速度比正常的函数调用快,开销都省了,但是,有一个内存问题.如果一个内联函数被调用的10次,将有10个拷贝插入到代码的函数.因此,内联函数是最好的小功能,这些功能通常被称为.一个类的成员函数,如果在类定义中定义,在默认情况下(没有必要使用inline关键字),否则,该关键字是必要的.编译器可能

智能合约从入门到精通:Solidity Assembly

简介:上一节,我们讲过Solidity 汇编语言,这个汇编语言,可以不同Solidity一起使用.这个汇编语言还可以嵌入到Solidity源码中,以内联汇编的方式使用.下面我们将从内联汇编如何使用着手,介绍其与独立使用的汇编语言的不同,最后再介绍这门汇编语言.Solidity Assembly内联汇编通常我们通过库代码,来增强语言我,实现一些精细化的控制,Solidity为我们提供了一种接近于EVM底层的语言,内联汇编,允许与Solidity结合使用.由于EVM是栈式的,所以有时定位栈比较麻烦,

linux c coding style

Linux kernel coding style This is a short document describing the preferred coding style for the linux kernel. Coding style is very personal, and I won't _force_ my views on anybody, but this is what goes for anything that I have to be able to mainta

MIT OS lab1

Lab 1: Booting a PC 1.Introduction Software setup Get jos code: athena% mkdir ~/6.828 athena% cd ~/6.828 athena% add git athena% git clone http://pdos.csail.mit.edu/6.828/2014-jos.git lab Cloning into lab... athena% cd lab Part 1: PC Bootstrap 1.1