随笔 - 74, 文章 - 0, 评论 - 26, 引用 - 0
数据加载中……

如何在Visual Studio 2005环境下用c语言调用ARM汇编,以便了解ARM指令

在网上找到了一个这样的例子,是Larry Bank写的。贴出来以供大家学习。让大家了解ARM汇编指令,并用vs2005进行调试跟踪。

工程是pocket pc 2003的工程,主要包含两个文件:main.c, armtest.asm。

对armtest.asm需要自定义编译选项,

命令行为 armasm -g armtest.asm
输出为   armtest.obj

源码如下:

main.c 源码

// Windows CE Sample Application

// Demonstrations how to use ARM assembly language within a C program

// Written by Larry Bank 3/25/2007

#include <windows.h>

int iGlobal;

int ARMTEST1(int, int, int , int);

/****************************************************************************

* FUNCTION : WinMain(HANDLE, HANDLE, LPSTR, int)                                               *

* PURPOSE : Program entrypoint                                                                               * ****************************************************************************/

int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)

{

      int iResult;

      TCHAR szTemp[256];

      iGlobal = 5;

      iResult = ARMTEST1(1,2,3,4);

      wsprintf(szTemp, L"Result = %d", iResult);

      MessageBox(HWND_DESKTOP, szTemp, L"ASM Result", MB_OK);

     return 0;

} /* WinMain() */

armtest.asm 源码:

; TITLE("Sample App")

;++

     AREA sample, CODE, READONLY ; name this block of code

     EXPORT ARMTEST1

   IMPORT iGlobal

; Called from C as int ARMTEST1(int, int, int, int);

; The first 4 parameters are passed in r0-r3, more parameters would be passed on the stack

ARMTEST1 proc

    add r0,r0,r1 ; add all of the inputs together

    add r0,r0,r2

    add r0,r0,r3 

   ldr r1,=iGlobal ; get the value of our global variable

      ldr r1,[r1] ; dereference the pointer (I know there's a pipe stall here)

    add r0,r0,r1 ; we're not concerned with performance in this example

    mov pc,lr ; return to C with the value in R0

     endp

     LTORG ; allow room for the address constant of our global (loaded relative to PC)

     END

posted on 2008-09-22 10:36 井泉 阅读(1457) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理