美文网首页
内联汇编

内联汇编

作者: lindyang | 来源:发表于2019-12-26 11:35 被阅读0次

lodsb: ds:[esi] -> al

stosb: al -> es:[edi]

SCASB: 比较 [edi] 和 al

Compares a byte in memory with the AL register value
AT&T: cmp (di), al, 是 al - (di)呢

asm (“assembly code” : output locations : input operands : changed registers);

如果不希望汇编语句被 GCC 优化而作修改,就需要在 asm 符号后面添加关键词 volatile
关键词 volatile 也可以放在函数名前来修饰函数,用来通知 gcc 编译器该函数不会返回:
volatile void do_exit(long code);
static inline volatile void oom(void)

如果输入寄存器的代码是 0 或为时,则说明使用与相应输出一样的寄存器;  空验证失败

“constraint”(variable)

q: EAX,EBX,ECX,EDX

r: general-purpose register; EAX,EBX,ECX,EDX,EBP,ESP,ESI,EDI

EAX为累加器,ECX为计数器,EBX,EBP为基址寄存器,ESI,EDI为变址寄存器,EBP还可以是基指针,ESP为堆栈指针

g: Use any register or memory

o:  Use an offset memory location(使用内存地址并可以加偏移值)

#include<stdio.h>

int main(void){

  char __res; int p[] = {0x970F};

  char b = 'b'; // 98

  char c = 'c'; // 99

  __asm__("movb %%cs:1%1, %%al" :"=a"(__res) :"o"(b));

  return __res;

}

/*

***-19****-18****-17****-16****-15****-14***-13****

|   98   |    99   |          |  0x0F  0x97  0x00  0x00  |                              
movl $38671, -16(%ebp)
movb $98, -19(%ebp)
movb $99, -18(%ebp)
movb %cs:1-19(%ebp), %al  # 99

*/

changed registers: 

using the full register names;
Using the percent sign with the register name is optional

把那些没有在输出/输入寄存器部分列出,但是在汇编语句中明确使用到或隐含使用到的寄存器.

如果在内联汇编中使用了没有在输入输出中定义的任何内存位置,必须标记为被破坏的。在改动的寄存器列表中使用”memory“通知编译器这个内存位置在内联汇编中被改动。

The compiler assumes that registers used in the input and output values will change, and handles thataccordingly. You do not need to include these values in the changed registers list. In fact, if you do, itwill produce an error message:
p.s. error: ‘asm’ operand has impossible constraints

下面是 output register

+ The operand can be both read from and written to.

= The operand can only be written to.

% The operand can be switched with the next operand if necessary.

& The operand can be deleted and reused before the inline functionscomplete.

早期会变的(earlyclobber)操作数。表示在使用完操作数之前,内容会被修改

earlyclobber

earlyclobber的意思是说,某个输出操作数,可能在gcc尚未用完所有的输入操作数之前,就已经被写入(inline asm的输出操作)值了。 换言之,gcc尚未使用完全部的输入操作数的时候,就已经对某些输出操作数产生了输出操作。

asm("movl $10, %0; mov $20, %1;": "=&r"(v1): "r"(v2));

“&” 是说明 Register should be used for output only

http://blog.sina.com.cn/s/blog_63fe270801014w5u.html

stos

STOS指令的作用是将eax中的值拷贝到ES:EDI指向的地址

https://zhuanlan.zhihu.com/p/27586298

防止头文件重复引入

文件开头加:
#pragma once

#ifndef _TEST_H
#define _TEST_H
...
#endif

相关文章

  • 汇编语言如何与高级语言混编

    汇编混编的两种方式(内联汇编 和 外链汇编) 内联汇编 asm();这是内联汇编,编译器可以直接运行asm ( 汇...

  • 内联汇编

    lodsb: ds:[esi] -> al stosb: al -> es:[edi] SCASB: 比较 [ed...

  • 内联汇编

    AT&T汇编语法 GCC只支持AT&T汇编语法内嵌在C语言中。 Intel和AT&T汇编风格对比: AT&T寻址 ...

  • 内联汇编

    内联汇编是指在 C/C++ 代码中嵌入的汇编代码,与全部是汇编的汇编源文件不同,它们被嵌入到 C/C++ 的大环境...

  • C编程使用内联汇编控制PC蜂鸣器发声

    有了《初识Linux汇编》和《内联汇编控制PC蜂鸣器》两篇文章的基础了解后,我们使用内联汇编来改造《C编程控制PC...

  • CAS与内存屏障: 内联汇编的实际应用场景_(S2实现CAS)

    c++的CAS与内存屏障: 从c/c++的内联汇编说起(S3) 现在讨论下内联汇编与CAS lock-free是什...

  • DPDK gcc内联汇编

    在DPDK中,使用gcc的内联汇编实现高效率的函数,比如自旋锁,cas操作等。今天简单介绍一下gcc内联汇编语法和...

  • gcc内联汇编

    文章来自这里:gcc内联汇编...... 在阅读Linux内核源码或对代码做性能优化时,经常会有在C语言中嵌入一段...

  • 11 内联汇编

    11.1 汇编概述 尽管智能合约的大部分业务需求我们都能通过solidity提供的功能和模块完成,但一些特殊的功能...

  • Visual Studio 不支持x64 vc中内联汇编的原因

    Visual Studio目前只支持32位(x86)的内联汇编,而不支持64位(x64)下的内联汇编,在x64下编...

网友评论

      本文标题:内联汇编

      本文链接:https://www.haomeiwen.com/subject/nrdugftx.html