织梦CMS - 轻松建站从此开始!

罗索实验室

当前位置: 主页 > 基础技术 > C/CPP专题 >

ARM汇编实例

落鹤生 发布于 2010-06-23 20:51 点击:次 
ARM下的一段C代码与CCS下反汇编代码比较。
TAG:

  1. void main(void
  2.     tsk_test5(); 
  3.     ....... 
  4. void tsk_test5(void
  5.    unsigned int i,j; 
  6.    unsigned char a,b; 
  7.    unsigned long l; 
  8.    i=0xAA55; 
  9.    j=0x55AA; 
  10.    a=0x88; 
  11.    b=0x99; 
  12.    l=0x11223344; 
  13.    while(i--){   
  14.       printf("haha5\n");   
  15.       printf("test5 count = %d",count++); 
  16.       funtionarg(i,b,l); 
  17.    
  18.        } 


//CCS下反汇编
void tsk_test5(void)
{
800019B8 STMFD         R13!, {R0-R3, R14} //这块没有为局部变量分配栈空间,不需要,因为此函数被main()第一个调用
                                            //所以可以用前面的空间也不会覆盖什么数据,如用[R13,0x4]存放j,
                                            //因为这个位置先前也没数据,所以不用sub R13,R13,0xXX;来分配空间
    unsigned int i,j;
    unsigned char a,b;
    unsigned long l;
    i=0xAA55;
800019BC MOV           R12, #0x55          //R12=0x55;
800019C0 ADD           R12, R12, #0xAA00   //R12=R12+0xAA00;
800019C4 STR           R12, [R13]          //*R13=R12;压栈
    j=0x55AA;
800019C8 MOV           R12, #0xAA         
800019CC ADD           R12, R12, #0x5500
800019D0 STR           R12, [R13, #0x4]   //*(R13+4)=R12;store 4 Btytes to momery
    a=0x88;
800019D4 MOV           R12, #0x88
800019D8 STRB          R12, [R13, #0x8]   //*(R13+8)=R12,store byte to momery
    b=0x99;
800019DC MOV           R12, #0x99
800019E0 STRB          R12, [R13, #0x9]   //*(R13+9)=R12,store byte to momery
    l=0x11223344;
800019E4 LDR           R12, CON18
800019E8 STR           R12, [R13, #0xC]   //*(R13+12)=R12,好像是存储4字节(要4字节对齐)
    while(i--){
800019EC LDR           R12, [R13]        
800019F0 SUB           R0, R12, #0x1      //把i值放入寄存器中并减1
800019F4 CMP           R12, #0x0          //比较 看是否为0
800019F8 STR           R0, [R13]          //先减后存放,
800019FC BEQ           DW$L$_tsk_test5$2$E
    printf("haha5\n");
80001A00 DW$L$_tsk_test5$2$B, L17:
80001A00 ADD           R0, PC, #0x64
80001A04 BL            printf
    printf("test5 count = %d",count++);
80001A08 LDR           R0, CON19
80001A0C LDR           R1, [R0]
80001A10 ADD           R12, R1, #0x1
80001A14 STR           R12, [R0]
80001A18 ADD           R0, PC, #0x54
80001A1C BL            printf            //没有用栈传递参数,可能是因为printf是库函数吧,
    funtionarg(i,b,l);
80001A20 LDR           R0, [R13]         //R0 R1 R2 把i,b,l传递过去
80001A24 LDRB          R1, [R13, #0x9]
80001A28 LDR           R2, [R13, #0xC]
80001A2C BL            funtionarg
    }
80001A30 LDR           R12, [R13]
80001A34 SUB           R0, R12, #0x1
80001A38 CMP           R12, #0x0
80001A3C STR           R0, [R13]
80001A40 BNE           L17
80001A44 L18, DW$L$_tsk_test5$2$E:
80001A44 STR           R0, [R13]
80001A48 LDMFD         R13!, {R0-R3, PC} //弹栈,R13的值一直没改变
}

int iprintf(const char *pbFmt,...)
{
        va_list pArg;
        char abString[256];

        va_start(pArg,pbFmt);
        vsprintf(abString,pbFmt,pArg);
      // UARTSendData(abString);
        va_end(pArg);

        return 0;
}
int iprintf(const char *pbFmt,...)
{
800019B8 STMFD         R13!, {R0-R3}//子函数中不必保护R0~R3,但必须压栈。
800019BC STMFD         R13!, {R7, R14}//如使用R4~R11必须保护,
800019C0 ADD           R7, R13, #0x8   //R7=R13+8;
800019C4 SUB           R13, R13, #0x104
        va_list pArg;
        char abString[256];

        va_start(pArg,pbFmt);
800019C8 ADD           R12, R7, #0x4 //没看懂,好像没通过SP传参,还是通过R3
800019CC STR           R12, [R13]
        vsprintf(abString,pbFmt,pArg);
800019D0 ADD           R0, R13, #0x4
800019D4 LDR           R1, [R7]
800019D8 LDR           R2, [R13]
800019DC BL            vsprintf
        va_end(pArg);

        return 0;
800019E0 MOV           R0, #0x0
800019E4 ADD           R13, R13, #0x104
800019E8 LDMFD         R13!, {R7, R14} //恢复R7和R14,
800019EC ADD           R13, R13, #0x10 //和压入的R0~R3对应;不必恢复
800019F0 BX            R14              //带状态切换的跳转指令
}

unsigned int funtionarg(unsigned int m,unsigned char k,unsigned long l)
{
   unsigned int i,j;
   unsigned char a,b;
   unsigned long ll=0;
   j=0x55aa;
   i=m+j;
   a=0x88;
   b=a+0x11+k;
   ll=l+1;  
adc_ch0( i);
   return i;
}
unsigned int funtionarg(unsigned int m,unsigned char k,unsigned long l)
{
80001944 STMFD         R13!, {R14}      //压入LR,也就是返回地址
80001948 SUB           R13, R13, #0x1C //分配栈空间
8000194C STR           R2, [R13, #0x8] //参数l压栈
80001950 STRB          R1, [R13, #0x4] //参数k压栈
80001954 STR           R0, [R13]        //参数m压栈
    unsigned int i,j;
    unsigned char a,b;
    unsigned long ll=0;
80001958 MOV           R12, #0x0
8000195C STR           R12, [R13, #0x18]
    j=0x55aa;
80001960 MOV           R12, #0xAA
80001964 ADD           R12, R12, #0x5500
80001968 STR           R12, [R13, #0x10]
    i=m+j;
8000196C LDR           R0, [R13, #0x10]
80001970 LDR           R12, [R13]
80001974 ADD           R12, R0, R12
80001978 STR           R12, [R13, #0xC]
    a=0x88;
8000197C MOV           R12, #0x88
80001980 STRB          R12, [R13, #0x14]
    b=a+0x11+k;
80001984 LDRB          R0, [R13, #0x4]
80001988 LDRB          R12, [R13, #0x14]
8000198C ADD           R12, R0, R12
80001990 ADD           R12, R12, #0x11
80001994 STRB          R12, [R13, #0x15]
    ll=l+1;
80001998 LDR           R12, [R13, #0x8]
8000199C ADD           R12, R12, #0x1
800019A0 STR           R12, [R13, #0x18]
    adc_ch0( i);
800019A4 LDRH          R0, [R13, #0xC] //加载无符号半字数据,即低16位
800019A8 BL            adc_ch0
    return i;
800019AC LDR           R0, [R13, #0xC]
800019B0 ADD           R13, R13, #0x1C
800019B4 LDMFD         R13!, {PC}
}
}

(myyb)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www1.rosoo.net/a/201006/9706.html]
本文出处:百度博客 作者:myyb
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容