汇编做应用层设计是不可取的,弄不好不见得能节省代码空间
有时代码速度和代码空间是能二者兼得的.俺就遇到过一类段代码,贴出来给大家看看. 下面是新的代码,采用联合体拆解数据.
uint8 SD_read_sector(uint32 addr,uint8 xdata *Buffer) //**************************************************************************** {
uint8 idata CMD[6]; union SDSector { uint32 ulAddress; uint8 ucADDByte[4]; } CurrentSDSector;
CMD[0] = 0X51; CMD[4] = 0X00; CMD[5] = 0XFF;
addr = addr << 9; //addr = addr * 512 CurrentSDSector.ulAddress =addr;
CMD[1] = CurrentSDSector.ucADDByte[0]; CMD[2] = CurrentSDSector.ucADDByte[1]; CMD[3] = CurrentSDSector.ucADDByte[2];
return SD_Read_Block(CMD, Buffer); }
下面是老的代码,用的是程序变换,不仅占空间,而且费时间,但看起来较容易理解,可读性貌视很好.
uint8 SD_read_sector(uint32 addr,uint8 *Buffer) //**************************************************************************** { uint8 CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF}; addr = addr << 9; //addr = addr * 512
CMD[1] = ((addr & 0xFF000000) >>24 ); CMD[2] = ((addr & 0x00FF0000) >>16 ); CMD[3] = ((addr & 0x0000FF00) >>8 );
return SD_Read_Block(CMD, Buffer, 512); } |
|