包含返回代码部分,用C要想高效还是传递指针最好,可以通过指针直接修改原始数据,节省了大量的数据传递时间,那样应该和汇编效率不相上下.
原代码 unsigned long SwapINT32(unsigned long dData) { // dData = ((dData&0xff)<<24) |((dData&0xff00)<<8) | // ((dData&0xff000000)>>24) | ((dData&0xff0000)>>8);
union{ U8 b[4]; U32 x; }temp;
temp.b[3] = ((U8*)&dData)[0]; temp.b[2] = ((U8*)&dData)[1]; temp.b[1] = ((U8*)&dData)[2]; temp.b[0] = ((U8*)&dData)[3];
return temp.x; } 编译效果 ; #define U8 unsigned char ; #define U16 unsigned int ; #define U32 unsigned long ; ; ; unsigned long SwapINT32(unsigned long dData)
RSEG ?PR?_SwapINT32?MAIN _SwapINT32: USING 0 ; SOURCE LINE # 130 MOV dData?141+03H,R7 MOV dData?141+02H,R6 MOV dData?141+01H,R5 MOV dData?141,R4 ; { ; SOURCE LINE # 131 ; // dData = ((dData&0xff)<<24) |((dData&0xff00)<<8) | ; // ((dData&0xff000000)>>24) | ((dData&0xff0000)>>8); ; ; union{ ; U8 b[4]; ; U32 x; ; }temp; ; ; temp.b[3] = ((U8*)&dData)[0]; ; SOURCE LINE # 140 MOV temp?142+03H,dData?141 ; temp.b[2] = ((U8*)&dData)[1]; ; SOURCE LINE # 141 MOV temp?142+02H,dData?141+01H ; temp.b[1] = ((U8*)&dData)[2]; ; SOURCE LINE # 142 MOV temp?142+01H,dData?141+02H ; temp.b[0] = ((U8*)&dData)[3]; ; SOURCE LINE # 143 MOV temp?142,dData?141+03H ; ; return temp.x; ; SOURCE LINE # 145 MOV R7,temp?142+03H MOV R6,temp?142+02H MOV R5,temp?142+01H MOV R4,temp?142 ; } ; SOURCE LINE # 146 ?C0020: RET ; END OF _SwapINT32
END |