免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1714 | 回复: 0

C语言调用汇编函数 [复制链接]

论坛徽章:
0
发表于 2009-04-20 23:07 |显示全部楼层


       
        文件:arm汇编手册中文版.chm
        大小:151KB
        下载:
下载
       
                               
               

  Normal
  0
  
  7.8 磅
  0
  2
  
  false
  false
  false
  
   
   
   
   
   
   
   
   
   
   
   
   
  
  MicrosoftInternetExplorer4



/* Style Definitions */
table.MsoNormalTable
        {mso-style-name:普通表格;
        mso-tstyle-rowband-size:0;
        mso-tstyle-colband-size:0;
        mso-style-noshow:yes;
        mso-style-parent:"";
        mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
        mso-para-margin:0cm;
        mso-para-margin-bottom:.0001pt;
        mso-pagination:widow-orphan;
        font-size:10.0pt;
        font-family:"Times New Roman";
        mso-fareast-font-family:"Times New Roman";
        mso-ansi-language:#0400;
        mso-fareast-language:#0400;
        mso-bidi-language:#0400;}
在C程序main函数中,接收用户输入任意个整数,然后在main中调用使用ARM汇编编写的函数(在该函数中完成对这些整数的排序功能),然后再在C程序main函数中输出这些排好顺序的整数。
这个要求涉及到了APCS(ARM 过程调用标准),上面的手册中专门介绍了它
main.c
#include stdio.h>
int main()   
{
    int i=0;
    int num=0;
    int *array=NULL;
   
    while(num = 0) //输入数组中元素的个数
    {
        printf("please enter the number of elements:\n");
        scanf("%d",&num);
        if(num > 0)   
        {
            break;
        }
    }
   
    if(NULL == (array = (int *)malloc(num*sizeof(int))))   
    {
        printf("malloc failed!\n");
        exit(-1);
    }
    printf("please enter the elements:\n");
    for(i = 0; inum; i++)    {
        printf("\n%d:\t", i);
        scanf("%d", array+i);
    }
    sort(array, num);//调用相应的汇编的函数,注意分析传参过程
    printf("The Result is:\n");
    for(i = 0; inum; i++)    {
        printf("%d:\t%d\n", i, *(array+i));
    }
    return 0;
}
//下面是相应的Sort.s
.section .text;声明为代码段
.globl sort;声明全局变量
sort:  ;linux下需要加冒号
    mov        r2, #0            
    mov        r8, r0
    mov        r9, r0
loop1:
    sub        r1, r1, #1
    cmp        r2, r1        
    add        r1, r1, #1
    beq        end   
    mov        r6, r2
    add        r3, r2, #1        
   
loop2:   
    cmp        r3, r1
    beq        continue1
   
    mov        r3, r3, lsl #2
    add        r8, r8, r3
    ldr        r5, [r8]
   
    mov        r6, r6, lsl #2
    add        r9, r9, r6
    ldr        r4, [r9]
    cmp        r4, r5
    bgt        exchange
continue2:        
    sub        r8, r8, r3
    mov        r3, r3, lsr #2
   
    sub        r9, r9, r6
    mov        r6, r6, lsr #2
   
    add        r3, r3,    #1
    b        loop2
        
exchange:
     str        r4, [r8]
    str        r5, [r9]
        b        continue2
continue1:
    add        r2, r2, #1
    b        loop1
end:
注意:通过APCS传过来的两个变量,保存在r0和r1,分别代表是数组的首地址和元素的个数
使用Arm交叉编译通过
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/61062/showart_1904902.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP