Chinaunix首页 | 论坛 | 博客
  • 博客访问: 665556
  • 博文数量: 156
  • 博客积分: 4833
  • 博客等级: 上校
  • 技术积分: 1554
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-21 19:36
文章分类

全部博文(156)

文章存档

2016年(2)

2013年(1)

2012年(13)

2011年(30)

2010年(46)

2009年(29)

2008年(23)

2007年(12)

分类: LINUX

2007-11-30 02:45:37

系统调用格式
 
 SWI + offset+ 系统调用号
 参数 R0,R1,R2
 
.text
       .align  2
       .global main
main:
       adr     r1, msg         @ address
       mov     r0, #1          @ stdout
       mov     r2, #13         @ length
       swi     #0x900004       @ sys_write
       mov     r0, #0
       swi     #0x900001       @ sys_exit
       .align  2
msg:
       .asciz  "hello, world\n"
 
//系统调用汇编参数传递格式 include\asm-arm\unistd.h
#define __syscall_return(type, res)     \
do {         \
 if ((unsigned long)(res) >= (unsigned long)(-MAX_ERRNO)) { \
  errno = -(res);      \
  res = -1;      \
 }        \
 return (type) (res);      \
} while (0)
#define _syscall0(type,name)      \
type name(void) {       \
  __SYS_REG(name)       \
  register long __res_r0 __asm__("r0");     \
  long __res;        \
  __asm__ __volatile__ (      \
  __syscall(name)       \
 : "=r" (__res_r0)      \
 : __SYS_REG_LIST()      \
 : "memory" );       \
  __res = __res_r0;       \
  __syscall_return(type,__res);      \
}
#define _syscall1(type,name,type1,arg1)     \
type name(type1 arg1) {       \
  __SYS_REG(name)       \
  register long __r0 __asm__("r0") = (long)arg1;   \
  register long __res_r0 __asm__("r0");     \
  long __res;        \
  __asm__ __volatile__ (      \
  __syscall(name)       \
 : "=r" (__res_r0)      \
 : __SYS_REG_LIST( "0" (__r0) )     \
 : "memory" );       \
  __res = __res_r0;       \
  __syscall_return(type,__res);      \
}
#define _syscall2(type,name,type1,arg1,type2,arg2)   \
type name(type1 arg1,type2 arg2) {     \
  __SYS_REG(name)       \
  register long __r0 __asm__("r0") = (long)arg1;   \
  register long __r1 __asm__("r1") = (long)arg2;   \
  register long __res_r0 __asm__("r0");     \
  long __res;        \
  __asm__ __volatile__ (      \
  __syscall(name)       \
 : "=r" (__res_r0)      \
 : __SYS_REG_LIST( "0" (__r0), "r" (__r1) )   \
 : "memory" );       \
  __res = __res_r0;       \
  __syscall_return(type,__res);      \
}

#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)  \
type name(type1 arg1,type2 arg2,type3 arg3) {    \
  __SYS_REG(name)       \
  register long __r0 __asm__("r0") = (long)arg1;   \
  register long __r1 __asm__("r1") = (long)arg2;   \
  register long __r2 __asm__("r2") = (long)arg3;   \
  register long __res_r0 __asm__("r0");     \
  long __res;        \
  __asm__ __volatile__ (      \
  __syscall(name)       \
 : "=r" (__res_r0)      \
 : __SYS_REG_LIST( "0" (__r0), "r" (__r1), "r" (__r2) )  \
 : "memory" );       \
  __res = __res_r0;       \
  __syscall_return(type,__res);      \
}

#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)\
type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) {  \
  __SYS_REG(name)       \
  register long __r0 __asm__("r0") = (long)arg1;   \
  register long __r1 __asm__("r1") = (long)arg2;   \
  register long __r2 __asm__("r2") = (long)arg3;   \
  register long __r3 __asm__("r3") = (long)arg4;   \
  register long __res_r0 __asm__("r0");     \
  long __res;        \
  __asm__ __volatile__ (      \
  __syscall(name)       \
 : "=r" (__res_r0)      \
 : __SYS_REG_LIST( "0" (__r0), "r" (__r1), "r" (__r2), "r" (__r3) ) \
 : "memory" );       \
  __res = __res_r0;       \
  __syscall_return(type,__res);      \
}
 
#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) { \
  __SYS_REG(name)       \
  register long __r0 __asm__("r0") = (long)arg1;   \
  register long __r1 __asm__("r1") = (long)arg2;   \
  register long __r2 __asm__("r2") = (long)arg3;   \
  register long __r3 __asm__("r3") = (long)arg4;   \
  register long __r4 __asm__("r4") = (long)arg5;   \
  register long __res_r0 __asm__("r0");     \
  long __res;        \
  __asm__ __volatile__ (      \
  __syscall(name)       \
 : "=r" (__res_r0)      \
 : __SYS_REG_LIST( "0" (__r0), "r" (__r1), "r" (__r2),  \
     "r" (__r3), "r" (__r4) )   \
 : "memory" );       \
  __res = __res_r0;       \
  __syscall_return(type,__res);      \
}
#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5,type6,arg6) \
type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5, type6 arg6) { \
  __SYS_REG(name)       \
  register long __r0 __asm__("r0") = (long)arg1;   \
  register long __r1 __asm__("r1") = (long)arg2;   \
  register long __r2 __asm__("r2") = (long)arg3;   \
  register long __r3 __asm__("r3") = (long)arg4;   \
  register long __r4 __asm__("r4") = (long)arg5;   \
  register long __r5 __asm__("r5") = (long)arg6;   \
  register long __res_r0 __asm__("r0");     \
  long __res;        \
  __asm__ __volatile__ (      \
  __syscall(name)       \
 : "=r" (__res_r0)      \
 : __SYS_REG_LIST( "0" (__r0), "r" (__r1), "r" (__r2),  \
     "r" (__r3), "r" (__r4), "r" (__r5) )  \
 : "memory" );       \
  __res = __res_r0;       \
  __syscall_return(type,__res);      \
阅读(1849) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~