欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux中c语言常用内嵌汇编

程序员文章站 2024-03-23 14:12:34
...
源码地址  http://download.csdn.net/download/sinat_35360663/10224892
//
//  main2.c
//  allen
//
//  Created by Allenboy on 2018/1/20.
//  Copyright © 2018年 Allenboy. All rights reserved.
//
//编译不过 可以生成.s文件  gcc -S main2.cpp
// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 

#include <stdio.h>
    int a = 10;
    int b = 20;
    int result;
int main()
{
    
    asm(
//       "pusha\n"
        "movl a,%eax\n"
        "movl b,%ebx\n"
        "imull %ebx,%eax\n"
        "movl %eax,result"
 //       "popa\n"
            );
    printf("the result is %d\n",result);
    return 0;
}
//  allen
//
//  Created by Allenboy on 2018/1/20.
//  Copyright © 2018年 Allenboy. All rights reserved.
//
//编译能过 可以生成可执行文件  gcc main2.cpp (a.out) 或者 gcc -o test main2.cpp (test) 运动时用 ./a.out(./test)
// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 
#include<stdio.h>
int main()
{
    int data1 = 10;
    int data2 = 20;
    int result;
    
    asm (
     
        "imull %%edx,%%ecx\n\t"
        "movl %%ecx,%%eax\n\t"
        : "=a" (result)
        : "d" (data1), "c" (data2)
     
            );
    printf("the result is %d\n",result);
    return 0;
}
//  allen
//
//  Created by Allenboy on 2018/1/20.
//  Copyright © 2018年 Allenboy. All rights reserved.
//
//
// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 

//tset 3
#include <stdio.h>

int main ()
{
  char* input[30] = {"ALLNBOY  人饭子  饭醉份子 \n"};
  char* output[30];
  int length = 25;
  asm  volatile ( 
  	"cld \n\t"
  	"rep movsb"
  	:
  	:"S"(input),"D"(output),"c"(length)
  	); //movs指令需要的3个指令,s代表要复制的字符串地址(esi) d代表目标位置存放在edi c 要复制的长度放在 ecx
  	printf("%s",*output);
    return 0;
}
// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 

//tset 4占位寄存器 表示法
#include <stdio.h>

int main ()
{
  int data1 = 10;
  int data2 = 20;
  int result;
  asm  volatile ( 
  	"imull %1,%2\n\t"
  	"movl %2,%0"
  	:"=r"(result)
  	:"r"(data1),"r"(data2)
  	); //
  	printf("%d\n",result);
    return 0;
}

#APP
# 9 "testasm.c" 1
    imull %eax,%edx
    movl %edx,%eax
# 0 "" 2
#NO_APP



// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 

//tset 5当输入输出是同一个变量时可以用相同寄存器
#include <stdio.h>

int main ()
{
  int data1 = 10;
  int data2 = 20;
  
    asm(
  	"addl %1,%0"
  	: "=r"(data2)
  	: "r"(data1),"0"(data2)
  	: "%ecx","%edx"
  	); //
  	printf("%d\n",data2);
    return 0;
}


// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 

//tset 6替换占位符
#include <stdio.h>

int main ()
{
  int data1 = 10;
  int data2 = 20;
  
    asm(
  	"addl %[value1],%[value2]"
  	: [value2] "=r"(data2)
  	: [value1] "r"(data1),"0"(data2)
  	); //
  	printf("%d\n",data2);
    return 0;
}

// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 

//tset 7使用内存位置
#include <stdio.h>

int main ()
{
  int data1 = 40;
  int data2 = 20;
  int result;
    asm(
  	"divb %2\n\t"
  	"movl %%eax,%0"
  	: "=m"(result)
  	: "a"(data1),"m"(data2)
  	); //
  	printf("%d\n",result);
    return 0;
}


// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 

//tset 8浮点数求法
#include <stdio.h>

int main ()
{
  float angle =90;
  float radian,cosine,sine;
  radian = angle/180*3.14159;
  asm("fsincos"
  	:"=t"(cosine),"=u"(sine)
  	:"0"(radian)
  	);
  	printf("angle is %f consine is %f sine is %f radian is %f\n",angle,cosine,sine,radian);
    return 0;
}





// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 

//tset 9浮点数求法
#include <stdio.h>

int main ()
{
  float angle =90;
  float radian,cosine,sine;
  radian = angle/180*3.14159;
  asm("fsincos"
  	:"=t"(cosine),"=u"(sine)
  	:"0"(radian)
  	);
  	printf("angle is %f consine is %f sine is %f radian is %f\n",angle,cosine,sine,radian);
    return 0;
}


#APP
# 9 "testasm.c" 1
	fsincos
# 0 "" 2
#NO_APP
	fxch	%st(1)
	fstps	-20(%rbp)
	fstps	-8(%rbp)
	movss	-20(%rbp), %xmm5
	movss	%xmm5, -4(%rbp)

// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 

//tset 10跳转
#include <stdio.h>

int main ()
{
  int a = 10;
  int b = 20;
  int result;
  asm(
  	"cmp %1,%2\n\t"
  	"jge greater\n\t"
  	"movl %1,%0\n\t"
  	"jmp end \n"
	"greater:\n\t"
	"movl %2,%0\n"
	"end:"
	:"=r"(result)
  	:"r"(a),"r"(b)
  	);
  	printf("value id %d",result);
    return 0;
}
	
	
	
	movl	-8(%rbp), %edx
#APP
# 9 "testasm.c" 1
	cmp %eax,%edx
	jge greater
	movl %eax,%eax
	jmp end 
greater:
	movl %edx,%eax
end:
# 0 "" 2
#NO_APP
	movl	%eax, -4(%rbp)




// gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 


//tset11 跳转 数字加字母f
#include <stdio.h>

int main ()
{
  int a = 10;
  int b = 20;
  int result;
  asm(
  	"cmp %1,%2\n\t"
  	"jge 0f\n\t"
  	"movl %1,%0\n\t"
  	"jmp 1f \n"
	"0:\n\t"
	"movl %2,%0\n"
	"1:"
	:"=r"(result)
  	:"r"(a),"r"(b)
  	);
  	printf("value id %d",result);
    return 0;
}
	
	
	movl	$20, -8(%rbp)
	movl	-12(%rbp), %eax
	movl	-8(%rbp), %edx
#APP
# 9 "testasm.c" 1
	cmp %eax,%edx
	jge 0f
	movl %eax,%eax
	jmp 1f 
0:
	movl %edx,%eax
1:
# 0 "" 2
#NO_APP
	movl	%eax, -4(%rbp)
	movl	-4(%rbp), %eax
	movl	%eax, %esi